10

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.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329

19 Answers19

20

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
  • 83,208
  • 23
  • 172
  • 177
  • 2
    More importantly ClearScreenBeforeRenderingPage() implies WHEN it should be called which is never good. – EightyOne Unite Aug 05 '09 at 10:50
  • 1
    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 Aug 05 '09 at 11:07
  • 6
    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 Aug 05 '09 at 15:04
  • I have to disagree with the "just noise" part. If a method is named like that there should be an important reason. Obviously, there would exist a "ClearScreenAfterRerenderingPage" In this case, that verbosity is extremely important. – Harry Mar 27 '23 at 12:48
14

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
  • 39,912
  • 17
  • 102
  • 155
  • I've accepted this answer because it was the top-voted one which at least provided some reasoning instead of just an opinion. – Daniel Rikowski Aug 24 '09 at 06:47
  • 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 May 29 '10 at 14:11
  • I think this answer is too prescriptive. As mentioned by many others, there is scope for selected abbreviations in a common-sense approach. This is true, especially, when those abbreviations are well-known to the average programmer in that business domain or programming language. – Cornel Masson Mar 30 '11 at 09:45
11

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
  • 9,552
  • 4
  • 50
  • 78
9

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
  • 15,358
  • 11
  • 64
  • 79
8

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,801
  • 11
  • 12
  • 1
    +1. Excellent point, not likely to be terribly obvious to native English speakers. – nagul Aug 07 '09 at 18:06
  • 1
    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. – user275587 May 29 '10 at 07:50
7

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
  • 219,715
  • 46
  • 258
  • 445
7

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
  • 1
    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 Aug 05 '09 at 09:47
  • 1
    @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 Aug 05 '09 at 10:31
5

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
  • 7,231
  • 1
  • 26
  • 29
  • 1
    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 Aug 05 '09 at 10:37
  • 1
    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 Aug 05 '09 at 11:04
5

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;
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
4

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
  • 305,152
  • 44
  • 369
  • 561
4

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. Since 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 abbreviate number as no, or as num? That makes errors more probable, as you choose the wrong identifier.

Porter
  • 23
  • 6
Mnementh
  • 50,487
  • 48
  • 148
  • 202
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
starblue
  • 55,348
  • 14
  • 97
  • 151
3

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
  • 2,243
  • 2
  • 19
  • 21
2

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
  • 507,862
  • 82
  • 626
  • 550
  • 5
    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 Aug 05 '09 at 15:10
2

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
  • 286,951
  • 70
  • 623
  • 758
1

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
  • 61,365
  • 24
  • 124
  • 204
1

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
  • 13,254
  • 3
  • 41
  • 57
0

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
  • 30,779
  • 11
  • 72
  • 106
0

No one has mentioned the negative impact on readability of identifiers that are too long. Once you start making identifiers that are 20, 30, 40 or more characters long you cannot write a reasonable statement on one line of text that is readable. Lines of code should be limited to about 80 characters. Anything longer is impossible to read. That is why newspapers are printed in columns. Even this webpage keeps the text column narrow so that it can be read without having to scan back and forth.

Tom
  • 47,574
  • 2
  • 16
  • 29