When is a function name too long?

后端 未结 27 1378
伪装坚强ぢ
伪装坚强ぢ 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 18:46

    When it contains information which is obvious from context (e.g., incrementInteger(int x), long longID), useless (e.g., ObsoleteIncrementer, RobertsCarFactory), incomprehensible (e.g., TheFunctionThatRobertWorkedOnLastWeekButDidntFinish), numeric (e.g., id1, id2, id3) or otherwise doesn't help understanding or contains a code smell. Note that even though some part of the names above should be pruned, you might need to pad them with useful information to keep them unique and make them understandable, as in person_id for id1, employer_id for id2 etc..

    0 讨论(0)
  • 2021-02-03 18:47
    1. If you have to scroll to the right to read it.
    2. Describes the 3 or more things that is does - it shouldn't do that many things.
    3. Your boss thinks its too long.
    4. It's longer than the code itself.
    5. It starts with Get, just like 500 other functions.
    6. Nobody wants to use it.
    7. There is another function that does the same thing with a shorter name that users understand.
    8. It can be made shorter.
    0 讨论(0)
  • 2021-02-03 18:47

    It may be a bit off topic, but as you asked specifically for a function name guideline (as opposed to say, method), I figured I would quote Linus Torvalds on naming (although it more refers to variables, but still - the principles hold).

    Chapter 3: Naming

    C is a Spartan language, and so should your naming be. Unlike Modula-2 and Pascal programmers, C programmers do not use cute names like ThisVariableIsATemporaryCounter. A C programmer would call that variable "tmp", which is much easier to write, and not the least more difficult to understand.

    Short, descriptive names go well with short, specific functions... which go well with code reuse.

    0 讨论(0)
  • 2021-02-03 18:47

    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.

    0 讨论(0)
  • 2021-02-03 18:48

    A function name is too long when it starts to either over-describe what it does, or when it hinders readability of the code.

    0 讨论(0)
  • 2021-02-03 18:48

    I'd say when you find yourself finding abbreviations for their names when referring to them. I also find it too much when people start describing pre-/post-/parameter- conditions in the names or giving hints on the implementation. (like getVersionInformationFormTheDatabase() or doSomethingWithoutCheckingFooFirst())

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