Thursday, February 23, 2017

Coding don'ts: enourmous if blocks

When wading trough other peoples code I find these annoyingly large if structures, especially used inside while/foreach loops that check if some item is ok to process or not.

Especially in PHP code for some reason or another. It's like people don't know about continue.

Don't do this!

foreach ($something as $item) {
 if ($item) {
   ...
   .. screenfull of code ..
   ...
 }
}

Do this instead

foreach ($something as $item) {
 if (!$item)
   continue;

 ...
 .. screenfull of code ..
 ...
}