Category Archives: Best Practices

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?

CallMethod(value.ToString())

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

Shortcuts you shouldn’t need to know

A few days ago I learned a new shortcut of visual studio  Ctrl + Break.
You might wonder what this shortcut does, I didn’t need it for more than five years of professional programming, but suddenly it appeared in my list of useful shortcuts.

I was mentoring one of our starters, extending a WPF application, some of my colleagues are developing and I’m not actively participating during my daily work. When I was coding, like I’m used to be (building instead of saving), I got very fast very upset. I never ever really recognized the building process, except when there where errors popping up, but in this case it was so annoying it takes about a minute to build. With our really high end notebooks it takes a few seconds in most of our solutions, fast enough to not pay attention to it.
OK, so I’d have to change my habits (saving instead of building more often), but when it comes to starting the application with attached debugger it still takes me over a minute to continue my work.
So what should I do when I accidently start the application or I detect an error in the code I just edited? I could go for a break or play with something lying around at our desks. After a few days during another accidently start, my partner quickly hit some keys and I could correct my code almost without any delay.
I asked him which keys he hit and he told me that it’s Ctrl + Break. I didn’t even know that that command exists.*

So now you know my story, but what I’m trying to tell you? That solution we are working on has almost 50 active projects, no wonder that it takes ages to build. So please, If you start detecting your build process, start splitting your solution before it is too late.

* Almost. I knew that shortcut from WinDbg already, but didn’t know that it exists in Visual Studio too.