determining which verb to use for method names in Java

后端 未结 7 2131
半阙折子戏
半阙折子戏 2021-01-30 23:02

I understand that naming conventions are important for a number of reasons, most having to do with making your code more readable and easier to integrate into larger projects, e

相关标签:
7条回答
  • 2021-01-30 23:07

    I think to find anything which could be a "solution" for the problem we should first extract criteria which play any role in choosing name, like:

    • readableness (trivial: getValue instead of transferValueFromThisObjectToYou)
    • convenience (trivial: getCoordValue instead of )
    • semantics of the method (get is not the same as calculate)
    • context of usage (in IDEs I usually type aaa.get_ and press Ctrl+Space to check what I can get from the object)
    • code guides (or other "guides" like Java Bean convention which force you to use some names)

    ...

    However as suat said - better spend some efforts on a documentation for your methods.

    0 讨论(0)
  • 2021-01-30 23:11

    Regarding the use of get and set methods for property accessors only: the whole point of information hiding is that the user of an API (i.e. the calling code) should not need to know or depend on whether the property is stored or calculated on the fly. The implementation should be able to change at any time, as long as the API stays the same.

    0 讨论(0)
  • 2021-01-30 23:14

    I usually ask myself:

    What is this method doing?

    The answer dictates what the method should be called. It is completely independent of the programmer, of course.

    Note: If you can't succinctly describe what the method is doing, it's probably doing too much and should be split up.

    Choosing your method's verb:

    • Performing calculation(s): calculate
    • Retrieving data: get or retrieve
    • Mutating data: set or change
    • Deleting data: delete or remove
    • Converting: convert
    • Initiating an action: start or initiate
    • Stopping an action: stop or cancel

    Now, not all methods begin with a verb; but they really don't need to. If you read:

    ... myString.length();
    

    or

    ... myArray.size();
    

    you know exactly what is going on - no verb required. This is true for many class methods higher up in the Java hierarchy; Collections, Math, etc. As long as the name accurately communicates what the method does, it's fine.

    0 讨论(0)
  • 2021-01-30 23:15

    As you said and as we can see in the answers, the verbs used at the beginning of method names are almost the same verbs. I think, if same amount of effort is spent for writing related documentation, methods become much more understandable and integrable :)

    I also realized after reading the question, most of the methods I write start with get, retrieve, create. So again it seems verb selection does not matter so much.

    Best

    0 讨论(0)
  • 2021-01-30 23:23

    I don't think java method names should "begin with a verb", I think they should describe the action. This often requires a verb, as verbs describe actions. Usually, they are important parts of the description (getVar and setVar mean totally different things). Occasionally, they add nothing to the description (can you think of anything that would operate on movingAverage besides get/calculate/generate?) and should be dropped.

    0 讨论(0)
  • 2021-01-30 23:24

    (My opinion) You don't need a verb in the method name if the object has no state, like a math-library. Compare computeSquareRoot(x) and getJoin(listOfStrings) with squareRoot(x) and join(listOfStrings).

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