I try to be rather descriptive with my function names, where possible. This occasionally results in function names in the twenty to thirty character range such as "GetActionFromTypeName" or "GetSelectedActionType". At what point do functions get too long to manage (not too long for the compiler)?
相关问题
- Name for a method that has only side effects
- Best way to keep the user-interface up-to-date?
- should I write more descriptive function names or
- Open a new tab in firefox and keep ff in the backg
- How do I write a function to compare and rank many
相关文章
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- Is there an existing solution for these particular
- Should I keep bad naming conventions?
- What is Scope Creep? [closed]
- How can I modify .xfdl files? (Update #1)
- F# naming convention
- coding style checker for c (variable names, not in
If the function name is 'too long' then it is likely that the function itself is also too long and has too much responsibility. Many wise programmers say that a function should do one thing and one thing only. A function whose name that has to be long to accurately describe what it does is likely to be a good candidate for refactoring into multiple smaller and simpler private functions that consequently have shorter names.
A function name is too long when it starts to either over-describe what it does, or when it hinders readability of the code.
I think you should worry more about when a function name is too short, or not descriptive enough. As long as your function does what its name describes (and everything its name describes), it is well-named. I often write functions with long names like getPropertyNameArrayFromObject (though I tend to underscore rather than camelize), which could be called getObjPropArr or something else but wouldn't be as descriptive. I tend to stay away from abbreviations because they become ambiguous when you go work on something else and come back to the code.
On the other hand, consider many built in PHP functions such as stricmp which should really be named something along the lines of caseInsensitiveStringComparison.
And there are cases where I intentionally write very short function names that are not descriptive at all. Sometimes I just want a short JavaScript function to act as a shortcut. For example, I generally alias $(id) to document.getElementById(id) because I get sick of typing that out.
Ah, a question with no answer!
I tend to find if I can't encapsulate it in a few words, then there's something up with the design (paracribbing from Code Complete).
So while I'm happy with
FindArticlesWithoutTitles
I would probably be disgusted byFindArticlesWithoutTitlesThenApplyDefaultStyles
. This is just wrong; either the name is too technical and not describing it's actual function (titles without articles often need styles to be fixed, so this would beFixArticleStyles
) or it should be two functions:FindArticlesWithoutTitles/ApplyDefaultStyles
.Also: frequency has much to do with it. If it's used often, I want it to be short, to reduce the glare of the code; long repetitive names make code ugly to read and a pain to type. If I'm always finding
FindArticlesWithoutTitles
I might just shorten toFindNoTitles
depending on the appropriate context or maybe even justFindArticles
if I have no other article finding functions.Function and method names start getting too long when the developer forgets about the descriptive nature of arguments (assuming meaningful argument and variable names). For example:
is transparent in what it does even if you know no Perl and have never heard of HTML::Template.
I have seen too often the temptation to name that
output
method something likeRenderHTMLViewFromTemplateObject
. If all the libraries used have such naming conventions, it becomes simply impossible for me to follow what is going on.Method names can be very very long depending on the language (Maximum Method Name Length). At some point your going to use that function or method and typing out a sentence for a function names seems unnecessary.
Keep your code maintainable, use comments instead.