Skip to main content
56 votes

Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition

This is easy. Almost nothing matters more than clarity to the reader. The first variant I found incredibly simple and clear. The second 'improved' version, I had to read several times and make sure ...
Lewis Pringle's user avatar
19 votes
Accepted

Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition

I think for simple loops, such as these, the standard first syntax is much clearer. Some people consider multiple returns confusing or a code smell, but for a piece of code this small, I do not ...
Nathanael 's user avatar
14 votes

What's The Difference Between Imperative, Procedural and Structured Programming?

I am afraid none of the answers given so far capture the core of the concepts very well. Imperative, procedural and structured are not mutually exclusive properties, they just focus on different ...
Martin Maat's user avatar
  • 18.6k
9 votes

Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition

int i = 0; while (i < array.length && array[i] != value) i++; return i < array.length; […] everything is more obvious and more in a structured-programming way. Not quite. The ...
null's user avatar
  • 3,767
8 votes

Is there a good reason not to use a "super class" (an all-encompassing class)

there is only one SPI bus, there will only ever be one SPI bus ... he still made an abstract SPI bus class that he overrides with... the real SPI bus class. It makes zero sense to me - CHollman82 And ...
candied_orange's user avatar
5 votes
Accepted

Proper program structuring in Python

Refactoring Lots of people have written tomes and tomes on the subject. Martin Fowler has some good advice. Highly recommend looking through his work, also take a look at Kevlin Henney. Sprout and ...
Kain0_0's user avatar
  • 16.6k
2 votes

Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition

The two loops have different semantics: The first loop simply answers a simple yes/no question: "Does the array contain the object I'm looking for?" It does so in the most brief manner possible. The ...
cmaster - reinstate monica's user avatar
2 votes

Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition

I'll suggest a third option altogether: return array.find(value); There are many different reasons to iterate over an array: Check if a specific value exists, transform the array into another array, ...
Kevin's user avatar
  • 731

Only top scored, non community-wiki answers of a minimum length are eligible