News for hackers. Stuff that matters.
Re:There’s No Such Thing as Clean Code by raverbashing on Friday January 28, 2022 @03:41AM (#30112188)
> is a set of rules that absolutely are not at odds with one another

Yes, if you take them as general guidelines, not dogmatic rules with variable interpretations

The latter is exactly what his fans do

> 2 .Keep it simple stupid

> 2. Prefer polymorphism to if/else or switch/case.

No, not at odds with one another...

Re:There’s No Such Thing as Clean Code by beebmam on Friday January 28, 2022 @03:46AM (#30112208)
Yeah, polymorphism is in no way simple in lots of languages/frameworks. It's actually quite complex in many common languages/frameworks. This is absolutely contradictory in my opinion, but I welcome disagreement :)
Re:There’s No Such Thing as Clean Code by mcv on Friday January 28, 2022 @04:51AM (#30112631)
And then there's a language like Scala where switch/case is so naturally integrated that it actually becomes the cleaner solution and you basically have to use it.

Whatever approach is clean or not can vary wildly by language. Or framework.

I do like clean code, but I think the article has a point: it's a very vague concept.

Re:There’s No Such Thing as Clean Code by hyperman1 on Friday January 28, 2022 @04:11AM (#30112373)
I see polymorphism vs if/... more as a tradeoff. For example:

Suppose the program has to make a choice. When the choice exists only in 1 place, use an if/switch. When the choice exists in multiple places, polymorphism centralizes it and quickly becomes the simple option.

For example, a change in law required us to make a choice based on a date:

   If (thing.someBusinessDate < 2010-06-01) { doTheOldWay();} else{doTheNewWay();}
after a while, a few edge cases sprang up, and we got variants of this code hiding in multiple corners. Then the law changed again with a new cutoff date. So we adapted to polymorphism:

  interface TheWay{ edgecase1(); edgecase2();...}    
  class TheOldWay implements TheWay{...}
  class TheNewWay implements TheWay{...}
  class TheNewNewWay implements TheWay{...}
  TheWay wayDetector(thing){/* all the ifs here */}

Unfortunately, adding just one more 'if' was always the cheapest choice. The refactoring was nevertheless clearly a better choice: it enabled us to point out more forgotten edge cases, avoiding costly mistakes.
Re:There’s No Such Thing as Clean Code by viceroyalbean on Friday January 28, 2022 @04:24AM (#30112464)
I agree with you, especially in instances where that decision logic is unrelated to the data that is actually being used. In your example you'd have to pass "thing" everywhere just for that if-statement, but it might not be used for anything else.

That said, I think "simple" if/else or switches are much more common than the complex cases that fit polymorphism, so it seems a bit off to have it as a general rule to prefer polymorphism.

Re:There’s No Such Thing as Clean Code by juped on Friday January 28, 2022 @03:48AM (#30112215)
They aren't. This is why pattern matching and typeclasses (to use the Haskell words, though I know other words are in use as well) have become increasingly popular features in languages.
Re:There’s No Such Thing as Clean Code by onion2k on Friday January 28, 2022 @03:59AM (#30112289)
Polymorphism is more complex than a switch/case in a single instance. Across a large codebase using polymorphism instead of hundreds of different switch/cases is simpler, especially when you need to change something. Encapsulating what a class should do within the class instead of in an external code block is much simpler when you're working on something big.
Re:There’s No Such Thing as Clean Code by fpoling on Friday January 28, 2022 @04:24AM (#30112469)
Polymorphism is a switch, but it is the switch over object kind, not action. In fact if there are only few subclasses compilers for some languages where a global analysis is possible implement polymorphic calls as switches over the object tag.

And notice how if one needs to add a new action, all objects are required to implement it. In typical OOP languages that touches many files even if the action is used only once.

So polymorphism works nicely if there are only few actions but many different objects, like in a typical GUI. But in cases of few objects and many actions, for example, doing a lot of different queries over few data sets it leads to a lot of boilerplate. Switch over actions will lead to less code.

Re:There’s No Such Thing as Clean Code by mpweiher on Friday January 28, 2022 @03:49AM (#30112218)
Hmm...polymorphism is almost invariably simpler than if/else or switch/case, so not seeing the contradiction.
Re:There’s No Such Thing as Clean Code by ajuc on Friday January 28, 2022 @03:56AM (#30112272)
I've seen this happening a lot:

- you have the same if-then-else in 3 places in code with 3 different effects

- you refactor it to polimorphism (Enum with a few overloaded methods works well in java)

- now one of these places need additional condition so you make it a little more complex

- year passes

- you have eldritch abomination of OOP instead of 3 if-then-elses with slightly different conditions because at no point someone was brave enough to refactor back into if-then-elses as the special cases piled up

Re:There’s No Such Thing as Clean Code by ziml77 on Friday January 28, 2022 @09:51AM (#30115287)
Exactly my experience with polymorphism. It starts out looking super clean but then the business requirements shift and each has its own edge cases that turn it into a giant mess, often ending up as a mix of both polymorphism and convoluted if statements.
Re:There’s No Such Thing as Clean Code by fortylove on Friday January 28, 2022 @03:54AM (#30112261)
Is that view not subjective though? Might someone think that if/else is simpler?
Re:There’s No Such Thing as Clean Code by qayxc on Friday January 28, 2022 @06:33AM (#30113373)
Depends entirely on the metric used.

Cyclomatic complexity would put if/else behind polymorphism while using lines of code and depth of inheritance would favour the if/else way.

That's the crux with software - there's not always an objectively better solution in terms readability/comprehensibility and some metrics that try to capture these qualities are inherently at odds with each other.

Re:There’s No Such Thing as Clean Code by rk06 on Friday January 28, 2022 @03:59AM (#30112294)
Polymorphism can replace multiple if/else/switch/case with a single one.

It has benefits, if you do not apply it dogmatically everywhere