Boolean parameters or Enums?
We all did it: adding a parameter like “bool useCachedConnection” to a method. When using the method it will look like this:
C#:
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#:
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. ;-)
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 | enum ConnectionOptions
|
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. ;-)
|
|
Virtual PC "erratic mouse" problem with some intel-chipsets |
|
|
.NET Framework 3.5 will be released with source code |
Comments
Why use enums in an OO language.
You could refactor them to objects
You could refactor them to objects
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 worldsWhy use enums in an OO language.
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.Why use enums in an OO language.
You could refactor them to objects
As for using enums in the domain model, this is another discussion. I prefer refactoring to objects. Mainly for reporting and for staying dynamic.
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.
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.
"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.