r/learnprogramming • u/qO_v_Op • 1d ago
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?
2
u/Rizzityrekt28 1d ago
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 1d ago
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
1
u/herrybaws 1d ago
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 1d ago
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 1d ago
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/Ormek_II 7h ago
While true, I do not see this as a perfect form of application. The loop starting at 1 might be considered “harder code” (as an extreme interpretation and because other readers might consider it wrong).
Following the professor’s rule super strictly I let the loop cover all elements (simple code) and add an if in the loop to skip the first element.
Not a good solution and taking a rule of thumb as a strict rule.
But I see in neither of OPs proposals to the program “harder work”.
1
u/StewedAngelSkins 1d ago
never. it's already obvious from the loop condition. the extra if is just confusing.
1
u/Ormek_II 7h ago
It is not obvious from the loop condition. The start at 1 could be a mistake. You might consider it obvious from the loop body.
1
u/Ormek_II 7h 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 1d ago
In your current implementation your for loop will not start at the correct element in the array
1
u/Ormek_II 7h ago
How would you fix it?
Yes, it does it starts at the second. If you think that the IF, a comment, or a test are indeed necessary.
-1
u/FunAd6672 1d ago
If your teammates cannot understand how a standard loop skips execution on short arrays then you have bigger team problems than code readability.
4
u/hellish_acceptance 1d ago
i'd skip it. the loop condition already handles the empty/single case cleanly, adding the if just adds noise for something that's already self-documenting