r/learnprogramming • u/qO_v_Op • Oct 27 '26
Would you write if-else in this example deliberately even if the for loop can handle it actually?
For example, there is a for loop to list the difference of scores at each time (in JavaScript):
for(let i = 1 ; i < scores.length ; i++){
console.log("Score changes: " + (scores[i] - scores[i - 1]));
}If there is no score or only have one score, the loop would not run, which meets the current requirement.
I'm curious if you would prefer add the if-else deliberately like the following:
if(scores.length >= 2){
for(let i = 1 ; i < scores.length ; i++){
console.log("Score changes: " + (scores[i] - scores[i - 1]));
}
}in order to remind yourself or other teammates that this loop is designed for scores with 2 or more elements only (especially for maintain and requirement changes). Is it necessary?
7
Upvotes
2
u/snozzd Oct 27 '26
In the general case, the convention is to omit the condition and just use the for loop. IMO It's better to write nothing than something that isn't necessary. You can make what's there more readable in other ways, it all depends on the larger context though.
2
u/Rizzityrekt28 Oct 27 '26
You could leave a comment with // to make sure you remember and your teammates understand. The if statement would be redundant code which is…. Frowned upon
2
u/DrDesperate Oct 27 '26
if you really want the intent written down, a tiny test with an empty array and a one item array does it better than an if. it documents the case and actually fails if someone changes the loop later, while the if just sits there.
1
u/herrybaws Oct 27 '26
Depends what's around it, is a silent skip over ok? Or would it be useful to know that the array was too small and then log that/inform user. If you don't need it, the plain loop is fine, otherwise the if would be useful.
1
u/horangiwhakkatchi Oct 27 '26
No definitely wouldn't write the if statement. Simpler code is better code. Everyone understands the intention based on the for condition.
1
u/SpicyNugget123 Oct 27 '26
My data structures professor stated this, and I will never forget it, "Be a lazy programmer. Let the program do the hard work". I mean, the simpler the code, the easier other programmers, even yourself will understand it down the road.
1
u/skawid Oct 27 '26
Presumably the program should do something else if there aren't enough records right? As long as *that* path is clear from earlier code, there's no need for the if.
1
u/StewedAngelSkins Oct 27 '26
never. it's already obvious from the loop condition. the extra
ifis just confusing.1
u/Ormek_II 29d ago
I would add a comment instead to document that the start at 1 is by Intention and not by mistake.
I see this as a perfect example why I think comments make sense :)
Why is the comment better than the code: it can be more elaborate in the why. It does not cost runtime (which is not a good reason, because maintainability is more important than unnecessary optimisation). Also the if statement might be removed by the next reader, because he does not see the possible misinterpretation.
-1
u/TheStonedEdge Oct 27 '26
In your current implementation your for loop will not start at the correct element in the array