“Else” is evil

One of the most important quality attributes of source code is the readability. Readability forms the base for understandable which in turn is necessary to fulfill other quality attributes like the possibility for (ease) modifications, extensions, and adaptations.

Of course there are a lot of possible reasons for bad code and there are also a lot of existing best practices which will help to write nice source code. The whole topic is known as “clean code” development.

But within this article I don’t want to write just another blog about clean code. I want to draw the attention on a dangerous base keyword available in nearly all programming languages. No, it is not the “goto” keyword, I speak about “else”.

But why do I think “else” is evil? Because one of the most recent reasons for complex code are if-else blocks. Even simple if-else blocks with just an if and a else part but with a lot of content within each part will reduce the readability of source code. And of course complex nested if-else blocks are nearly incomprehensible. Even if they have just a few lines of code as content and will fit on one page, it will cost a lot of time to understand such nested if-else blocks. And usually such complex statements will extend over several pages.

A simple if-statement is easy to understand. Even nested if-statements are relatively easy. Of course if you nets 10 if-statements, it will result in an ugly arrow shaped code, but even this code is (easy) to understand. Therefore I try to use at maximum three nested if-statements.

But as soon as an else-statement is used the source code gets more complex, as it becomes branches. Even a simple if-else-statement with another nested if-else-statement is very hard to understand. This issue will grow exponentially with each nesting layer.

So, from my point of view, the “else” statement is a source of friction. It has the big disadvantage that it will create complex code and it has nearly no advantage, as each if-else-statement can also be implemented without use of “else”.

Therefore I started a simple self-experiment and eliminated the “else” statement from my keyword list. Surprisingly this was a really easy process. After a few days I don’t even miss this common keyword.

So after a few projects and thousands of lines of code without a single else-statement I can say the “else” keyword is not needed and without this keyword the quality of the source code increases noticeable.

A few sentence before I wrote that it was surprisingly easy to eliminate the else-statements. Following I want to show you three very simple template. By using these templates you are able to remove nearly 100% of the else-statements from your source code.

 
Initialization instead of else-statement

The following source code shows an typical if-else-statement. Depending on a value some other values is set.

private void DoSomething(bool b)
{
    int x;
    if (b == true)
    {
        x = 1;
    }
    else
    {
        x = 2;
    }

    DoSomethingOther(x);
}

 
The else-statement may be eliminated by using a initialization for the variable. The following source code shows the adapted version of above function.

private void DoSomething(bool b)
{
    int x = 1;
    if (b == false)
    {
        x = 2;
    }

    DoSomethingOther(x);        
}

 
Return instead of else-statement

The next example contains nearly the same function like the first example, but this time a result will be returned instead of calling the next function.

private int DoSomething(bool b)
{
    int x;
    if (b == true)
    {
        x = 1;
    }
    else
    {
        x = 2;
    }

    return x;
}

 

Of course, the else-statement may be eliminated in the same way like before, with an initialization of the variable. But this time you will have another possibility: an immediately return.

private int DoSomething(bool b)
{
    if (b == true)
    {
        return 1;
    }

    return 2;
}

 
Verification instead of else-statement

At next we have an if-else-statement with verification. It will be checked whether a value is valid or not. The source code looks nearly equal to the second example, but this time the if-statement is implemented by another reason.

private int DoSomething(string s)
{
    int x;
    if (s != null)
    {
        x = s.Length * 3;
    }
    else
    {
        x = -1;
    }

    return x;

}

 

The solution is also nearly equal to the one of the second example. At first you will do the parameter validation and then you will execute the function content.

private int DoSomething(string s)
{

    if (s == null)
    {
        return -1;
    }

    return s.Length * 3;
}

 

If you have several parameters or several validation checks for one parameter you can now implement atomic checks instead of one complex and maybe nested if-else-statement.

 
Verification and initalisation

The next example shows a more complex statement.

private void DoSomething(string s)
{
    int x;
    if (s != null)
    {
        if (s.Length == 0)
        {
            x = 0;
        }
        else
        {
            x = s.Length * 3;
        }
    }
    else
    {
        x = -1;
    }

    DoSomethingOther(x);
}

 

It can be simplified by using the first and third template (“initialization” template and “verification” template).

private int DoSomething(string s)
{

    int x = -1;

    if (s != null)
    {
        x = 0;

        if (s.Length > 0)
        {
            y = s.Length * 3;
        }
    }

    DoSomethingOther(x);
}  

 
Verification and return

The next example shows another complex statement.

private int DoSomething(string s)
{
    int x;
    if (s != null)
    {
        if (s.Length == 0)
        {
            x = 0;
        }
        else
        {
            x = s.Length * 3;
        }
    }
    else
    {
        x = -1;
    }

    return x;
}

 

It can be simplified by using the second and third template (“return” template and “verification” template).

private int DoSomething(string s)
{
    if (s == null)
    {
        return -1;
    }

    if (s.Length == 0)
    {
        return 0;
    }

    return s.Length * 3;
}

 
Summary

Within this article you have seen three simple templates which will allow you to remove all else-statements from you source code. Without else-statements your source code will become easier to understand and therefore it will be ready for modifications, extensions, and adaptations.

Advertisements
Dieser Beitrag wurde unter .NET, C# veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s