4 ms·
I tentatively disagree with both of you. I've sometimes wondered whether code would be universally clearer if all you could do in a loop is have a one-liner or
by lesserknowndan 7y ago
I tentatively disagree with both of you.
I've sometimes wondered whether code would be universally clearer if all you could do in a loop is have a one-liner or call another function/method (mainly when looking at my own code within loops and thinking WTF!!!).
Would probably cause issues when teaching programming though. It would be interesting to have a compiler switch that enforced this...
- baddox 7y agoWhat comes to mind immediately is that you would lose the ability to `break` unless you had another convention like returning a specific value to indicate you want to break.
- lesserknowndan 7y agoIf you are limited to one statement you could still do something along the lines of the following (no particular language): foreach ( $item in $list ) { if ( 1 == some_function( $item ) ) { continue; } else if ( 2 == some_function( $item ) ) { break; } else { another_function( $item ); } } But I would immediately concede that it is starting to miss the entire point of why it was enforced in the first place...
- joshuamorton 7y agoI actually like this style (or similar if not quite as extreme). I find it's much clearer what's going on.