开发者

Difficulty in naming functions [duplicate]

开发者 https://www.devze.com 2023-01-04 17:10 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Anyone else find naming classes and methods one of the most difficult part in programming?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Anyone else find naming classes and methods one of the most difficult part in programming?

Sometime开发者_如何学运维s it seems i cant really find any name for a function i am writing, can this be because the function is not cohesive enough?

What do you do when no good name for a function comes to mind?


For naming functions, just avoid having simply nouns and rather name them after verbs. Some pointers:

  1. Have function names that are unique visibly, e.g. don't have validateInput() and validateUserInput() since it's hard to say what one does over another. Also, avoid having characters that look very similar, e.g. the number 1 and lowercase 'l'. Sometimes it makes a difference.
  2. Are you working on a project with multiple people? You should spend some time going over naming conventions as well, such as if the function name should have underscores, should be camelCase, etc.
  3. Hungarian notation is a bad idea; avoid doing it.
  4. Think about what the function is doing. The cohesion that you mentioned in your question comes to mind. Generally, functions should do just one thing, so don't name it constructCarAndRunCar() but rather have one function that constructs and another that runs it. If your functions are between, say 20 and 40 lines, you're good.
  5. Sometimes, and this depends on the project, you might also want to prefix your function names with the class if the class is purely procedural (only composed of functions). So if you have a class that takes care of running a simulation, name your functions sim_pauseSimulation() and sim_restartSimulation(). If your class is OOP-based, this isn't an issue as much.
  6. Don't use the underlying data structures in the functions themselves; these should be abstracted away. Rather than having functions like addToVector() or addToArray(), have them be addToList() instead. This is especially true if these are prototypes or the data structures might change later.
  7. Finally, be consistent in your naming conventions. Once you come up with a convention after some thinking, stick to it. PHP comes to mind when thinking of inconsistent function names.

Happy coding! :)


Give it your best-shot and re-factor later if it still doesn't fit.


Sometimes it could be that your function is too large and therefore doing too many things. Try splitting up your function into other functions and it might be clearer what to call each individual function.

Don't worry about naming things with one or two words. Sometimes if functions do something that can be explained in a mini-sentence of sorts, go ahead and name the function a little longer if it'll help other developers understand what is going on.

Another suggestion is to get feedback from others. Often others who come from another perspective and seeing the function for the first time will have a better idea on what to call the function.


I follow following rule: Name according to the purpose (Why? - design decision) and not to the contents (What, How? - can be seen in the code).

For functions it is almost always an action (verb) followed by the noun of parameters and (or results. (Off-topic but for variables do not use "arrayOfNames" or "listOfNames", these are type information but simply "names"). This will also avoid inconsistencies if you refactor the code partly.

For given patterns like object creation, be consistent and always use the same naming like "Create..." (and not sometimes "Allocate..." or "Build..." otherwise you or your collegues will end up in scratching their head wound)


I find it easier to name functions when I don't have to cut back on the words. As long as your not doing javascript for the google start page you can do longer names.

For example you have the method dequeueReusableCellWithIdentifierandmergeChangesFromContextDidSaveNotification in apples cocoa framework.

As long as it's clear what the function is doing you can name it whatever you want and refactor it later.


Almost as important as the function name is that you are consistent with comments. Many IDEs will user your properly formatted comments not only to provide context sensitive help for a function you might be using, but they can be used to generate documentation. This is invaluable when returning to a project after a long period or when working with other developers.

In academic settings, they provide an appreciated demonstration of your intentions.

A good rule of thumb is [verb]returnDescription. This is easy with GetName() type functions and can't be applied universally. It's tough to find a balance between unobtrusive and descriptive code.

Here's a .Net convention guide, but it is applicable to most languages.


Go to www.thesaurus.com and try to find a better suited name though synonyms.


As a practical rule of my own, if a function name is too long, it should be atomized in a new object. Yet, i agree with all posts above. btw, nice noob question

0

精彩评论

暂无评论...
验证码 换一张
取 消