3 ms·
Disagree strongly with the comment about naming : "Names of variables and functions should be distinct and provide a general idea of what they do". The cleares
by enriquto 8y ago
Disagree strongly with the comment about naming : "Names of variables and functions should be distinct and provide a general idea of what they do".
The clearest code that I have read used consistently single-letter variable names. If a variable name holds a meaning, you are then repeating this information each time you use the variable. It is better to name the variable "a", and put a comment upon its declaration. This is the convention used in mathematics and physics, and it works really well.
- ab71e5 8y agoMaybe some of them, like in tight loops. But the most difficult code to understand for me is Matlab scripts written by physicists, naming variables a,b,c or i,ii,iii etc. Variables with longer lifespan should have more explicit names in my opinion.
- enriquto 8y agoOf course! I am talking about the names of local variables, not the names of externally visible functions or global variables.
- alecbenzer 8y agoThere's a spectrum of how 'local' a variable is though. There's a big difference between a local variable whose uses span ~5 lines vs. ~30 lines. You're making a trade-off between repeating information (what the variable is) and forcing the reader to keep in their own (human) memory what each variable is for (or else forcing them to constantly go back and re-discover what the variable is for). Go sometimes gets a lot of flak for its style here (partially because it's sometimes misapplied) but I think it works pretty well: it's fine to use a short non-descript variable name like `a` if its uses only span a few (~5-10) lines (or other certain special cases like `i`, `j` for loop indexes), otherwise use longer descriptive names.
- enriquto 8y agoThis sounds more a problem about your total number of local variables than their names. If you have too many variables, using longer names is not going to help you as much as organizing your code differently.
- crazygringo 8y agoIt's the most elegant code, sure. But it also creates more work for me to read it, because I need to build up a mental map memorizing {letter => meaning, letter => meaning, ...}. If you create a meaningful name, then that means I can understand the code more quickly with less effort, and save that mental effort for more productive tasks. If you're programming professionally, why would you prefer greater elegance and conciseness... over less work? It feels like a harmful over-optimization.
- enriquto 8y agoYou do not need to memorize anything if your function fits in a single screen and you have less than 10 variables at the same time (which are good conventions as well).
- sethammons 8y agoRemembering 10 single letter mappings vs clear variable names sounds onerous.a, b, c, d, e, f, g, h, i, and j vs price, discount, coupon, item_description, item_id, ...
- enriquto 8y ago> Remembering 10 single letter mappings vs clear variable names sounds onerous Have you ever read an applied math book? They do not seem to have this problem. My point is that int d; // dimension_of_the_space is much better than int dimension_of_the_space; especially if the quantity appears inside many formulas.
- crazygringo 8y ago> Have you ever read an applied math book? They do not seem to have this problem. Working through an applied math book takes an entire semester with many, many hours of study, and you're expected to master the material inside out. Programmers generally do not have anything like the luxury of that kind of time, nor are they expected to reach that level of mastery with the codebase -- they need to quickly identify where changes need to be made, make them, ensure test coverage continues to be complete and passes, and move on to the next thing.