views:

500

answers:

18

I always wondered: Are there any hard facts which would indicate that either shorter or longer identifiers are better?

Example:

clrscr()

opposed to

ClearScreen()

Short identifiers should be faster to read because there are fewer characters but longer identifiers often better resemble natural language and therefore also should be faster to read.

Are there other aspects which suggest either a short or a verbose style?

EDIT: Just to clarify: I didn't ask: "What would you do in this case?". I asked for reasons to prefer one over the other, i.e. this is not a poll question.

Please, if you can, add some reason on why one would prefer one style over the other.

+2  A: 

It's really all about finding a balance between the two, that is easy enough to read while at the same time not overly verbose. Many people have a personal dislike for Java or Win32's elaborate function/class names, but many others dislike very short identifiers as well.

Amber
I don't know about Win32, I think that the problem with Java's names isn't that the names are bad: the names are actually perfect for what they describe. The problem is that while UnAntialiasedPageFactoryFactoryFactory may be a perfect name for an object, such an object should not exist. An accurate, clear name is always good; when a name gets that long it's a problem with the design, not the name.
Imagist
+21  A: 

I'd go for clarity over verbosity or brevit.

ClearScreen() 

is easier to parse than

clrscr() 

in my opinion, but

ClearScreenBeforeRerenderingPage() 

is just noise.

Rich Seller
More importantly ClearScreenBeforeRenderingPage() implies WHEN it should be called which is never good.
Stimul8d
Actually I was implying that the method did the rendering, but that's just as bad. Both are a smell that you need another method
Rich Seller
It's worth noting that C functions like clrscr() and malloc() are so-named because there originally was a six-character limit on names. Were the C standard written anew today, I doubt that they would not be named clearScreen() and memoryAllocate().
Imagist
A: 

Nothing wrong with a short identifier as long as its obvious what it means.

Personally I'd lean toward the latter because i prefer to be verbose (Though i try not to be so verbose as MS and their CoMarshallInterthreadInterfaceInStream function) but as long as your Clear Screen function is not called "F()" I don't see a problem :)

Goz
+9  A: 

Certainly .NET developers should be following the .NET Naming Guidelines.

This suggests that the full names should be used, not abbreviations:

Do not use abbreviations or contractions as parts of identifier names. For example, use GetWindow instead of GetWin.

tomfanning
+5  A: 

My personal preference is to have verbose public identifiers and short private ones.

By public I mean class names, method names, global variables and constants, packages, namespaces - in short anything that can be accessed from a large number of places and by large number of people.

By private I mean local variables, private members, sometimes parameters - stuff that is only accessed from inside limited local scope and by single developer only.

Gregory Mostizky
Private variables, etc, might only accessed by the original author today, but what about tomorrow? If some names need to be clear and verbose, surely they all do?
Steve Melnikoff
I think you misunderstood my point. If it the variable is going to be accessed from more than one point, sure go ahead and name it properly. On the other hand usually "for (int i=0;;)" is perfectly valid and more readable than "for (int myIterator=0;;)". Anyway these are guidelines and not strict rules - just do what makes sense.
Gregory Mostizky
+4  A: 

Every developer should know how to touch type. Adding a few extra characters is not a productivity issue unless you're a hunt and peck typist.

With that said, I agree with the previous comments about balance. As with so many answers here, "it depends". But I favor verbosity and clarity. Taking out vowels is for old DBAs.

duffymo
+1  A: 

Naming conventions and coding style are often discussed topics.
That said, the naming conventions are always very subjective -- to people and platform.

Bottom line is always -- let things make sense (yes, very subjective).
Wikibook search -- Naming identifiers

nik
A: 

Also one has to think of where the identifier is, the well known i as iteration counter is a valid example:

for(int i=0;i<10;i++){
    doSomething();
}

If the context is simple the identifier should reflect this accordingly.

Alberto Zaccagni
ii is even better in case you ever want to search for your iterators
fupsduck
+6  A: 

Personally I like to try a follow the wisdom of Clean Code by Uncle Bob.

To me it suggests that Code should read like prose. By using descriptive names and ensuring that methods have a single responsibility we can write code that accurately describes the programmers intent obviating the need for comments (in most cases).

He goes on to make the observation that when we write code, we often spend 90% of the time reading the surrounding code and dependent code. Therefore by writing code that is inherently readable we can be far more productive in our writing of code.

teabot
+5  A: 

My basic rule is that every line of code is written only once, but read 10, 100, or 1000 times. According to this, the effort of typing is totally irrelevant. All that counts is the effort to read something.

