(no title)
flaviu1 | 4 years ago
- There are many many languages without classes that make good use of enums
- Doing this refactoring is excessively verbose
- Someone making bold claims like this about a language feature that's never been considered error prone and been around since the beginning ought to provide some really good evidence to back things up.
There's cases where enums are good, and there's cases where polymorphic classes are good. Dismissing one of them by default is wrong, since both are useful at different times.
edit: and the citation there to Martin Fowler et al., Refactoring: Improving the Design of Existing Code doesn't even match the claim being made. That book mentions
- "The first part of this problem is that switch statement. It is a bad idea to do a switch based on an attribute of another object. If you must use a switch statement, it should be on your own data, not on someone else's." -- sure, I agree
- "Often you find the same switch statement scattered about a program in different places. If you add a new clause to the switch, you have to find all these switch, statements and change them. The object-oriented notion of polymorphism gives you an elegant way to deal with this problem." -- fair enough
This is a long way from "enums are a code smell," and honestly feels like padding the citation count. Another thing to note is that the second edition of Refactoring: Improving the Design of Existing Code says:
> Even in our more wild-eyed youth, we were never unconditionally opposed to the conditional. Indeed, the first edition of this book had a smell entitled “switch statements.” The smell was there because in the late 90’s we found polymorphism sadly underappreciated, and saw benefit in getting people to switch over.
> These days there is more polymorphism about, and it isn’t the simple red flag that it often was fifteen years ago.
oflannabhra|4 years ago
Which, in my experience is a huge advantage rather than the supposed disadvantage Fowler is saying it is (although I could see that being the case in many other languages).
bryanrasmussen|4 years ago
originally the meaning of code smell was not that code that had the smell was bad, but there was a chance it was and should be examined. For this reason of course it is useful to get rid of code smells so that people don't feel the need to investigate hey is this smelly code actually bad code.
But all that said not sure if an enum is a code smell.
on edit: this at any rate was the rationale behind the phrase code smell I was first introduced to.
masklinn|4 years ago
Even that seems like it could easily be way overkill e.g. if you have multiple switches which, say, generate a label from an enum, the first step is probably to add a utility function / method, not to migrate the whole thing over to polymorphism.
Although there is one thing to be said about context:
* OP works in C#, whose enums are literally useless (they’re like C’s)
* apparently even in Java (which at least has type-safe enums even if not sum types), `switch` is unable to check for completeness
couchand|4 years ago
aardvark179|4 years ago
jaywalk|4 years ago