Complexity in Naming Functions

Possible duplicate:
Does anyone find classes and naming methods one of the most difficult to program?

Sometimes it seems that I can’t find any name for the function that I am writing, can it be because the function is not cohesive enough?

What do you do when a good name for a function does not occur?

+7
function naming
source share
8 answers

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

  • Names of functions that are unique, for example, do not have validateInput() and validateUserInput() , since it is hard to say what it does on another. Also, avoid having characters that look very similar, for example. number 1 and lowercase "l". Sometimes it matters.
  • Are you working on a project with several people? You should also spend some time on named conventions, for example, if the function name must have underscores, it must be camelCase, etc.
  • Hungarian notation is a bad idea; avoid this.
  • Think about what the function does. The explanation you spoke of in your question comes to mind. As a rule, functions should perform only one thing, so do not call it constructCarAndRunCar() , but rather create one construct and the other that runs it. If your functions are between, say, 20 and 40 lines, you are good.
  • Sometimes, and it depends on the project, you may also need to prefix the name of your function with the class if the class is purely procedural (consisting only of functions). Therefore, if you have a class that manages simulation, name your functions sim_pauseSimulation() and sim_restartSimulation() . If your class is OOP based, this is not a problem.
  • Do not use basic data structures in functions themselves; they should be distracted. Instead of having functions like addToVector() or addToArray() , let them be addToList() . This is especially true if these are prototypes or data structures may change later.
  • Lastly, be consistent in your naming conventions. Once you come up with an agreement after some thought, stick to it. PHP comes to mind when thinking of inconsistent function names.

Happy coding! :)

+14
source share

Sometimes it can happen that your function is too great and therefore does too many things. Try to split your function into other functions, and it may be more clear what to call each individual function.

Don't worry about calling things one or two words. Sometimes, if functions do something that can be explained in a mini-sentence, continue and call the function again if it helps other developers understand what is going on.

Another suggestion is to get feedback from others. Often others who come from a different perspective and see the function for the first time will have a better idea of ​​how to call the function.

+2
source share

The following rule: Name according to purpose (why? - design decision), and not the content (What, How? - can be seen in the code).

For functions, this is almost always an action (verb) followed by a noun of parameters and (or results. (Not for the topic, but for the variables are not used "arrayOfNames" or "listOfNames", this is type information, but simply "names"). This will also avoid inconsistencies if you partially edit the code.

For given templates, such as creating objects, consistently and always use the same name, for example, "Create ..." (and sometimes "Select ..." or "Create ..." otherwise you or your colleagues end up scratching your head wound)

+2
source share

Give him the best shot and repeat the factor later if it still doesn't fit.

+1
source share

Go to www.thesaurus.com and try to find a more suitable name, albeit synonymous.

0
source share

Almost as important as the function name is that you agree with the comments. Many IDEs will use your properly formatted comments not only to provide context-sensitive help for a function that you could use, but you can use them to create documentation. It is invaluable when returning to a project after a long period or when working with other developers.

In an academic setting, they provide a valuable demonstration of your intentions.

A good rule is [verb] returnDescription. This is easy with functions like GetName () and cannot be used universally. It's hard to find a balance between unobtrusive and descriptive code.

Here . Net usage guide , but applicable to most languages.

0
source share

It’s easier for me to name functions when I don’t need to shorten words. Until you do javascript for the google start page, you can make longer names.

For example, you have the dequeueReusableCellWithIdentifier and mergeChangesFromContextDidSaveNotification in the cocoa framework core.

As long as it’s clear what the function does, you can name it whatever you want and reorganize it later.

0
source share

As a rule, my own, if the function name is too long, it should be sprayed into a new object. Nevertheless, I agree with all of the above. btw, good question noob

0
source share

All Articles