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

Try to fix it before you try to break it

“As a user I don’t want to fix problems that could be fixed by the program itself.” – might be a user story that should be applied to almost every software out there.

If you want your software to succeed, you should think about some tasks you could take off of your users responsibilities. I use LINQPad as a code scratchpad during my daily work. Sometimes I want to test a simple statement with the ‘C# Expression’ feature.

image

In this case, as a programmer, you are familiar with ending each statement with a semicolon. LINQPad does not allow that when using single expressions. Why should the software raise an exception in this case, when it is possible to easily remove the semicolon at the end of line.

Defensive coding is your friend

This post is a response to Ayende’s post http://ayende.com/blog/160898/defensive-coding-is-your-friend please read it before continuing.

It took me a while to understand what Ayende is trying to say with this post. First I thought he is faking his test results. The test is expecting that Fiddler is running on the test machine and he was trying to ignore it was not running. So why his test isn’t simply looking like this?

[Fact]
public void Should_get_independent_notification_subscriptions() {
    using (var store = NewRemoteDocumentStore(fiddler: false)) {
        /// Do whatever was intended to be done
    }
}

Then I looked up Fiddler and found that it is used for logging HTTP-Traffic. I realized what he was trying to explain. When executing this test locally on his machine (where Fiddler seems to be installed) he wants to be able to log traffic for development purposes but on the build server no one cares about that.

So Ayende’s solution is to check if fiddler is running and then to decide whether the test should use it or not.

What annoys me is the fact that you make your test system dependent, or at least give it the logic to behave different on different systems.

A possible solution that comes to my mind is to add another test class that provides the same tests, but uses Fiddler instead of the direct Url.

public class LocalDocumentStore : IDocumentStore {
    public LocalDocumentStore() { 
        //Using http://localhost:8079
    }
}

public class FiddlerDocumentStore : IDocumentStore {
    public FiddlerDocumentStore() {
        //Using http://localhost.fiddler:8079 here
    }
}

public abstract class SubscriptionTestBase<T> : IUseFixture<T> where T : IDocumentStore, new() {
    IDocumentStore store;

    [Fact]
    public void Should_get_independent_notification_subscriptions() {
        using (var session = store.OpenSession()) {
            session.Store(new TestDocument { Name = "Name" });
            session.SaveChanges();
        }
    }

    public void SetFixture(T data) {
        store = data;
    }
}

public class LocalSubscriptionTest : SubscriptionTestBase<LocalDocumentStore> { 

}

public class FiddlerSubscriptionTest : SubscriptionTestBase<FiddlerDocumentStore> { 

}

OK, it is a lot of work compared to Ayende’s solution but it is more understandable, in my humble opinion. The class LocalSubscriptionTest is used on build server for automated testing and the FiddlerSubscriptionTest is ignored except I explicitly start this test for debugging purposes.

The missing piece in that puzzle is, that it seems that xUnit.net does not support skipping of classes (like it would be possible in NUnit for example).

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.

Don’t show the user things he doesn’t want to see

It might sound stupid, but think of your users as they would behave like the most stupid user you might imagine.
Try to guide them through your application, but do not ask to much of them.

Today, during my work with Visual Paradigm, a dialog appeared in the upper right corner. My focus went up, away from what I was currently doing, I read the story it was telling me and my thirst thought running through my mind was: “So what?”.

imageI was trying to bring a control to front, but it was already there. If you don’t mind my saying, that’s a dialog I didn’t expect to see. It is telling me, that an operation that I tried to execute couldn’t be executed, because it wouldn’t change anything. Inexperienced  users might think they would have done something wrong, while scratching their head.

That reminds me of my colleague tapping Ctrl + C for copying something four, maybe five, times repeatedly popping up an message for every tap: “Cannot copy content because it is in your clipboard already”

Now to one aspect the guys from Visual Paradigm solved perfectly. The message doesn’t need acknowledge, like a message box would, the user can continue work and the dialog will fade out after 10 seconds without any further action needed. That’s the behavior I’d expect when displaying a message of that severity.