by delinka · 10y ago · 0 comments
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.
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.
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.
foo OF bar
rather than bar.foo
but as you say, in code "the" is a noise word, so I'd resist. :)"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?