Re: enums, using methods as initializers
- From: mekane <mekane8@xxxxxxx>
- Date: Thu, 15 Nov 2007 15:47:02 -0600
Daniel Pitts wrote:
mekane wrote:Yes, it is far more than an implementation detail, it is a design principal.
That is very specifically a Bad Idea!
f.extract should NOT have a switch statement, but instead should be polymorphic.
What difference does it make? Anything other than I might forget to add another case?
Is this more than just an implementation detail?
Switch statements should be avoided. I know this is going to sound snobby, but polymorphic behavior is far superior for this situation. Not only is it likely to have better performance, it is easier to refactor into a more useful idiom. Say someone wants to add a custom field extractor, its easy to change these enums into a regular class, and have the extract be a method in an interface. That way, the client can say "extract this field with this approach."
I see. That makes sense, and I would agree that polymorphism is much more elegant. But isn't the point of an enum to say "here are all the possible values of this type, that's it". So a better design decision here would be to use something other than enums in the first place. Especially if you can't say for sure that the fields will never change.
To me, an enum and a switch work nicely together, especially when the alternative is to write different versions of a big, complicated method in the definition of an enum.
I'm not trying to argue, I'm just expressing an opinion.
Would you never use a switch?
.
- Follow-Ups:
- Re: enums, using methods as initializers
- From: Daniel Pitts
- Re: enums, using methods as initializers
- References:
- enums, using methods as initializers
- From: Mikhail Teterin
- Re: enums, using methods as initializers
- From: mekane
- Re: enums, using methods as initializers
- From: Daniel Pitts
- Re: enums, using methods as initializers
- From: mekane
- Re: enums, using methods as initializers
- From: Daniel Pitts
- enums, using methods as initializers
- Prev by Date: Re: Great SWT Program
- Next by Date: About swing Timer and synchronized
- Previous by thread: Re: enums, using methods as initializers
- Next by thread: Re: enums, using methods as initializers
- Index(es):
Relevant Pages
|