Before:
++i; /* record another match of this expression */After:
++number_of_expression_matches;As the example above demonstrates, a typical comment can be encoded in an identifier. Most programmers attempt to encode some information in an identifier that is useful in understanding what role the identifier serves in the code. However, this information could be completely misleading or even complete gibberish. If the programmer isn't vigilant when modifying code, identifiers with initially well chosen names could end up playing very different roles in the code while retaining their old names. This is exactly the same problem as when a comment isn't updated to reflect changes in the code it is documenting.
This is a refactoring issue. When you change what the code does, you should also change identifier names (and method names: IntentionRevealingSelector) as appropriate to make sure the code still communicates its intent correctly. At least you have the advantage that your identifiers and method names are precisely captured in a form amenable to tool manipulation (source code). It's much easier to update the references to an identifier than it is to find and fix all the comments which describe in English a certain concept.
A great quote someone left on the CommentCostsAndBenefits page:
One of the tenets of ExtremeProgramming is that large methods should be broken up into small methods. This kind of code will have a lot more semantic annotation in the form of all these extra method names that code with longer methods won't have. If XP also stresses long method names, then even more annotation will be present. If identifiers really are comments, then practitioners of XP are in fact systematically documenting their code even though they officially eschew comments. Is this correct?
Hmmn - I agree that meaningful identifiers can obviate the need for various comments. But at what point does it become impractical to use a really long variable name? Something like the example above is pretty darn unwieldy to type if its a commonly used variable. Yes - we all know that programs are supposedly "write once; read many." But at some point, doesn't the length of the variable name and the inconvenience of typing it and the increased likelihood of mistyping it begin to outweigh its usefulness?
number_of_expression matches is very clear, but also very long and unwieldy. num_expr_matches is a tad shorter. How much less readable is it? What about num_matches? Where is the midpoint? (somewhere between 16 & 32 characters perhaps???) Or is it just a matter of having nice tools (like Emacs for example) that have auto-completion for your identifier.
I'm not trying to be facetious, this is a genuine question I have -- BradAppleton
Names should be as meaningful as possible, but not necessarily long. I'd never abbreviate, if I weren't so lazy. "maxPay" is probably ok, but "maximumPayment" probably better. Readers who think that code writers are thinking about them when they write code are, in fact, stupid. We write this way because we are stupid. -- RonJeffries
I don't think it's that we are stupid, more that we are forgetful. Just as we were entrenched and immersed in the muck and mire when we coded a particular section, we are often more distant from it when we revisit (and entrenched in something else ;-) So if we are thinking only of ourselves, we use names and comments that will trigger the requisite mental association to "refresh our mental state" and push the retrieved context onto our mental stack.
I think this is different (or at least *can* be different) from trying to teach or explain to first timers with our names and comments. The former presupposes a non-trivial degree of "clue" about the overall design/organization and tries to "recreate" a mental flow-state. The latter makes no such presupposition (or at least significantly less). Which is better? Who can say, it probably varies from shop-to-shop. -- BradAppleton
Ya know, every now and then someone around here starts a conversation to which I can actually contribute something. Makes me feel all glow-y.
CodeComplete, SteveMcConnell's killer book on coding, offers the following interesting data point:
Gorla, Benander, and Benander found that the effort required to debug a COBOL program was minimized when variables had names that averaged 10 to 16 characters (1990).
Also, simply from my own experience, I would add that a very important convention for naming identifiers is the use of specialized sub-words with absolutely rigid meanings:
I agree with Brad's comment about context. As an instance variable (of a regular expression class) I might call it "matchCount". In a function, I might call it just "count", because the function name would make it obvious what it was counting. One consequence is that variables (and methods) usually get renamed as they leave their context. -- DaveHarris
I think IndexOrCount deserves more discussion. -- JayBazuzi
This appears to a case of being overly rigid. I find the use of "number" in the above example to be perfectly understandable and correct. Other synonyms, such as "count" or "quantity" or even "total" used as a suffix (expression_match_total) provide equivalent meaning for the variable name. Focus on providing a clear name, and avoid worrying about limiting the vocabulary to be used, though brevity may be important. -- WayneMack
This is a subject near to my heart. See http://www.oma.com/ottinger/Naming.html. As far as having long names, there are lots of ways to work with that. One is to use a good editor. If you use even something as primitive as vi (I use gvim a lot) you have an abbreviation command. You tell it "abb nexpr number_of_expressions", and when you type "nexpr" it expands the word. It is smart enough to wait to make sure the character after 'r' is really a punctuation or whitespace.
Surely the modern IDEs aren't so stupid that they can't do this? If you have an editor that insists on acting like a word processor (mouse-heavy, leave the home row constantly) you can type nexpr, then highlight the function and do a replace so it changes the short name to the long. Or, I suppose, there's the cut-n-paste.
So having more modern (and yet more primitive) tools shouldn't prevent us from having good identifier names. If worse comes to worse, you can always learn VI (gvim is great) or EMACS and you can easily do abbreviations and the like.
-- TimOttinger
(PatternsOfSoftware discusses tools that do this: RichardGabriel seems to like the idea, but in this case might like the editor to also be able to show you the abbreviated form. -- MartinPool)
Even better: writing Java with EclipseIde, you can type the first few letters of an identifier and then press Ctrl-Space for code assist, and it pops up a menu with any matching identifiers (or if there is only one, it just inserts it for you).
Given IdentifiersAreComments and what we know about comments, then: (1) use good identifiers named by intention, and (2) see if you can refactor rather than commenting. If you must use globals, give them good names, but better yet is to factor them out entirely. --MartinPool
double Q = this->maximumPrimaryEnergyUsageForTheMonth(); double P = this->currentPrice(); return (P*Q)/pow(1-P*P, P/Q);or
Select C.name, A.balance From dbo.t_currentcustomer C, dbo.t_acctreceivables A Where C.id = A.customer_idI'm leaning toward the adage: if "local variable names matter, your method is too long".
-- JohnClonts
What about, instead:
double energy = this->maximumPrimaryEnergyUsageForTheMonth(); double price = this->currentPrice(); return (price*energy)/pow(1-price*price, price/energy);Hmm, interesting ... I thought I'd prefer the longer names ... but in this case I'm not sure I do. Hmmm. --
There is at least one undiscovered method in the last line. --
The name energy is poorly chosen. Consider using quantity or usage instead. --
Umm...you're advocating maximumPrimaryUsageUsageForTheMonth? :-)
No, I'm suggesting:
double quantity = this->maximumPrimaryEnergyUsageForTheMonth(); double price = this->currentPrice(); return (price*quantity)/pow(1-price*price, price/quantity);or:
double usage = this->maximumPrimaryEnergyUsageForTheMonth(); double price = this->currentPrice(); return (price*usage)/pow(1-price*price, price/usage);
For example, variable identifiers could be composed of one or more capitalized words; class identifiers of one or more all-uppercase words; and method identifiers of one or more all-lowercase words. If a generalized form of the Smalltalk message syntax were adopted - one where parameters may be arbitrarily placed in the method identifier, e.g., "kill Evil Bear brutally with Stick" - I think it could be pretty cool.
In lisp, you can have those.
Long names are a OnceAndOnlyOnce sin. If a variable is commonly-used, then long names just bloat up the code size and make it harder to read. For example, single lines are more likely to need wrapping if there are lots of long names, and wrapped lines decrease readability of code greatly (in my opinion/experience). For one, they muck up the visual indentation. Generally the more often an identifier is used, the shorter it should be. Give the full name as a comment near where it is first declared. If you forget what it means, then simply find the declaration. Good use of abbreviations makes code cleaner. True, it is an art-form that is hard to get right, but when done right, things sing along. Example:
double maxMonthUsage; // maximum primary energy usage for current monthIf there are other kinds of monthly usages, such as "secondary" in addition of primary, then here are some options:
double maxPriMonthUsage; // maximum primary energy usage for current monthOften such are just "slots" to the programmer anyhow. They don't really need to readily know what the difference is between a primary and secondary usage. They just want some kind of unique name to serve as slots to stick stuff. Note that I left "Max" in the name because calculating the max often is something the programmer has to deal with. From experience one knows that there is fairly likely to be a "curPEUCM" ("current") that is used for the summation and/or max-finding algorithm. Of course it is experience that teaches one what to abbreviate and what not to. (Even the experienced get it wrong sometimes. But the benefits of the times it is done right outweigh the losses from bad guessing.)double maxPEUCM; // maximum primary energy usage for current month
Not in my experience. If we can find programmers who can even spell correctly, then there is only ONE thing to search for: "maximum_primary_energy_usage_current_month". I elided the "for". If you only need the "maximum month usage" (and don't care about "primary", "energy" and "current", then call it "maximum_month_usage". If you are in an environment like PythonLanguage, JavaScript, or SmalltalkLanguage, then call them "maximumPrimaryEnergyUsageCurrentMonth" and "maximumMonthUsage".
The point is that when I search code, please don't make me guess at all the possible abbreviations some programmer someplace might come up with. If you do, please don't then argue that this is a good thing. In my opinion, it is not.
cf. "HttpReferer" (sic) property
This page mirrored in WikiPagesAboutRefactoring as of April 29, 2006