Still, I believe that if you really care about making the structure of the code clear, then you would be well advised to (a) avoid inverting the sense of boolean tests, and (b) avoid relying on the fact that one arm of a conditional has a control transfer so  that you can “get away with” saving a level of horizontal indentation.

I think the clarity knife sometimes cuts in this direction, but sometimes in the other direction.

If I have:

    if (x instanceof P(var y)) {
        // more than a page of code
    }
    else
        throw new FooException();

vs

    if (!(x instanceof P(var y)))
        throw new FooException();

    // the same page of code

In the latter case, i've checked all my preconditions up front, so it's more obviously fail-fast.  Maintainers are less likely to forget the condition they just tested a page ago, and readers are more able to build a mental model of the invariants of the happy path for this method.  So I think it's not always about "saving indentation"; in this case it's "get the precondition checks out of the way, and set me up to do the work without further interruption."


Reply via email to