Web Development

General discussion


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

By RayJeff ·
People: ghost_75_24@...
Variable names--can they be too short or too long?
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 less than 1 Meg 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 -

you actually hit it on the head there

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

if you are coding for an embedded application, then you have size constraines that have to be taken into concideration even today. ( cell phones, palm computers )

yet, the name of a variable isn't really a size issue.
most compilers only use the first few characters for the name when they compile.

with interpreted languages like java, it's more of an issue, so you have to look at the target hardware.

yes, they can be to short.
far to short.

grab yourself a copy of the linux kernel, and open one of the larger c or c++ files.

single letter variable names. ( not just a loop variable either )
no indication of what it's for. ( bad commenting )
so it is better, when not space constrained with interpreted language to have the longer, descriptive variable name.

Collapse -

Java isn't Interpreted

by lance.dolan In reply to you actually hit it on th ...

Unless I'm mistaken and we're talking JavaScript, Java is not interpreted. It's compiled into bytecode, and how a JVM chooses to manage and represent variable names in RAM within itself is JVM dependent I believe...

But yes, you are correct that at compile time, the entire variable name is not used, and so there is no advantage of shorter variable names unless you're really worried about the size of your SOURCE, not your application.

Thus, I use very long variable names all the time, in pursuance of maintainability, since there is no real disadvantage to a long variable name.

That being said - don't go off with a variable name so long that it actually makes its purpose LESS clear. For example, waterTemperature is better than waterTemperatureInCelciusWhileBeingManipulatedByTheUser
or whatever.

Related Discussions

Related Forums