On naming
quietlyamused.org
quietlyamused.org
So I've started also using a thesaurus when I code and so far I'm happy with the results. It's been helpful in accurately framing some of the hazier concepts with interesting names. I also revisit the naming much less than I use to, because I'm comforted with the notion that if I couldn't find a better word with a thesaurus, then maybe there isn't an obvious one. I also now use words that are clear, but that I so far neglected to include in my programming vocabulary, because other more prevalent ones would jump in my mind and put a shadow on my naming creativity.
So, if you need some help naming modules, functions, variables and db entities, a thesaurus could help. Though, take care to not abuse of it.
I'd love to have a "programmer's thesaurus" with more detail on names that appear frequently in code. For one of the two hard problems in CS, the art of naming things has little reference material beyond raw source code.
Maybe something like Visual Wordnet (http://kylescholz.com/projects/wordnet/) would be useful too.
http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus...
Over generalized or specific names cause far more confusion and productivity loss than we think.
So take the time to learn what you're working on and how it works at the micro and macro level not only in the context of the application itself, but where and how the application fits in it's ecosystem.
Learn the lingo that your software's ecosystem uses, instead of inventing, by accident or purpose, your own clever and tricky rationalizations ending up in even more confusing concepts and names describing the same things.
The purpose of naming is similar to the purpose of software; creating clarity and empowerment, instead of more confusion and conceptual gymnastics.
When developers (and the code), product managers, domain experts, and end users all use different terminology, then cross-discipline communication difficult and imprecise. A documented Ubiquitous Language enforces shared terminology (and meaning) for all communication within the team and in the code.
I have found that when I can't find a good name for a method or a class, it's because I haven't really thought the reason for the same through. This is especially true for classes where I often realize that I am making the class do too much, or too little.
This book, "Clean Code" by Robert Martin has it's second chapter dedicated to naming things. A whole chapter! The book, alone for that chapter is worth the money. If you haven't read it, I highly recommend it.
http://www.amazon.com/Clean-Code-Handbook-Craftsmanship-eboo...
http://www.amazon.com/Code-Complete-Practical-Handbook-Const...
There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.
As Lars mentioned—if something is hard to name, it's probably because the thing you're naming isn't very clearly defined, is a bad/mixed metaphor, or is just plain hard to understand.
And if all else fails, there's always http://www.classnamer.com/ ;-)
Once in an interview, I asked the guy to write some code, he used variable names like "int ttttt=1;". I never saw him again!!
The bigger problem is we haven't moved on from the simple text representation of code. And this overly simple tool, text, is influencing the way we're thinking here. The Humble Programmer phenomenon in action.
Good or bad, it works.
double anospic; // average numbers picked
Just raising it as an option; I haven’t actually used that style much myself. With the code I’ve been writing recently I haven’t had any problems so far giving variables names as long as I’d like.The Ruby convention would be to write out the variable with underscores anyway, just sayin'.
Generally, I find that long names fall into three categories:
1) refactoring opportunities
Use more namespaces, write smaller functions, embrace lexical scoping, etc. You don't need to call something a foo-bar-frobber when it's being used in the foo-bar context, just call it a frobber.
2) line noise
If you're only using the frobber within a two line function, and it is initialized via create-frobber, do you really need to spell it out? f or x will do. If your function grows, rename the variable. Or refactor!
3) an absentee synonym
Maybe you've got a monster-which-attempts-to-defeat-player, when you really need a baddie. Or maybe the word you need doesn't exist yet? It's rare, but sometimes you really do need to invent or repurpose a word.
...
I always feel dirty when I have a long variable name. pg writes about this in On Lisp: long names feel heavier. They might be super cheap to call, but because the name is long, you're less likely to call it.
Furthermore, I much rather adhere to a line length limit, as I find wrapping lines far harder to read than lines with short variable names. Long names makes this harder.
All that said, I almost never use abbreviations other than standin variables like a single letter. The only exception to this is well known abbreviations, like for your most commonly used utility functions or for objects which are extremely common in your problem domain. Such a set of abbreviations becomes a part of the language you share between your compiler and your team.
Why would you just not give it a reasonable name to begin with? Why not call your variable frobber? The only possible reason for not doing so is that you're using an editor that makes you type it every single time. I insist that calling your Frobber variable frobber is strictly easier to understand than f or x. Why not t? Or sdfsdg? None of them make any sense to someone reading the code, and it increases the amount the reader of the code has to keep in their short-term memory, making it more challenging to understand.
pg writes about this in On Lisp: long names feel heavier. They might be super cheap to call, but because the name is long, you're less likely to call it.
Even if pg wrote it this is patently absurd, and it's endemic in lisp code which contributes greatly to the famously write-only nature of lisp. Again, if you're using a decent editor, there is no reason to call your UserDetails object anything but user-details (or maybe details if you're in a context only relating to users).
Here's some C:
for (int player_index = 0; player_index < players.length; player_index++) {
int rank = player_index + 1;
render_score(rank, players[player_index].name);
}
Blah. So much for the brain to parse! for (int p = 0; p < players.length; p++) {
int rank = p + 1;
render_score(rank, players[p].name);
}
I find that much, much easier to parse in my head. The definition of p is available without any backtracking. The word "player" appears five fewer times, reducing confusion with the players array. This is a small case.If this code were to start to grow, and the definition of p and its usages start to grow appart, it's trivial to replace.... but it's probably better for you to refactor.
Um... Because that would be hard to read?
I'm advocating using full words in general, avoiding long variables (multi-word phrases, adjectives, contextualization, etc), and not being afraid of short variables names if they help, rather than hurt readability.
There are plenty of cases where short names are totally fine. If you've got a module full of string utilities and every single function takes a first argument of type String, why not just call it "s"? Everyone will figure it out in seconds and it will save the mental overhead of differentiating "String" and "string". when that does come up.
Things like that; I'm saying use your judgement.
I agree with this part and code this way.
>line noise
I generally follow this, it depends on how clear the abbreviation is. If I would need a comment I'll spell out the name instead.
>I always feel dirty when I have a long variable name.
Once it passes a certain size, I do too.
>pg writes about this in On Lisp: long names feel heavier.
This is something I strongly disagree with pg on and I would point to arch as prove that he's wrong here. Arch uses ridiculously short names for everything and I personally can't stand the language. As far as I can tell, the language never got popular despite a decent sized group of people waiting to see what pg would make.
>They might be super cheap to call, but because the name is long, you're less likely to call it.
I think pg arrives at this position because he uses vi. If you have a really nice IDE that does advanced code completion (e.g. I can type "RBV <tab>" for ReallyBigVariable) you'll find that you're no less likely to use a long variable or function name.
>Furthermore, I much rather adhere to a line length limit,
Agreed.
Keep in mind that I don't do this with client work or anything that might land on the hands of another developer.
had to name a variable earlier this week and even broke out the thesaurus. best i could muster was years_experienced_absolute :/
Since naming is hard, I try to minimize the number of names I create. I think the number of names in a codebase is a measure of complexity.
It's very similar to the phenomenon of notation (including maths, sciences, and, yes, programming languages themselves):
"By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental power of the race." -- P Davis and R Hersh The Mathematical Experience (Boston 1981).
I know that my team spends an inordinate amount of time debatting the names of concepts. We've found that, since we're trying to build something unique, there aren't always words for the concepts we're building. Worse yet, there are words that are close, but not quite right! It's extremely difficult to write any code before everyone can hear a word and reach a common mental image. I've found that if you get the right names, the code is so much easier to write and reason about.