Re: enums, using methods as initializers



Daniel Pitts wrote:
mekane wrote:

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?
Yes, it is far more than an implementation detail, it is a design principal.

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?
.



Relevant Pages

  • Re: enums, using methods as initializers
    ... 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. ... 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 saying there are NEVER times when you can use switch statements, ... I'm just saying that by the time I need one switch statement, I probably need two, and at that point its time to use polymorphism and create an abstract method for each of my switch statements. ...
    (comp.lang.java.programmer)
  • Re: Long switch statements (and Swing application structural design)
    ... In one application I have a long switch statement that looks like this ... I don't know off-hand whether you can make an `enum' abstract, ... but "You can declare method abstract in the enum type and ... home dot woh dot rr dot com slash jbmatthews ...
    (comp.lang.java.programmer)
  • Re: Newbie: Problems Picking up integer values (dereferencing problem?) Really Trying To ReadTheGosh
    ... rather than just omitting it. ... the colon after the label must be followed by a statement ... the default label is at the very end of the switch statement. ... the enum, or a default. ...
    (comp.lang.c)
  • Re: switch
    ... using polymorphism." ... Imo the decision not to implement a switch statement was wise. ... have a switch statement because polymorphic dispatch is the right way ... If you see too many elifs in your code (or if you find yourself ...
    (comp.lang.python)
  • Re: Random Enum
    ... Lew wrote: ... It actually took me a while to realize that although Enum itself couldn't be subclassed directly, Enum still works fine as a super class capable of holding any enum. ... Sometimes, polymorphism is better than generics, although in the example I gave I think generic are actually a bit better, or it's a wash if you're a bit charitable. ...
    (comp.lang.java.help)