Code Style Terror I — Big Names Can Kill Your Network!
You need to write efficient code. Your page load has strict requirements since each 100ms it takes you lose 1% of your sales. What should you do?
̶U̶s̶e̶ ̶l̶e̶t̶t̶e̶r̶s̶ ̶a̶s̶ ̶v̶a̶r̶i̶a̶b̶l̶e̶ ̶n̶a̶m̶e̶s̶!̶!̶!
No! Minimize your code!
There may have been a time in which variable and commands length took a very important role. Terminal commands needed to be small since terminals were slow. Small commands were crucial for the typist’s productivity.
But in the 21st century, it is awkward that we still have to discuss the importance of variable naming with programmers. Even if you lose some productivity because you need to type more, we spend most of the time reading code, not writing it, so it probably won’t be a bottleneck.
Besides, if you use an IDE, or almost any worthy code editor, you will have any kind of auto completion, so your 30 character variables only need to be written once! Do you think any Java developer has to write EnterpriseGradeFizzBuzzSolutionStrategyFactory more than once? Even if you are using a dynamically-typed language, using a code editor such as Atom will give you pretty good suggestions of existing symbols.
But, But, The Network…
When developing web-applications in a large scale, every byte counts. If you are worried about the size of your code files and their delivery to the browsers, I am glad to inform you that this has been solved years ago! Just minimize your code with any of the tools that exist for that! These tools transform all your variable and function names into small symbols like ‘a’ and ‘b’, remove all the spaces, all the line terminators, etc. Some tools, such as Google’s Closure Compiler, will even improve your code performance and remove non-used code!
If you use angular, you may still be worried about your never minified controller names, but I do believe those will not be a bottleneck.
Pull Requests are trending!
When you analyse code, the “unit” of code you need to read depends on your current context. If you are coding, you are probably reading a class or a function.
But if you are searching for a recent bug and need to check the latest changes, or you are reviewing the work the team did during your vacation, what you are reading is probably a commit, so the unit of code is lesser than a method, is just a diff in some file and a commit comment message.
The “cool” thing about diffs is the leak of context. For instance, in GitHub, unless you click to see the whole file, you probably will not see the function name if the changes were performed in the middle of a block.
The role of the variable name here isn’t even to improve code reading. If you are just skimming through recent commits, as I do in some third party projects, the variable names are a big hint to check if the commit is worth reading.
Some Recent Cases
st = len(contextPath)
for i in range(st, len(path)):
#do the stuff
This sample was a bit more complicated than the part I am showing here. Maybe I was in a bad day, but I really struggled to understand what the st variable meant. It’s easy to know from what is written that it is the size of the context path, but what does it mean in the context of the algorithm?
When I realized it meant “start” it made a big difference to understand the whole algorithm.
var a = $(“someCSSSelector”);
I spent some time thinking why would somebody name a to a variable. I don’t know if this variable name is a front-end standard, but I really think that linkForSomething would be more readable than a. (It was a as in <a>)
Historically Acceptable Cases
Of course there are some classic small variable names that are socially acceptable.
In any programming language where for loops exist, we have i, j, k variables. But even those should be carefully used. Having an i variable in a 2 line for block is ok, but if your block grows (as it shouldn’t), during a pull request the reader may not notice that the variable is inside a loop block, so it will be a meaningless name.
For all the generic fans, at least in Java, we have the T, U, V variables. Sometimes I feel that real names like TypeOfObject would be preferable. Generics based code is usually complex and written by advanced programmers. Having to guess what those type variables mean can be hard, specially if you have multiple generic types at the same time. In the middle of the code it may be hard to remember what the variables T and U stand for.
What About You?
Do you have any variable name that you are still trying to figure out?
pls com || hrt ths stry (Please comment or heart this story) :)