News for hackers. Stuff that matters.
Re:When are enums not a code smell? by fivelessminutes on Sunday November 21, 2021 @07:09AM (#29295787)
> "WARNING As a rule of thumb, enums are code smells and should be refactored to polymorphic classes. [8]" Seemann, Mark, Dependency Injection in .Net, 2011, p. 342

Gosh, what a load of baloney from a frog in a well.

Re:When are enums not a code smell? by dang on Monday November 22, 2021 @03:32PM (#29311277)
Ok, but please don't post unsubstantive comments here. In particular, shallow dismissals and calling names are against the site guidelines. "A good critical comment teaches us something."

https://news.ycombinator.com/newsguidelines.html

Re:When are enums not a code smell? by userbinator on Monday November 22, 2021 @09:35AM (#29306684)
Seeing sentences like that are very illuminating to explain why a lot of software turns into bloated monstrosities.
Re:When are enums not a code smell? by quickthrower2 on Monday November 22, 2021 @09:51AM (#29306840)
“Should” is way too strong. “You should consider” is appropriate.

An enum is essentially an encoding over integers. I guess this depends on your programming language, so humour me.

If you have an integer whose meaning is a category (as opposed to a count or measure) - does this need refactoring into classes?

Probably not most of the time.

For example you import a CSV and one of the fields is a categorisation. Let’s say county code. Your code may occasionally care if you are in an EU country.

You could have a class per country and override isEU as needed but this is clumsy and ugly in my opinion.

You’d also need a switch statement, in a factory method to create these from the csv data.

A lot of the time it’s easier to follow code where the switch statement is in its intuitive place.

Highly OO abstracted code can be very hard to reason about too.

Re:When are enums not a code smell? by dahfizz on Monday November 22, 2021 @10:11AM (#29307106)
> Highly OO abstracted code can be very hard to reason about too.

Definitely. Coming up with all the right design patterns and abstractions can feel quite clever and satisfying. But when someone else needs to debug that code, having to jump through layers and layers of object, interfaces, factories, etc to find out what a method call is actually doing is a pain.

Re:When are enums not a code smell? by aardvark179 on Monday November 22, 2021 @10:32AM (#29307418)
I seem to remember the Martin Fowler advice that this apparently cited being slightly more nuanced, and it’s not totally wrong. If you have a simple enumerator and many switch statements depending on it then it is quite error prone if you have to add a value (depending on your language it may even be a silent failure), but a class will force you to implement the bits of behaviour you need as methods, and gives a good template for when you add another subclass. It’s not always the right thing to do, you’re trading the flexibility for others to add new types for the old flexibility of it being easy to add new behavioural decision based on a fixed set of types.

I think in a lot of cases it’s definitely worth stopping and thinking, and making sure you are implementing the right thing if you find yourself using enums heavily, or using a class hierarchy as an enumerator replacement.

Re:When are enums not a code smell? by dwaltrip on Monday November 22, 2021 @01:22PM (#29309823)
If I’m understanding correctly, languages like rust avoid that problem by throwing a compile error when a match statement doesn’t cover all possible cases.

Of course, a catch-all would then somewhat reintroduce the problem.

Re:When are enums not a code smell? by aardvark179 on Monday November 22, 2021 @04:14PM (#29311640)
Yes. The problem you then have is that adding additional values to the enum then either breaks compatibility or relaxes that guarantee. Since Java tries to run old code for as long as possible the only option for switch statements is to ensure there is a fallback so that you at least know there is a problem.
Re:When are enums not a code smell? by adamkl on Monday November 22, 2021 @10:07AM (#29307045)
In Mark’s defence, our views on software development can change over time as we learn and grow.

Mark, for example, is now primarily a functional programmer and makes good use of F#’s type system:

https://blog.ploeh.dk/2015/08/10/type-driven-development/

So he might even agree with you these days (on the “baloney” part, but maybe not the “frog in a well”).

Re:When are enums not a code smell? by kaetemi on Monday November 22, 2021 @08:58AM (#29306319)
That guy must be so afraid of bitfields.
Re:When are enums not a code smell? by raverbashing on Monday November 22, 2021 @09:56AM (#29306905)
I find it funny how Java (and maybe .NET, though the example is .NET - but probably "extended" from Java) seems to have catalyzed the worse types of cargo-culting, OO "overdose" and similar pseudo-good practices.

Then we end up with "corporate java code" that wants to throw out what was the basis of computing (if statements, switches) and turn all into some crazy type hierarchy.

Re:When are enums not a code smell? by Supermancho on Monday November 22, 2021 @10:52AM (#29307703)
Not so much .NET/Java but Structured Programming (1972 Edsger W. Dijkstra, Ole-Johan Dahl, and Tony Hoare) has prompted the gradual backlash against switches. (https://youtu.be/eEBOvqMfPoI?t=1512, re: Duff's Device). Senior developers should watch the whole thing to understand how block scope and functions and classes relate.

Modern development has drifted toward lots of small functions (which causes a mess because modern IDEs dont have supporting features like this: https://youtu.be/baxtyeFVn3w?t=1786, re: gtoolkit). A switch requires you to look inside and reason about the internals of a function to understand how it will behave before exiting, as the compiler. This doesn't matter so much if you dont care about early exits (no branch prediction, etc). Either way, it's extra cognitive overhead and error prone for a small improvement in readbaility. It's the line where programming becomes tricky programming.

As a thought experiment, imagine there is ANOTHER keyword that acts like an if/else (or switch with mandatory breaks). If A. it's terse, B. it's structured, C. it's readable, would that be better?

We primarily use switch because it's easy to read (versus a ton of if{}/else{}), as it gives us almost-block scoping without having to define another indirection/function. Python has this switch equivalent in match/case, but you have to use Python to get it.

Switch use is a syntax problem. Lots of software is colored by bad syntax and language design^.

^ I think most people realize that type hierarchies are a brittle/limited form of mixin. Even Java has softened to this a bit (aspects, etc) but will likely never be fun to use.

Re:When are enums not a code smell? by bena on Monday November 22, 2021 @09:15AM (#29306484)
I'd never heard of the phrase "frog in a well" before. I like it, I don't think I ever had a term for that phenomenon before.
Re:When are enums not a code smell? by marcosdumay on Monday November 22, 2021 @09:32AM (#29306665)
It's a reference to a story about a group of frogs that adapt to living a hellish life in a well, and when given the means to escape decide to destroy it instead of escaping.

The story itself seems to be ancient, but I couldn't find where it originates from.

Re:When are enums not a code smell? by Jensson on Monday November 22, 2021 @10:26AM (#29307328)
I think it is originally from China, I see it a lot in Chinese stories and never seen it anywhere else (except here now).
Re:When are enums not a code smell? by marcosdumay on Monday November 22, 2021 @11:01AM (#29307828)
Hum... I know it from Portuguese literature.

I was able to find some references from ancient India, but nothing about it coming from there.

Re:When are enums not a code smell? by smegsicle on Monday November 22, 2021 @10:56AM (#29307754)
"Kupa manduka is a Sanskrit phrase for the frog in the well that imagines the well, its home, to be the whole world and, therefore, becomes pompous. It is a term of derision for the intellectually complacent, for someone who thinks he knows everything there is to know."
Re:When are enums not a code smell? by cle on Monday November 22, 2021 @08:59AM (#29306332)
Can you elaborate on your shallow dismissal?
Re:When are enums not a code smell? by flaviu1 on Monday November 22, 2021 @09:10AM (#29306426)
I'd expect it to go something like this:

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

Re:When are enums not a code smell? by oflannabhra on Monday November 22, 2021 @10:03AM (#29306989)
In Swift specifically, switch statements are exhaustive, so adding a new case to an enumeration that is switched on will automatically give you compilation errors at every point in the code base where that enum is matched against.

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

Re:When are enums not a code smell? by bryanrasmussen on Monday November 22, 2021 @10:04AM (#29307007)
>This is a long way from "enums are a code smell,"

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.

Re:When are enums not a code smell? by masklinn on Monday November 22, 2021 @09:52AM (#29306855)
> - "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

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

Re:When are enums not a code smell? by couchand on Monday November 22, 2021 @10:05AM (#29307023)
Generally the issue with the switches scattered about (or more generally, and concern about conditions scattered about the codebase) is that the body of each case is different. Of course if all the bodies are the same the easier option is a utility method.
Re:When are enums not a code smell? by aardvark179 on Monday November 22, 2021 @11:45AM (#29308428)
Switch statements are not required to be exhaustive, but switch expressions are. I’d have to check but I think it was found that changing the exhaustiveness requirement would break too much source code. The compiler will still insert some some sort of default case because somebody might add to the enumerator, and it might be in a separate compilation unit.
Re:When are enums not a code smell? by jaywalk on Monday November 22, 2021 @10:47AM (#29307637)
As someone who works in C# and finds enums useful, I'd be curious to know why you describe them as literally useless.
Re:When are enums not a code smell? by masklinn on Monday November 22, 2021 @11:09AM (#29307925)
A C# enum is, like a C enum (as they were explicitly introduced to be compatible with those), just a bunch of constants for integers.

So when you have an enum-typed value, odds are good that it’s one of the named ones but there’s no mechanism anywhere preventing it to be any other integer of the underlying type.

Re:When are enums not a code smell? by Semaphor on Monday November 22, 2021 @11:44AM (#29308423)
But how is that an issue? I guess when you are casting random integer to the enum-type without any checks?
Re:When are enums not a code smell? by masklinn on Monday November 22, 2021 @12:00PM (#29308616)
> But how is that an issue? I guess when you are casting random integer to the enum-type without any checks?

Any caller can send any garbage (if you're publishing a package / API), likewise a dependency can return any garbage, etc... C#'s enums are entirely indicative.

Re:When are enums not a code smell? by Semaphor on Tuesday November 23, 2021 @02:30AM (#29315444)
Then you have the default path, throwing an exception. I don’t see how that is different from accessing an array with an OOB index.
Re:When are enums not a code smell? by dtech on Monday November 22, 2021 @09:35AM (#29306679)
This might have been valid in 00's Java/C#. However, most modern languages and even Java 16+ have good Enum support and exhaustiveness checks, making them very convenient and safe to express a limited set of possible values.

In contrast, an alternative like polymorphism + a visitor is safe, but excruciatingly verbose and hard to follow and modify.

Re:When are enums not a code smell? by strulovich on Monday November 22, 2021 @09:14AM (#29306464)
For me it’s the following: - going for polymorphic classes instead of a few switches can cause bad coupling of knowledge. ( For example, instead of a switch on what to log in module A and the same for module B, now you’re mixing code from these two modules due to a shared constant.

Sometimes these polymorphic classes make sense, but my experience is that it’s a minority, and it’s better to err on the side of some simple enums.

Also note that Uncle Bob in Clean Code pretty much directly opposes in one of the chapters about logic and data separation (can’t find the chapter right now since I don’t have the book anymore)

Re:When are enums not a code smell? by mannykannot on Monday November 22, 2021 @10:39AM (#29307530)
Fivelessminutes's dismissal is indeed shallow, but then again, the rule being dismissed looks like shallow dogma (though we are not seeing it in context.)

Enums are essentially categorical variables, and there is nothing wrong with that: they are widely used in statistics and other real-world applications (arguably, even, booleans are a case of such.)

As a junior programmer, I was once assigned to work on an example of what could happen as a result of following this advice: an explosion of subclasses for the sake of avoiding a few conditional clauses. Algorithms had been broken up so that the common parts could be put in base classes, and you had to jump all around the code to see what was going on. Worse, objects could change their categorization during their lifetime, which required the substitution of a new object for the previous one. This complexity was contagious; once this one type was implemented in this manner, a lot of other things had to follow suit.

This was one of the experiences that turned me into a skeptic with regard to the proposition that there's no problem that cannot be simply solved by being more object-oriented.

Re:When are enums not a code smell? by kaetemi on Monday November 22, 2021 @10:04AM (#29307010)
The OO story generally falls apart once you go outside of the module/library, and want to actually treat objects like generic objects, and have new custom behaviour for some types of objects.

Either you end up with 'scary' enums, or with dynamic_cast and 'scary' null pointers.

Re:When are enums not a code smell? by coldacid on Monday November 22, 2021 @09:47AM (#29306797)
Mark Seemann started it first.
Re:When are enums not a code smell? by AndrewBissell on Monday November 22, 2021 @09:44AM (#29306764)
Somehow I knew it must have been a dependency injection advocate who came up with the idea that enums are a code smell.
Re:When are enums not a code smell? by quickthrower2 on Monday November 22, 2021 @09:58AM (#29306923)
The more insidious problem is the card carrying believer in X saying “!X is harmful”

See this pattern all the time in both technical and people side all the time.

“You should never estimate using time”

“Don’t start new work until all the teams sprint commitments are completed”

Etc.

In a word it is arrogance with a twist of ignorance.

Ignorance that complex systems of humans and code don’t follow perfect dogmatic rules.

It might be ok for junior devs to be a bit like this but always add a pinch of “most of the time, but when experienced you’ll know when to ignore the rule”

Ignoring some unwritten or written dogma is often what makes for a competitive advantage.

Let your competitor drown in EnumClassFactorys .

Re:When are enums not a code smell? by AndrewBissell on Monday November 22, 2021 @12:08PM (#29308722)
Every one of these language toolbox books should come with a disclaimer for new devs up front about how the mark of a good craftsmen is knowing when to use the tools, when to bend or break their rules and guidelines, etc. I know it certainly might have saved me a fair bit of grief and oddly written code.
Re:When are enums not a code smell? by userbinator on Monday November 22, 2021 @03:47PM (#29311421)
But then they wouldn't sound "authoritative" enough. The dogma-cult that keeps its leader well-fed wouldn't survive.

I think the best authoritative-sounding counter-quip is "Best practices are best not practiced."