I thought an example would help others to make up their mind. The writer actually recommends the use of switch statements to implement constant-specific behavour rather than the "constant-specific methods" recommended by Joshua Bloch in 1.5 Tiger release documentation. He completely misses the vital point that an enum will not compile unless all of the constants provide an overriding implementation of an abstract method. This effectively avoids the maintenance problem associated with comparable switch statements.
He reasons as follows.
"What about just using a more generic method that determines what to do based on a switch statement? Well, that's a better idea, to be honest. Here's the (much cleaner) way to write OpCode:
...[code example omitted]...
"This is so painfully simpler than the first version of OpCode that I hesitated to even include this lab-but [sic] for completeness, here it is. If at all possible, though, consider using switch in your method bodies to direct program flow, rather than [constant-specific methods]."
Essentially his argument is one of code aesthetics, which makes me wonder if he is even a programmer. Again, he completely misses the raison d'etre for this language feature and seriously misguides his readers.
And then there is the issue of style.
I am so sick of reading "How do I do that?" and "What just happened" along with the author's generally condescending tone. For example, at the start of the above quoted section on what he likes to call "Value-Specific Class Bodies" (rather than using Bloch's name of "constant-specific methods"), the author concludes the introductory paragraph by saying: "This is a rather obscure bit of functionality, but sort of cool to talk about around the water cooler." There is nothing obscure about constant-specific methods; they are quite simply anonymous class bodies that implicity extend the enum type.
[ September 01, 2004: Message edited by: Doug Dunn ]
[ September 01, 2004: Message edited by: Doug Dunn ]
[ September 01, 2004: Message edited by: Doug Dunn ]
[ September 01, 2004: Message edited by: Doug Dunn ]