When is a function name too long?

后端 未结 27 1381
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 18:45

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 \"GetActionFr

相关标签:
27条回答
  • 2021-02-03 19:10

    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 by FindArticlesWithoutTitlesThenApplyDefaultStyles. 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 be FixArticleStyles) 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 to FindNoTitles depending on the appropriate context or maybe even just FindArticles if I have no other article finding functions.

    0 讨论(0)
  • 2021-02-03 19:13

    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.

    0 讨论(0)
  • 2021-02-03 19:13

    Trying to avoid subjectivity:

    When names get to be about 1/3 of whatever your typical line length is, then you're stretching it. At 1/2 line length you're too far gone. One statement per line gets pretty tough when the names take up the whole line.

    Beyond that, most IDEs support completion (thereby saving the programmer from actually typing most names out fully), so the more important thing to do, in my opinion, is make sure that the name is as unique as possible as soon as possible.

    0 讨论(0)
提交回复
热议问题