Of course, this alone still leaves enough room for subjective opinions (is clrscrn readable enough?), but at least is narrows the field somehow.

sbi
+2  A: 

Most modern IDEs (and even older ones) have an auto-complete feature, so it doesn't really take more time to type a long identifier (once it is declared of course). So I'd go for clarity over brevity, it makes the program much easier to read and more self-explaining

Thomas Levesque
Sadly, it's STILL not that common in embedded compilers :-(
Steve Melnikoff
+6  A: 

Please go directly to the relevant chapter of Steve McConnell's "Code Complete" (sanitised Amazon link).

Specifically, chapter 11, "The Power of Variable Names".

Rob Wells
I second Rob's suggestion. Steve McConnell gives detailed info on many aspected of naming. In my experience, the most important thing is that everyone working on the project buys-in to the same strategy.
JBRWilkinson
@JBRWilkinson, yes good point! When I wrote the C++ Coding Standard for a major company (over 200,000 people worldwide), I came up with reasons for the choices rather than a series of "Thou shalt..." pronouncements. Lots of people said they appreciated the background info and sometimes the explanations led to improvements to the standards or explanations.
Rob Wells
+3  A: 

As a programmer I do much more thinking while programming than typing. So the extra time typing a longer identifier is of no relevance. And today my IDE is supporting me, I now have only to type some letters and the IDE let me choose from legal identifiers. So the productivity-argument against longer identifiers is today more invalid than it was a few years ago.

On the other side you gain readability, if you choose more meaningful identifiers. Because you will read source-code more often than writing it, this is very important. Another point is, that abbreviations often are ambiguous. So do you abreviate number as no or as num? That makes errors more probable, as you choose the wrong identifier.

Mnementh
+4  A: 

Always using full words in identifiers also helps to maintain consistency.

With abbreviations there is always the question whether the word was abbreviated, and if yes how.

For example, right now I'm looking at code which has index abbreviated as ndx or idx in various places.

For very local context it is OK to abbreviate, but then I'd use only the first letter of each word to guarantee consistency. E.g. sb for StringBuilder.

starblue
oh yes, there's also the absolutely horrible dsc/desc/descr/descript/description naming. Writing it out would not have hurt anyone...
Matthias Kestenholz
+2  A: 

I think you'll find precious few hard facts, but lot of opinion on this subject.

The Wikipedia page on this topic links to a paper on a cost/benefit analysis of identifier naming issues (External Links section), but no language I know of bases its official or accepted naming conventions on the basis of a "scientific" study.

Looking at code in a social context, you should follow the naming conventions imposed by:

  1. The project
  2. The company
  3. The programming language

.. in that order.

nagul
+4  A: 

I remember a talk from Larry Wall some time ago where he talked about the verbosity of identifiers when you have non-native English speakers in your team.

ClearScreenBeforeRerenderingPage()

parses fine if you're a native English speaker. However he suggests (and experience shows) that:

Clear_Screen_Before_Rerendering_Page()

is much better.

The effect is exacerbated when you don't use both upper and lower case.

David Lawrence Miller
+1. Excellent point, not likely to be terribly obvious to native English speakers.
nagul
I like the idea but unfortunately I think it will never become a standard because the underscore key is one of the most painful key to access on a keyboard. The left shift key puts a lot of strain on the wrist and the underscore is too close to the right shift to make it comfortable.
+4  A: 

Also consider where it's going to be used, ClearScreen() is likely to appear on its own.
However you cringe when new programmers who have learned that the identifier must be easily readable, produce code like.

 screenCoordinateVertical = gradientOfLine * screenCoordinateHoriontal + screenCoordinateOrigin;

instead of

 y = m*x + C;
Martin Beckett
Actually, I cringe at y=m*x+CIt's obfuscated for someone without math graphing background.
+12  A: 

Abbreviations put a much greater burden on the reader. They are ambiguous; they are indirect; they are harder to discriminate. They burden the writer, too, for s/he must always be asking, "was that Cmd for Command, or Cmnd... or Cm?". They clash - a given abbreviation rule could produce the same abbreviation for two (or more) different words.

Because they are ambiguous, the reader must take time to think about what word is intended; if the word itself is present, the reader need only think about its meaning.

Because they are indirect, they are analogous to a pointer - just as there's a little processing time consumed by every pointer dereference, there's a little (human) processing time consumed, and additional memory occupied, by every abbreviation.

Carl Manaster
I've accepted this answer because it was the top-voted one which at least provided some reasoning instead of just an opinion.
DR
Totally agreed. Weird abbreviations screw things over, just like in PHP. Can anyone really read "strstr" out loud and get meaning from it?
Arve Systad