Coding like Shakespeare: practical function naming conventions
rainsoft.io
rainsoft.io
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?
My go-to example is the Tensorflow code base. It sounds intimidating, deep learning, etc., right? But take something like the Seq2Seq implementation, and marvel how elegant and readable the module is. Some part of that is due to the simplicity of "programmer happiness languages" like Python and Ruby (sorry Java, JavaScript), but it's also clear that the easy part for them was making it work, while the part that probably took most of their focus was engineering simplicity. To even be in that position means you're already really, really good.
I typically contrast this with another Seq2Seq implementation in another certain library (I won't call out anybody) done by a PhD student, that looks very intelligent, but with no focus on engineering simplicity or elegant naming, it is an exercise in frustration to get through.
It is very nice to read.
maybe one off scripts should be like lyric poetry though.
I disagree! This is a bad mindset that gets dispelled when you use a language that distinguishes between functions and actions. Writing to a database is doing something; returning the second value of a tuple isn't. There's no good reason to put a verb in the function that takes a tuple and projects out its subcomponents. "first" or "firstOf" gets the point across more concisely.
- Don't force people to guess. Guessing takes time, and is an opportunity for a mistake.
- Explicit information is better than implicit information. Abbreviations, incomplete identifiers are a form of implicit information.
- Lying exists in programming. Identifiers that do not match their purpose can mislead programmers and introduce bugs. e.g: Including side-effects in a function is called "get", "read" or "retrieve".
- Coupling brings more information into a scope, making it more complex and decreasing readability.
Maybe I'm just too biased because I try to have const-correct/mutable-correct code?
https://en.wikipedia.org/wiki/Shakespeare_Programming_Langua...
Good names mean a great deal, but this is too much to expect from them. Understanding what a function does includes issues such as its time complexity, whether it is thread safe, what (if anything) it modifies, what exceptions it might throw and what exception safety guarantees it honors... And there may well be specific corner cases - for example, in a function with 'find' in its name, what does it do if there are zero or multiple matches to the key being sought?
"for each card in deck" is much better then "for each item in list" as card and deck gives much more information!