Boolean parameters or Enums?

By mOrPhie on Wednesday 12 March 2008 15:44 - Comments (5)
Category: programming, Views: 1497

We all did it: adding a parameter like “bool useCachedConnection” to a method. When using the method it will look like this:


C#:
1
DoSomething(true);


While the method works, one day your collegue (or yourself) will come across this function and needs to digg into the definition to understand what the boolean implies, or worse, needs to adjust the method interface due to the fact that there are three options, instead of using or not using the cached connections. I’ve seen it several times and it saves a hell of a lot time if boolean parameters were to be avoided. A better way to do it:


C#:
1
2
3
4
5
6
7
8
9
10
11
12
enum ConnectionOptions
{
    CachedConnection,
    NewConnection
}

void DoSomething(ConnectionOptions options)
{
    //
}

DoSomething(ConnectionOptions.CachedConnection);


Now it’s possible to add options without having to change the methods’ interface. After a while you’ll have a nice collection of enums which can be reused by other projects. And don’t forget that the .NET Framework holds his own collection of enums which can be reused. When working with languages like T-SQL you don’t have a choice, but with .NET you have. Please make use of it. It’ll brighten up my day….. and yours. ;-)

.NET Framework 3.5 will be released with source code

By mOrPhie on Thursday 04 October 2007 15:44 - Comments (0)
Categories: programming, techrelated, Views: 410

As anyone can read on Scott’s page Microsoft decided to release the source code (with comments) of the .NET Framework (not the CLR as some blogs might make you think), beginning with V3.5. This is a big thing. But I want to tell you why.

There are several disassembly tools available for .NET which allow you to disassambly an assembly into source code. When you want to see what happens in System.Threading, just disassemble. But, it won’t provide you with debugging symbols. The result is you need to read it line by line when you want to figure something out. Hooray for Scott, because this release won’t just include the source code, but also debugging symbols, which are dynamically (down)loaded from the MSDN in VS 2008. Hence, the big thing. Great stuff. :-)

FizzBuzz

By mOrPhie on Wednesday 20 June 2007 15:41 - Comments (1)
Categories: life, programming, techrelated, Views: 309

FizzBuzz is a common example to test whether someone is competent in programming. The assignment is to write a program which prints 1 to 100. For every multiple of 3 print ‘Fizz’ and for every multiple of 5 print ‘Buzz’. For every multiple of 3 _and_ 5 print ‘FizzBuzz’.

The trick is that most of the time only competent programmers know of the “modulo” operator (% in C, C++, C# and others). Silly thing is: it’s usually true! Why? No one knows…

Competent programmers should be able to com up with this under 5 minutes:


C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (int i = 1i <= 100i++)
{
    if (i % 3 == 0 && i % 5 == 0)
    {
        Console.WriteLine("FizBuz");
    }
    else if (i % 3 == 0// && i % 5 != 0
    {
        Console.WriteLine("Fiz");
    }
    else if (i % 5 == 0// && i % 3 != 0
    {
        Console.WriteLine("Buz");
    }
    else
    {
        Console.WriteLine(i);
    }
}


Now don’t rely on this example. There are other tests out there. Still, it’s quite funny to see your colleagues struggle with something so simple and finally bang their head against the table if you tell them the trick.