My team has an “Effective Java Book Club” where we go over the book “Effective Java (3rd edition)” a few items at a time every couple of weeks. This is from Item 34 of that book.

Whatever you do, don’t do this.

I keep saying to everyone that when you’re writing code you’re writing code for the next person that happens to read your code. You are not writing it for the compiler. I couldn’t give a rat’s ass about what the compiler and runtime will deal with. I care about the person who will read your code.

Enums, in Java and most other languages, are a way of grouping named values together into a convenient structure. Java (and other languages as well) started to extend this to allow more class-like behaviors. If you’re going to add a bit of metadata to the DayOfWeek enum to say weekend or weekday, then I’m all for it. It’s still just a description of the value.

But when you start to give computations to enums, I think you’ve gone too far.

Programming is about communication of ideas to the next person. Enums mean a certain thing. This breaks that idea. This is surprising. Surprise isn’t a good thing in programming.

It’s akin to making a getter that has side effects. Is it a valid bit of code? Sure it is. The compiler will make it do what you say. But the next person who calls it will get a rude awakening. And that’s something that ought to be avoided.