The Artima Developer Community
Sponsored Link

Java Answers Forum
enum best practices?

3 replies on 1 page. Most recent reply: May 13, 2006 10:18 AM by Ravi Venkataraman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Paul Tomsic

Posts: 3
Nickname: ptomsic
Registered: Jan, 2003

enum best practices? Posted: May 11, 2006 1:55 AM
Reply to this message Reply
Advertisement
what's the common best-practice employed for use of enums?

say you've got a ridiculiously large codebase that makes liberal use of wire-objects and RMI.

there's a ton of static enum's that cover off on things like "Color.RED" or "Color.WHITE" but when you need to add a new color, you've got to rev the whole jar's version that the enum resides in just to add one line of code that says

public static final Color BLUE = new Color("Blue", some_other_attribute);

this is a trivial example, of course, but imagine hundreds of these and on a rapid pace, rev'ving of versions across the board, b/c of one new "Color" or one new "Foo" in an enum.

just curious how others have dealt with this.

the obvious right answer is don't get into this situation in the first place, but the codebase is so big that there's a lot of anti-patterns in there from legacy-gone-by.

thanks in advance


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: enum best practices? Posted: May 11, 2006 4:20 AM
Reply to this message Reply
I used enums like:
enum Correction{LEFT, RIGHT, CENTER};
 
enum Direction{ASCENDING, DESCENDING};
 
enum WorkSide{TOP, BOTTOM, LEFT, RIGHT, FRONT, BACK};
 
enum Workstep{CLEAN_AREA, FINAL_CUT, ROUGH_CUT, ...};
 
enum Status{OK, NO_TOOL, INVALID_COORDINATES, ...};
 


I just love enums. No more error handling except the check for null, readable code ...

Use enum for everything where you previously used constant Strings or ints, so no one can mess with invalid parameters.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: enum best practices? Posted: May 11, 2006 6:36 AM
Reply to this message Reply
> what's the common best-practice employed for use of
> enums?

I'm not sure you are talking about enums here. You seem to be talking about 'Java constants'.

> there's a ton of static enum's that cover off on things
> like "Color.RED" or "Color.WHITE" but when you need to add
> a new color, you've got to rev the whole jar's version
> that the enum resides in just to add one line of code that
> says
>
> public static final Color BLUE = new Color("Blue",
> some_other_attribute);
>
> this is a trivial example, of course, but imagine hundreds
> of these and on a rapid pace, rev'ving of versions across
> the board, b/c of one new "Color" or one new "Foo" in an
> enum.

One of the things I see in our code is that people used 'constants' for things that should be dynamically loaded. It's good to define constants instead of using the same literal throughout code but it's still hardcoding. It's not something that should be done as a general solution for any set of data. I see really dumb things like a list of constants (like currency codes) that must be updated everytime a new value is added to the database. That's an anti-pattern for sure.

So, I would say that in a lot of cases, the solution is to eliminate the constants and load them at runtime from an external source.

Ravi Venkataraman

Posts: 80
Nickname: raviv
Registered: Sep, 2004

Re: enum best practices? Posted: May 13, 2006 10:18 AM
Reply to this message Reply
I strongly agree that
 public static final ... FOO 
is used where the data should be loaded dynamically from an external source, a database or a text file. When I make this argument I am told that it would be overkill and that the list of values is never going to change anyway!

Personally I see little difference in expressiveness or effort writing (the most commonly followed way)
 Color red = Color.RED; 
and
 Color red = Color.get("red"); 

The second option has the advantage that we do not have to recompile anything when a new colour is added, as the original poster was complaining about, and we have the advantage that James mentioned.

Flat View: This topic has 3 replies on 1 page
Topic: gener/ol Previous Topic   Next Topic Topic: Q about type conversion in Java 5

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use