Tech /

Else If Style





edit SideBar

Else If Style

Else If chaining with unrelated expressions is bad style

Related expression chaining is GOOD

if (a == A)
    X;
else if (a == A2)
    Y;
else
    Z;

Unrelated expression chaining is BAD

if (a == A)
    X;
else if (b == B)
    Y;
else
    Z;

Nested Equivalent

if (a == A)
    X;
else
{
    if (b == B)
    {
        Y;
    }
    else
    {
        Z;
    }
}

Equivalence

ChainedNested
if (A)
    X;
else if (B)
    Y;
else
    Z;
if (A)
    X;
else
{
    if (B)
        Y;
    else
        Z;
}

Truth table

ABChainedNested
11XX
10XX
01YY
00ZZ

Links

Recent Changes (All) | Edit SideBar Page last modified on 04 January 2010, at 05:34 PM UTC Edit Page | Page History
Powered by PmWiki