r/learnprogramming • • 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?

5 Upvotes

20 comments sorted by

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

2

u/snozzd 1d ago

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 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

u/Ormek_II 7h ago

Very true, if the team considers test as specification and runs them.

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/skawid 1d ago

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 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.