How long is too long for a variable name?

bad variable names python
long variable names
good variable names python
descriptive variable names
variable naming best practices
python variable name generator
variable names examples
most common variable names

I suppose, I'm looking for both answers that are both technical and opinionated in nature.

How long is too long for a variable name?

I always try to make my variables as short as they can be while maintaining the proper level of meaning. However, as some "sub-routines" increase in complexity, I'm finding the need to create longer variable names to continue to have them be meaningful. Does the length of a variable have an impact on performance?

For instance, I just used MBReadPerSecondAverageAfterLastFlushToLog, which is as close to supercalifragilisticexpialidocious as I hope I ever come.

Thanks,

Matt

p.s. Sorry if you now have a Mary Poppins song in your head.

You are correct in naming variables as short as you can while retaining enough meaning to be able to describe what the variable does by just looking at its name.

No, the length of a variable name has absolutely nothing to do with performance.

Edit:

For some reason I thought you were talking about C++. If you are (or C or Delphi or another compiled language) the above is correct (barring debug information which won't appear in a release executable).

For dynamic languages such as Lua or Python or Ruby, the length of a variable name could very well affect runtime performance depending on how variable name lookups are performed. If variable names are hashed and then the hash is used to index a table of values to get the value of the variable, then natrually the more data the hash function has to process, the longer it will take.

That said, do not sacrifice meaningful variable names for short ones just because you think they'll be faster. The speed increase will usually be extremely negligible, and definitely not worth sacrificing the maintainability of your program for.

How long is too long for a variable name?, 3 Answers. You are correct in naming variables as short as you can while retaining enough meaning to be able to describe what the variable does by just looking at its name. No, the length of a variable name has absolutely nothing to do with performance. Environment variable name or value is too long which for a regular occurring script will eventually cause the environment variable to be too long.

Make it as long as it takes to make it clear. But, in your example, it looks like you may have some other issues.

  1. The name of the Variable indicates to me that you have a function that has more than one responsiblity.

  2. Isn't this what objects are for? Imagine an object called Reads, this is just a really simple example, that may/may not in anyway match what you need.

      Reads.AveragePerSecond  
      Reads.Size  
      Reads.IsLoggedFlushed
    

Are my method or variable names too long?, Your names seem okay to me in terms of length. However, the way they are named suggests that maybe some new classes are in order? For example, instead� The variables editor shows the variable properly (HEM_Uipp_DeviceCheck has value 0) but in the preflight check (in log), it gets translated to True for no reason. I simply cut "HEM_" from variable name and magically it started working. So either there is some unknown character limit in variables, or adding second underscore _ character breaks it.

I think the main problem here that you forgot that variables are not some sort of global unique identifiers. They have a context, which helps explaining them. By reading them you normally know that context: namespace, class name, method name, the meaning of the previous lines, method name by giving value to the variable or in your example some basic scientific knowledge like MBps is a unit of data transfer speed, speed is always measured by average, etc... The MBReadPerSecondAverageAfterLastFlushToLog can easily be just Speed if the context explains the rest...

To make it easier to understand if you want a beer than you just say to your girlfrend that Give me a beer please! not that Give me an 5°C Gösser beer from the fridge I bought yesterday in the shop for 1$ please!

What kind of overhead does introducing an object do? Is not declaring a variable much less?

This might be premature optimization. First you write the code, after that should you look for bottlenecks...

I already have a "ReadFiles" method where this like MBReadPerSecondAfterFlushToLog, KBReadPerSecondAverageAfterLastFlushToLog, MBReadPerSecondAveragePerReadOperation, etc etc etc are located.

These might be ReadSpeadAfterFlushToLog if the first 2 variables are about the same thing and you should have a DataTransferSpeed class, which does the unit conversion for you when needed. The third variable can be AverageSpeedPerReadOperation. If we have only reading speeds here and the context describes it, e.g. the method name contains that info, then even the Read part can be omitted. If you have way too many variables, then you might need multiple smaller methods and you might want to group the variables you use with structs and objects.

Is anybody else using ridiculously long variable names?, However, if a method/function is too long (i.e. too many lines in that single method/function) then it is very likely to have longer variable names� A variable name is the wrong length when it is not clearly describing it's purpose. That applies to being too short or too long. Short and terse variable names can be ambiguous, or worse have no relationship to what the variable is. In the days of interpreted BASIC, we were limited by the interpreter to two character names.

Python Code Style, Use descriptive names for variables, functions, classes. You don't want to make function and variable names too long, but they should be descriptive (e.g. use� The function name is too long when the programmer feels it is too long. None of us can tell you if you, the programmer working on the code, feel that the name is too long. – jalf Jun 19 '09 at 12:39

Is it bad to use long variable names?, While this variable's name is very helpful being so descriptive, I'm let some lines and expressions too long - and long lines or expressions are� I need help choosing proper names for variables with long actual names. I have read pep8 docs, but I couln't find addressed such issue. Would you rename very_long_variable_name to something like vry_lng_var_nm or would you leave it as it is. I notice that pythons build in libraries have very short names and I'd like to follow the conventions if

The Case Against Descriptive Variable Names, People believe that long descriptive variable names and function names I think people use abbreviations and acronyms far too frequently. Sets the maximum capacity of each variable to the specified number of megabytes. If this directive is unspecified in the script, it will behave as though set to 64. This setting restricts only the automatic expansion that a variable does on its own.

Comments
  • What language(s) are you developing in? That may have bearing on the answer.
  • Depends on the language. If you're programming in TI-Basic, then 2 characters is too long, as it only supports 1 character variable names.
  • Don't hate the player, hate the game.
  • I agree with your naming style. Something like dMBXlgS is what maniacal programmers do to fool people into thinking they are smart just because nobody else knows what they mean.
  • Actually, the length of a variable name could have something to do with performance (even if never a practical concern), depending upon language and implementation. But still +1.
  • It would never have an impact on runtime performance in a compiled language. But in a just-in-time compiled language, it could slowdown program startup and program performance. Just to expand upon your answer :)
  • If your debugger can show the actual variable names, they're being moved around in memory and probably have some impact. Of course, you probably couldn't reasonably measure this impact in microseconds over the life of a program, but at the CPU cycle level it's likely to be nonzero.
  • Thanks Doug. What kind of overhead does introducing an object do? Is not declaring a variable much less? I already have a "ReadFiles" method where this like MBReadPerSecondAfterFlushToLog, KBReadPerSecondAverageAfterLastFlushToLog, MBReadPerSecondAveragePerReadOperation, etc etc etc are located.