Web Development

General discussion


Variable names--can they be too short or too long?

By RayJeff ·
Everyone who's taken any type of programming class has had the obligatory lesson of creating variables. Going from chosing the type for a specific function all the down to the name being very descriptive of what the actual variable does. How many of us actually keep those lessons today, years after the class has been over, much less even being awake in class that day? It's almost two years since I've been programming professionally and those lesson come back to me almost everyday. But one thing I'm having second thoughts about is the way I name a variable. Specifically, I think I make the variable name too long.

The reason I believe my variable names are too long because for one, they are! Depending on what the variable holds, I make it long. It's usually the format of what the variable holds and where the variable is located. For example, If I were to have a variable in a Java applet that holds a SSN in an employee info form. the variable name might be "ssnEmployeeInfoForm" with EmployeeInfoForm" being a part of the actual whole name of the form. This can get very tedious over time. I've read over many texts and blogs and group posts, etc of "following established naming conventions" of a particular company, etc. This puts me at a great debate. I say this because of the current project I've been working on for the almost two years. I'm working in a division that had no IT person at all before me, muchless a programmer for me to be mentored by. Also, there is no one in the college (where I work) that has ever designed an in-house program/application for use. And the main part is there is no established naming convention. So, I'm basically using all of the academic knowing I have and applying it the best way I can.

My problem is considering my situation, are my variable names long enough or too long? Should variable names be as descriptive as they can be or just brief enough to be ok? I also remember the lessons about in the good ol old days of computing when variable names had to be very short because of writing a program with KBs of memory and only KBs of hard drive space. Totally different from today's world. So, I want to put this out there to you TechRepublic. I'd like to hear your opinions.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

I apologize for the "twice-ness"...

by RayJeff In reply to Variable names--can they ...

I apologize to all TechRepublic members who are upset by the number of times I have this thread posted.

Collapse -


by Tony Hopkinson In reply to Variable names--can they ...

they can certainly be too short as in procedure p and variable v2.
Too long I must admit I'm prone to that as I'm allergic to abbreviations unless they are standardised, documented and adhered to.
Even then confusion can still reign.
For instance is Inv_nbr Invoice_Number or Inventory_Number. Is a Nbr an integer type or a string identifier ?
You might say in the invoicing system Inv is invoice and in the stores system it's Inventory. But what is it when you are recording invoices for the purchase of inventory ?
The only time long names become annoying is in dotted notation
for instance but that's still way better than W.L.I.ID
The rule is simple, if in six months time when you go back to the code and you don't know what it means then you didn't do it right.

Collapse -


by jgarcia102066 In reply to Well

"The rule is simple, if in six months time when you go back to the code and you don't know what it means then you didn't do it right."

This is a great rule and if you work with a development team then the rule should be amended so that if any developer in your organization doesn't know what it means, then you didn't do it right.

Collapse -

Couldn't agree more

by Tony Hopkinson In reply to Addendum...

A lot of people fail on the readability front because they feel they are the only ones who will or should read it. The six months trick is a bit of management judo.

Collapse -

long names ok

by adam In reply to Variable names--can they ...

I prefer longer more descriptive variable names. I always think a variable should tell you what it holds and if appropriate the type of the variable. For example, to store a last name I would make the variable Lname or strLname or LastName rahter then calling it z. I find more descriptive names helps make the code more readable.

Collapse -

Too short is a killer

by JFercan In reply to long names ok

I worked with a guy who's variables where always letters of the alphabet and when he run out he started doubeling the letter for the names like a, b, c ... z, aa, bb, cc ... zz.

When he quite we had to rewrite all his modules becuase no one could understand what they were doing.

I think the typeName is a good convention and event if the name is long its better than spending hours trying to decipher three lines of code.

Just my two cents.

Related Discussions

Related Forums