5 ms·
Clean code is code you and others can understand and everybody seems to have a different idea of that. Sentence long variable names I don't like, they slow me
by samsquire 4y ago
Clean code is code you and others can understand and everybody seems to have a different idea of that.
Sentence long variable names I don't like, they slow me down when thinking about or remembering what to do I'm not sure you can remember the names without Intellisense/IntelliJ autocomplete.
I don't think I would enjoy working on this codebase.
- zelphirkalt 4y agoNaming things is hard. Often when names are very long, they could be shortened, because they are in a function, which already narrows down the context and makes a shorter name still understandable. Very long names can also sometimes be a code smell, if they are relating to a lot of global state, which is not narrowed down in context by where it is defined. Sometimes however, it is really difficult to come up with a shorter name. I think it is definitely a skill, that takes practice and never is really perfected.
- lukeramsden 4y agoThe point is to always name your variables / methods / classes as exactly what they do (not how they do it, but what they achieve). If this name looks ridiculous, like `doXAndYAndZAndSideEffect`, then your code is likely poorly factored. I've found it works very well for helping to find good separation of concerns.
- thraxil 4y agoThe 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.
- ebiester 4y agoI think I get frustrated here because "Clean Code" is talking about a very specific style of coding popularized by Robert Martin in the book with the same name. It is not a synonym for Good Code. Longer variable names are certainly a facet of Clean Code. It's in chapter 2, and Chapter 1 is an introduction. That said, I strongly suggest Arlo Belshee's series https://www.digdeeproots.com/articles/naming-as-a-process/ https://www.digdeeproots.com/articles/naming-as-a-process/ - this gets to the heart of how to have both descriptive and not-sentence-long.
- skybrian 4y agoI think the names in the article were pretty decent, but here is good article about common errors that result in unnecessarily long names: https://journal.stuffwithstuff.com/2016/06/16/long-names-are-long/ https://journal.stuffwithstuff.com/2016/06/16/long-names-are...