4 ms·
Fever lines aren't automatically better, but fewer lines means less code to maintain. More often than not, fewer lines is easier to read too. The example here
by xorcist 6y ago
Fever lines aren't automatically better, but fewer lines means less code to maintain. More often than not, fewer lines is easier to read too.
The example here is a good one. The shorter version reads much better and is more obviously right.
- qualityonly 6y agoI have one to bring up "for loops". I think they are infinitely worse than while loops. (I ran into this specifically with Pandas dataframes/series) You save 1 or 2 lines and have ambiguity on wherever "each" is. Maybe static typing solves this, but I've decided I hate syntax sugar and dynamic typing.
- alisonkisk 6y agoYou might have a good point, but you haven't given enough code to expresss your idea. What is "each" ?
- cgriswald 6y agoAlthough for loops might all be while loops at heart, I think the distinction is more than just syntactic. Properly used, the use of a for loop conveys particular information: the code block is intended to run a set number of times or over a particular group of objects. This allows while loops in general to be used specifically for running the code block an indefinite number of times until the criteria is met. (Certainly both types of loops can be misused and abused.) It also splits apart two different kinds of criteria. "Has this run ten times" is a different sort of question than "is the error within the given margin" or "does the temperature now read 60 degrees". (At least, if you're intentionally running the thing 10 times. If it's running incidentally so you don't even know if it will run 10 times then it can be the same sort of question, but in that case I'd argue you should use a while loop….) > You save 1 or 2 lines and have ambiguity on wherever "each" is. Like the sibling poster, I'm not certain what you mean by this.
- larsrc 6y agoHaving been programming for almost forty years, in everything from machine code over C to functional languages, I do not find the shorter version "more obviously" right. It requires more commentary to explain here and depends on pointer manipulation above and beyond understanding the structure of the list and the actions of the algorithm. It may be easier if you think about pointer manipulation all the time, but I would consider such code a land mine, just waiting for someone to accidentally change something without enough understanding.