Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, 19 February 2009

Nullable<bool>


I don't like Nullable<bool>. The cognitive dissonance is louder and more dissonant than a Sepultura guitar solo. [3'00]

A boolean is defined as: "Of or relating to a data type or variable in a programming language that can have one of two values, true or false."

Two. Deux. Zwei. Dos.

TWO!

Not three.

But that is what exactly Nullable<bool> is. True, false and null. Three states.

What does null mean? It's got to mean something. You're either pregnant or you're not. You can't have a third state. A light switch is either on or off.

If a light switch doesn't exist then it's potentially very dangerous. Almost as dangerous as having a nullable bool.

If it has three states, it isn't a bool.

Tuesday, 7 October 2008

Comments Anti-Pattern

Code comments. They are good right? Well yes. However you can fall into the trap of thinking if commenting is good, more comments are better. Comments all 'round. Comments for everyone! The Milkybar comments are on me.

This is the common anti-pattern:
/// <summary>
/// Tests if an order can be prepared for printing.
/// </summary>
[Test]
public void PrepareOrder()
{
...
}
If you multiply that by every public method and or test it can make the class files look a bit jumbled.

So why not refactor by renaming the method and lose the comment?
[Test]
public void OrderCanBePreparedForPrinting()
{
...
}
or even
[Test]
public void Order_Can_Be_Prepared_For_Printing()
{
...
}
I find this far easier on the eye.