Sunday, 8 September 2013

How to handle enum parameters properly?

How to handle enum parameters properly?

This question is about a certain bad programming practice. I am looking
for some good article that explains about it and why it's bad.
Unfortunately, I don't know any name or jargon for this practice. So I
will try to describe it instead and ask you to tell me how it's called and
maybe refer me to a good article on the subject.
Suppose we have some enum:
public enum Day
{
SUNDAY, MONDAY, ...
}
Next, suppose that the server-side programmer attaches a numeric id to
each day. The correct thing to do, of course, is to map each Day to an
Integer using an EnumMap, which is defined and used only within the
server-side code. Instead, he messes up the common code and adds a getId()
method:
public enum Day
{
SUNDAY {public int getId() {return 100;}},
MONDAY {public int getId() {return 101;}},
...;
public abstract int getId();
}
Next, the GUI programmer wants to attach a string to each day, so he adds
his own stuff:
public enum Day
{
SUNDAY {
public int getId() {return 100;}
public String getName() {return "sunday";}
},
MONDAY {
public int getId() {return 101;};
public String getName() {return "monday";}
},
...;
public abstract int getId();
public abstract String getName();
}
You get the point...
How do you call this thing? Know any good article about it?

No comments:

Post a Comment