Boolean parameters or Enums?

By mOrPhie on Wednesday 12 March 2008 15:44
Category: programming, Views: 1285

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. ;-)

Volgende: Virtual PC "erratic mouse" problem with some intel-chipsets 12-03
Volgende: .NET Framework 3.5 will be released with source code 04-10

Comments


By T.net user Diradical, Wednesday 12 March 2008 16:44

Why use enums in an OO language.
You could refactor them to objects

By T.net user Little Penguin, Wednesday 12 March 2008 16:51

Why use enums in an OO language.
Well, in Java you can do both, because Enums are objects in Java 5 (JSE1.5+) - so in Java you get the best of both worlds :)

By T.net user mOrPhie, Wednesday 12 March 2008 23:45

Why use enums in an OO language.
You could refactor them to objects
That example applies to the domain model. There is a relation between a person an a bloodtype. My example isn't about domain modelling, but about the API specification of an object model. There is no domain specific relation involved. I should have made that clearer in my post.

As for using enums in the domain model, this is another discussion. I prefer refactoring to objects. Mainly for reporting and for staying dynamic.

By mesa, Thursday 27 March 2008 09:52

Using an enum iso bool hides the information that there are only 2 values: on or off. If there are more options then just 2, you can use an enum (instead of an int). If a parameter that was once just true or false, suddenly changes to more possible values then the responsibilities of that function changed. It has become too different to be compared with the previous instance and such changes warrants a changed interface. Again, a one size fits all interface hides information.

By T.net user Ruudjah, Tuesday 22 April 2008 11:40

Another huge advantage of using enums is that you can gicve meaning to a value. A boolean can only be true or false, while an enum says something about the value. Example:
"Is your laptop broken?" can have two answers: "Yes" and "the laptop is broken". I would prefer the second answer, since that answer can never by doubted about, since it references semantically back to the question.

Comment form
(required)
(required, but will not be displayed)
(optional)

Please enter the characters you see in the image below: