.NET, C#, Programming Tutorial

C# Tutorial – Why void is a bad idea

I’ve been doing this programming thing for a long time now (8 professional years), and during that time I have accumulated a lot opinions (some are right, some are probably wrong). In this phase of my career I am looking at what programming constructs can I use to make my life easier. One thing that sticks out to me as a sore point is the keyword void. In a previous post I showed you about Fluent Interfaces, in this post I’ll try and elaborate how and why you should avoid void.

To state it simply, void is useful when I want to perform an action on an object and don’t expect a result returned.

In theory it sounds great, but if I don’t expect a single result back, I am most likely going to want to inspect the object itself, or perform another action on the object. Let’s take a look at an example.

public class Calculation {

    public class Result { get;set; }

    public void Add(int number) {
        Result += number;
    }

    public void Subtract(int number) {
        Result -= number;
    }    
}

var calculation = new Calculation();
// call void methods
calculation.Add(10);
calculation.Add(20);
calculation.Subtract(10);
// write out result
Console.WriteLine(calculation.Result);

I’ve repeated myself a lot above. The variable calculation shows up a lot. This all happens because I made the design decision of using void.

Now what happens when I change void to return this, or the instance of the object that I am using?

public class Calculation {

    public class Result { get;set; }

    public Calculation Add(int number) {
        Result += number;
        return this;
    }

    public Calculation Subtract(int number) {
        Result -= number;
        return this;
    }

}

// write out result
Console.WriteLine(
    new Calculation()
        .Add(10)
        .Add(20)
        .Subtract(20)
        .Result);

All I did was Change the void to return the instance and I instantly got a clearer and more concise codebase.

Conclusion

The void keyword cuts off any chance to chain calls and add brevity to your codebase. If you have nothing to return, it is always a good idea just to return the instance itself. You lose nothing by doing so, but you have so much to gain. So ditch void and just return the instance itself. Now if only C# supported this construct. I guess I can always wish :).

public class Calculation {

    public class Result { get;set; }

    public this Add(int number) {
        Result += number;
    }

    public this Subtract(int number) {
        Result -= number;
    }
}

Happy coding!!

From tech.pro

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s