News for hackers. Stuff that matters.
Re:“Clean Code, Horrible Performance” Discussion by goto11 on Saturday March 11, 2023 @04:13AM (#35106764)
> E.g. preferring inheritance over switch is less readable.

Surely this depends on the context? If you have a many different classes it tend to lead to cleaner code if you encapsulate the class-specific logic with the class definition rather than intermingled in multiple giant switch statements. In particular you can add new classes without making the rest of the code more complex.

Of course there are cases where a switch is the right choice.

Re:“Clean Code, Horrible Performance” Discussion by kubanczyk on Saturday March 11, 2023 @04:35AM (#35106856)
> Surely this depends on the context?

Yes, it depends on the context. In this case the context is the switch statement described in the https://www.computerenhance.com/p/clean-code-horrible-perfor.... Too bad TFA never mentions it explicitly (other than in the title and in the video). Recently it generated a heated discussion around here.

Re:“Clean Code, Horrible Performance” Discussion by pkolaczk on Saturday March 11, 2023 @04:22AM (#35106797)
Switch statements don't have to be giant. Hint: you can still extract each branch to a separate function / module. But what is more readable about them is the control flow: the condition is explicit and all targets are easy to find. A codebase using switches/ifs and function calls can be easily navigated with ctrl-click in most IDEs. A codebase relying heavily on interfaces and inheritance cannot.
Re:“Clean Code, Horrible Performance” Discussion by laserlight on Saturday March 11, 2023 @04:40AM (#35106880)
If you aren't going to use polymorphism to vary behavior, but depend on conditionals, what do you use classes for? Just for hierarchical data encapsulation?
Re:“Clean Code, Horrible Performance” Discussion by pkolaczk on Saturday March 11, 2023 @05:29AM (#35107094)
Surprise: I don't use classes. Rust doesn't have them. :P
Re:“Clean Code, Horrible Performance” Discussion by laserlight on Saturday March 11, 2023 @06:42AM (#35107463)
So, you’ve been arguing for using switch statements, instead of Clean Code’s suggestion of using object-oriented polymorphism, in a context where polymorphism doesn’t exist?
Re:“Clean Code, Horrible Performance” Discussion by pkolaczk on Saturday March 11, 2023 @07:34AM (#35107779)
Not at all. Not having classes is not the same as not having polymorphism. Rust supports polymorphism just fine (and one could argue its support for polymorphism is actually much more advanced than that of Java's; but that's not enough reason to use it everywhere).
Re:“Clean Code, Horrible Performance” Discussion by whstl on Saturday March 18, 2023 @08:45AM (#35208638)
The greatest trick OOP ever pulled was convincing the world that polymorphism didn't exist outside OOP.
Re:“Clean Code, Horrible Performance” Discussion by bart_spoon on Saturday March 11, 2023 @08:05PM (#35115063)
How are structs in Rust any different than classes?
Re:“Clean Code, Horrible Performance” Discussion by hdhrufjdi on Sunday March 12, 2023 @01:14AM (#35117276)
Behavior and data is more separated

A struct only contains data, however you e.g. can define methods on it or implement traits for it No inheritance.

Re:“Clean Code, Horrible Performance” Discussion by bart_spoon on Sunday March 12, 2023 @09:21AM (#35119756)
They don’t only contain data though, you can define methods as you said. Yes, there is no-inheritance, but they contain data, maintain state, and allow for defined methods. 90% of the time people are talking about classes, they are talking about that.
Re:“Clean Code, Horrible Performance” Discussion by goto11 on Sunday March 12, 2023 @05:30AM (#35118262)
I use multiple languages and I always consider the context of programming advice. I recommend a broadened perspective.
Re:“Clean Code, Horrible Performance” Discussion by dsego on Saturday March 11, 2023 @06:21AM (#35107347)
Afaik, data-oriented approach doesn't use classes to encapsulate behavior.
Re:“Clean Code, Horrible Performance” Discussion by goto11 on Saturday March 11, 2023 @04:34AM (#35106853)
So you are saying context doesn't matter, switch is always better?
Re:“Clean Code, Horrible Performance” Discussion by pkolaczk on Saturday March 11, 2023 @05:36AM (#35107121)
If we talk about readability - it is hard to think of a situation when inheritance would be better - it adds an indirection, it hides what's really going on, and at the end of the day the code is also longer and more complex.

However it has some other uses, e.g. when you really want to support adding new cases without modifying the code, e.g when doing a library, you need some kind of polymorphism.

So basically - it is not that abstractions/indirections are inherently bad, but they come at a cognitive cost. And it depends on the context if this cost is justifiable.