Dark code-style academy: spoil if statement

    image


    Do you want to raise your salary? Do you want always to be in demand? Do you want to have your job as long as you want? It is absolutely real! You just need to change the way you write your code. Basically, you need to increase your job security. You have to write code which will be almost impossible to maintain for everyone except you. And in these series of articles, I will tell you how to achieve it. Welcome under the cut.


    How to spoil if statement


    Let’s start from if statement. Developers usually underestimate the importance of if. But it is a really powerful statement. It is very easy to confuse other developers using special approaches. So, here are a few basic ways to spoil if statement.


    Case #1


    Take a look at this piece of code. What’s wrong here?


    if (condition) 
    {
        // actions when true
    }
    else
    {
        // actions when false
    }

    Of course, you noticed. Here we missed the simplest place to confuse the reader. Always inverse condition. At least once.


    if (!condition) 
    {
        WhenFalse();
    }
    else
    {
        WhenTrue();
    }

    My favorite one is !string.IsNotEmpty(value).


    Lesson: Always keep inverse condition.

    Case #2


    Ok, that was an easy one! How about this?


    if (nameIsValid) 
    {
        // ...
    }

    Yes! You are right. We don’t need readable and clear conditions. It becomes much easier to read and maintain such if. The easiest way to spoil this one is to paste calculation directly into the condition.


    if (!string.IsNullOrEmpty(name) && name.Length > 4 && name.Length < 20) 
    {
        // ...
    }

    Now is much better.


    Lesson: Make the condition as complex as you can.

    Case #3


    Go to the next one. This one is quite simple as well. Thoughts?


    return condition;

    Absolutely! If you don’t have if statement then you must add it.


    if (condition)
    {
        return true;
    }
    else
    {
       return false;
    }

    Looks good.


    Lesson: Transform boolean return values to conditional statements.

    Case #4


    What about this one? Take your time to think!


    if (condition)
    {
        UsefulFunction();
    }
    else
    {
        OtherUsefulFuntion();
    }

    Do we need to decompose complicated parts to separate methods? Of course not. Developer who reads this code should be busy trying to understand what that method does in order to make him forget what was the relevant condition. Moreover, while he will be parsing code from else part he should already forget what was in a then part.


    if (condition)
    {
        argsCount++;
        queue.Enqueue(new[] { default(Element), default(Element), a });
        _scanner.DeleteRestorePoint();
        _writer.ChangeCursor();
        value = 42 * days;
    }
    else
    {
        var element = default(Element);
        var children = queue.Dequeue();
        while (children[0] == Grammar.New)
        {
            children = queue.Dequeue();
        }
        element = new Element(ElementType.MemberExpression, children);
    }

    Isn’t it awesome? We will cover the ways how to generate such a useless code later. Anyway, don’t even try to understand what is going on there. I don’t know that either.


    Lesson: Do not extract methods and leave complicated code inside if statement.

    Case #5


    One of my favorites. What is wrong here?


    statusHandlers[status].Handle();

    We don’t need to produce clean code! We don’t need any patterns or other smart stuff. Leave Strategies and Chains of responsibility to junior developers. Checking status case by case makes code bloating. Now it becomes much more difficult to reuse such code. Developers who will be supporting this can easily get confused and produce a bug. That is what we need. Job security!


    if (status == Status.New)
    {
        NewHandler();
    }
    else if (status == Status.Open)
    {
        OpenHandler();
    }
    else if (status == Status.Closed)
    {
        ClosedHandler();
    }

    We can make it worse. Replace enum with magic numbers or strings and as we learned before – paste methods code inside then. Enjoy.


    Lesson: Do not use patterns. Seriously.

    Case #6


    Relax, here is not so complicated way to make your code dirty. Take a look at this piece of code.


    var condition = first && second;
    
    if (condition)
    {
        // code
    }

    See and operation? It means that you can split condition and place each one into its own if statement.


    if (first)
    {
        if (second)
        {
            // code
        }
    }

    Lesson: Place conditional inside each other if you can.

    Case #7


    This one is difficult.


    return string.Format("{0:P2}", systemPower);

    Do not use formulas or any calculations. Why do you need to spend such an awesome opportunity to spoil the code? You can provide value for every case. The code becomes huge, difficult to read and understand.


    if (systemPower == 1)
    {
        return "100%";
    }
    else if (systemPower == 0.99)
    {
        return "99%";
    }
    else if (systemPower == 0.98)
    {
        return "98%";
    }
    ...
    else if (systemPower == 0) {
        return "0%";
    }

    Simple change which will give you a couple of unforgettable days of fixing bugs with double values.


    Lesson: Provide values for every case in a separate if statement.

    What I need to add, is that you must be careful about code reviews and quality gates on your CI. Later on, we will cover those topics, but for now, think out what you just learned.


    If you are mature enough so that you have your way to spoil if statement, let me know in comments. I will keep this post updated.

    AdBlock has stolen the banner, but banners are not teeth — they will be back

    More
    Ads

    Comments 0

    Only users with full accounts can post comments. Log in, please.