4 ms·
The recommendation I read back in the 90's in Code Complete that has served me well ever since was that variable name length should be roughly proportional to t
by thraxil 4y ago
The recommendation I read back in the 90's in Code Complete that has served me well ever since was that variable name length should be roughly proportional to the variable span. "Variable span" being the distance between where the variable is first defined and its last use. Generally that means that a temp variable inside a short function can be a single letter since you can see the full context of its entire lifecycle right there and it's unlikely to be confusing; indeed, a short variable name in that case will often be much easier to read and work with than a longer one. OTOH, a global or exported variable that might be accessed in different files from where it is defined or used across thousands of lines really needs to be descriptive enough that anyone encountering it should know what it's for without having to backtrack to where it was defined. A variable name that's a few words long might be entirely appropriate for that. A corollary is that a large variable span is often an indicator of a bad design. So if you find that you need really long variable names to make your code easy to understand, you might want to step back and see if you could set up some better or different boundaries.
I've found this to be a much better rule of thumb than "always use long variable names" or "always use short variable names".
- mgkimsal 4y agoThank you. I'd read CC ... 15+ years ago, but couldn't remember this specific step (but I know I've internalized it). I had criticism from folks a couple years back because I used "x" and "i" as variable names in loop counting - for (x=0; x<limit; x++) - that sort of thing. I had folks block my PR because "don't use short variables - that's unprofessional". These were loops inside a test file, and, at that time (8 months after launch), I was committing the first test to the project. It was blocked while I had to think of something better, like for(loop=0; loop<limit; loop++) - which was deemed "acceptable". This concept from CC was at the back of my mind, but couldn't articulate it, or even remember where I'd first read about it, but it's been something I've worked at adopting and growing over the years. 6 line loops - x/i/j/k/etc are all valid and usable. In some ways, it's actually more helpful to me, because the longer names indicate something concrete/real, and the single letter names indicate local/throwaway values.
- samsquire 4y agoFor Java and Python for each loops I often use "current" or a singular name for the thing being iterated. If it's a nested loop then I use "other". For C and JavaScript I use single letter indexes ijkl or "current". I think it's a personal preference. I don't think there is a wrong way or a right way. Your team needs to agree though. I would be annoyed if I implemented a deep feature and the PR was blocked due to something as inane and useless as this.
- DanHulton 4y agoI don't mind "i", but if there's an inner loop with "j" as well, you are BEGGING for confusion.
- PeterisP 4y agoOften "i" and "j" would appear when the code is the implementation of some math formula which would idiomatically use "i" and "j" when written 'properly' on paper or LaTeX.
- samsquire 4y agoI got down to "k" too. What about "x" and "y"? One important consideration of nested loops is row major access pattern rather than column major pattern, so access array[i][j] rather than array[j][i] due to the data locality of each. You shall get a performance boost by accessing them in that order. I worked on parallelizing nested loops across OS threads and load balancing between outer loops for different progressions through the loop. I also wrote some code to reverse the loop direction to avoid a cache miss when the indexes go back to 0. https://github.com/samsquire/multiversion-concurrency-control#concurrent-loop-concurrentloopjava-and-tickpy https://github.com/samsquire/multiversion-concurrency-contro...
- DanHulton 4y ago`x` and `y` are also problematic, though slightly less so. The biggest issue I have is that they're similar in appearance. `abc` is much better, because they're clearly different from each other, if you're married to single-letter values. `i` and `j` are usually indicative an index for something, and indicating what you're indexing makes the code clearer to read and easier to maintain. `idxUsers`, `idxPosts`, for example. `x` and `y` are a bit trickier because they're very frequently the canonical positional co-ordinates, and renaming them can actually make it _harder_ to read, so (like all things in naming) is a real judgment call, but frequently can be replaced with `col` and `row`, which, to me reads _much_ clearer, especially when using row-major access pattern: `array[row][col]`.
- pjmlp 4y agoKudos to Code Complete, specially l the tips regarding "safe" C code and parameter validation, based on Microsoft learnings in failure to do so. One anecdote I remember from the book, is how for a while Excel allowed for dual colored cells based on a parsing error due to trying to be too clever.
- FractalHQ 4y agoI like this, I adopted this intuitively over time. But man do I loathe single variable names. My brain starts to shut down when there’s more than one of those anywhere. I write a lot of GLSL and that crowd loves working in wingings.