if (condition) continue; OR if (!condition) { ... }? (style preference)

I prefer Style 1 - with the indenting.


I prefer Style 2 - with the continue statement.


In principle I agree with the majority who prefer style 1. This is what Steve Mcconnell endorses in "Code Complete" - say what you mean, i.e. if you are more interested in the condition being true, while the false state is rarer or not preferred, then state the preferred version.

In practice though I often find myself using style 2, because I like to weed out all the possible error / invalid states first. After I get rid of all the possibilities I am not interested in, I can write the heart-of-the-matter code down to the end of the routine without constantly wondering if I need to guard against some condition or other. Basically, the attitude is, get rid of the chaff, then do the real work in peace.

Tags:

Coding Style