Mathew Nolton Blog

Software dementia unleashed...

How useful are enums?

I have been reviewing code and designs lately and when you have a discrete set of values that can be grouped together, you should use enumerations. Or does it really matter? Since .Net will allow you to pass in any integer value in place of an enumeration, you must still validate their values by either executing Enum.IsDefined() or some comparison operation (e.g. switch statement) to determine valid values. As Brad Abrams points out in this blog, IsDefined is expensive...and may not work as expected. So a switch statement is the only real way to validate valid values without a performance hit.

Now I still think Enum's are valuable, but you have to admit that they are far less useful when it comes to validation. Will it require that I come up with a Validating Attribute for my architecture as I do for validating other parameter types. For example:

public void UpdateBillingOption(
   [RequiredItem()][CustomerAccountNumber()]string accountNumber16,
   [RequiredItem()][MakeSureEnumIsValid(typeof(eBillingOption))] eBilling Option billingOption)

{
}
Will this really work?

Or should I use the converter concept and always use my custom converter classes to convert a value and throw a validation exception if it cannot make the conversion? For example:

eTaskCode taskCode = (eTaskCode)TypeDescriptor.GetConverter(typeof(

               eTaskCode)).ConvertFrom(eTaskCode.Task.ToString());

-Mathew Nolton

Posted: Feb 18 2004, 03:56 PM by MatiasN | with 4 comment(s)
Filed under:

Comments

james@enigmativity.com (James Roe-Smith) said:

One exceedingly useful tip is that the "Option Strict" setting should be turned on. I'm not a C# person, but I assume that it is available with C# as it is with VB.Net. Turning this on will prevent .Net accepting Integers for Enums... James.
# February 18, 2004 5:30 PM

Mathew Nolton said:

Unfortunately this won't work. Option Strict is always on for c#. This problem will typically happen if the client and server have different versions of the enumeration or if you Or enumerated values together. e.g. eMyEnum.Value1 | eMyEnum.Value2...or just doing a simple cast.
# February 18, 2004 5:40 PM

Udi Dahan - The Software Simplist said:

You just HAD to press the "performance" button.
=)

My thoughts about performance can be seen here:
This "performs" better than that - who cares ? http://udidahan.weblogs.us/archives/015589.html

I say use the Enum.IsDefined. When profiling the system, and you see that there is a performance problem because of the call - only then change it. And when you change it:

1. comment out the original line
2. Write a comment immediately following the Enum.IsDefined line explaining that the reason for the change is because of performance, stating under what circumstances the profiling was done.
3. Write the switch statement.
4. DON'T skip the comment step ! There's nothing worse than seeing code that breaks with conformity with the rest of the system without understanding why.
# February 22, 2004 7:32 AM

Mathew Nolton said:

i agree with commenting. an explanation of the who's what's where's and why's are always good for someone to see. else, some person at a later time will undo what you did.
# February 22, 2004 11:28 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)