Use Enums Not Numbers

Everything said in UseEnumsNotBooleans (where it is advised that the boolean values "true" and "false" be used to represent themselves, and not be overloaded to represent arbitrary binary conditions) applies to the use of numbers to represent n-ary conditions, where n is typically greater than 2.

Of course, some languages (Java cough cough) don't provide proper enums (I know, JavaLanguage will in 1.5), so use of StaticFinalInt? is a workaround. (Declaring a set of FlyweightEnums--see FlyweightPattern--is probably a better workaround). If you do that, you should make sure that the values of the various cases are unimportant.

Using numbers for n-ary enums has one additional disadvantage over use of booleans for binary conditions--you may encounter numbers that are "out of range". Then what? If the language has subrange types; you can solve the problem that way.

Use of numbers to represent quantities that are not numbers is the 8th of P.J.Brown's DeadlySins.


DesignPattern


EditText of this page (last edited October 29, 2007) or FindPage with title or text search