6 ms·
"The method name getAbsoluteDifference() means to get the absolute difference between the pair of numbers. The method name contains a verb get. It would not ma
by delinka 10y ago
"The method name getAbsoluteDifference() means to get the absolute difference between the pair of numbers. The method name contains a verb get.
It would not make much sense to name the method simply absoluteDifference(), because such name does not indicate any action."
I disagree. I want the property of 'absolute difference' between the two numbers. Getting that value is what I do when I make an assignment. Whether this value is cached or calculated is of little consequence to outside code provided it has no side effects on other state within the object. Should a method (member function, whatever) cause side effects within the target object, then the method has become a verb. Like, 'increase(by:)' - that's a verb, affects the state of the object ... well-named, friend.
Aside from that, I think the rest has embodied well Apple's talk about Swift API style from WWDC with a nice dose of input from this author.
Edited to add: 'getAverage()' shows up later. I fail to see how 'get' adds anything of importance considering its company 'reversed' and 'sorted' - they should all prefix with 'get' or they should not. I'm in the 'not' camp.
- viraptor 10y agoIt's interesting how functions and properties can change the appearance. I like .xyz or .getXyz() - but the other way around it just seems wrong.
- function_seven 10y agoIt would make me sad to type getSqrt(), getMin(), getMax(), getStrLen(), getSizeOf(), get… Putting a verb in the function name is not a bad idea, but if you find yourself identifying the verb as “get”, then it’s a useless three extra letters.
- TillE 10y agoI think for most people, "getFoo" generally implies direct access to a data member, not a calculation. This is a useful hint, and an argument for not using it in other contexts.
- function_seven 10y agoGood point. For me personally, I find myself using “get” whenever a function or method will query a database or some other external source to deliver the result. For example, getNormalizedDeliveryAddress(), or getRelatedComments().
- catlover99 10y agoPersonally, for whatever reason in my mind, I think "get" as referring to something local which is why I choose between three different verbs, "grab", "fetch", or "query" (typically grab), as a descriptive prefix for functions that retrieve data from external sources.
- pvg 10y agoI doubt that's most people unless most people are confused. The whole point of having getters and setters is to abstract away the implementation details of where the value comes from. Direct access to a data member is easily achieved via direct access to the data member, it doesn't require a method.
- Stratoscope 10y agoBack in the early days of Pascal programming for both DOS and Mac, it was common to put a 'the' prefix on just about every variable. I guess it was supposed to be more "English-like". You'd have variables like this: theWindow theUser theFile theLineCount I hated it! I never did find out why people wanted to put in such a useless noise word in every single variable name, but I decided to never do it again. Make every word count.
- jejones3141 10y agoThat would almost tempt me in Algol 68, where, like FORTRAN, space isn't significant, and where members of structures are referred to as foo OF bar rather than bar.foo but as you say, in code "the" is a noise word, so I'd resist. :)
- ams6110 10y ago"my" was another often-seen useless prefix of that era
- flukus 10y agoIt still has it's uses as a class name prefix. MyFoo extends GenericFoo.
- louthy 10y agoIndeed, it is horrific. As is Hungarian notation. Anything where the type or locality ('m_foo', 'g_var') has infiltrated the name of the variable says to me that the variable is named badly.
- Terr_ 10y agoI'm in the "do" camp, because using "get" (or similar verbs) offers a hint that the method does not mutate the object. "Average" can be both a noun and a verb, so "getAverage()" clarifies that it's being used as a noun. An alternate but less-common approach would be to assume verbs and conjugate, like "averaged()"... But what if the verb and noun have very different meanings?