Tag Archives: Clean Code

Name your code

I stumbled upon a code snippet that is not easy to understand, until you know what it is doing.I remember my colleague explaining it to me a while ago, but it seems that I forgot what it is used for since the last time I looked at it.

CallMethod(value + "");

So this is all of the code. It looks like it is used for converting value object a string. You might think, why the hell he isn’t using the ToString method?


You would be right in most cases, but the real purpose of this usage is to handle null values and convert to a string. Another way to write it, maybe a bit more understandable.

CallMethod(value == null ? string.Empty : value.ToString);

Another possible solution is to introduce an extension method, this will give you the advantage of naming your code. Another coder will know what your intention was, without asking for it.

public static string ToStringOrEmpty(this object value) {
    return value + string.Empty;

I created a repository on GitHub for my commonly used extension methods. Currently it is rather empty but trust me, there is a lot to extend. https://github.com/dwoni/Extensions