News for hackers. Stuff that matters.
Re:“Clean Code, Horrible Performance” Discussion by tpoacher on Saturday March 11, 2023 @04:04AM (#35106721)
Clean code is defined as code that improves programmer efficiency and program readability, eschews premature optimisation by optimising for simplicity, makes code less complex and objectively better by optimising for readability, allowing virtually anyone to safely and easily change it when really needed.

So you're actually a clean code proponent.

As is usually the case with such debates, it is a debate of differing definitions masquerading as a debate of differing arguments.

Re:“Clean Code, Horrible Performance” Discussion by josephg on Saturday March 11, 2023 @04:11AM (#35106751)
> Clean code is defined as code that improves programmer efficiency and program readability, eschews premature optimisation by optimising for simplicity, makes code less complex and objectively better by optimising for readability, allowing virtually anyone to safely and easily change it when really needed.

I think plenty of people love clean code. I love programmer efficiency and readability. But there's clean code and "Clean Code". Some of the code examples from Robert C. Martin's Clean Code book are absolutely atrocious. I would be horrified to find anything so unnecessarily abstracted and overcomplicated in my codebase.

Its precisely because I'm all for readability and programmer efficiency that I find the recommendations in "Clean Code" so bizarre and abhorrent.

Re:“Clean Code, Horrible Performance” Discussion by dsego on Saturday March 11, 2023 @05:55AM (#35107213)
His examples use class state as faux-global state and dozens of small one-line methods that "do one thing", but don't really, since they operate on this object state. In the end, it makes it difficult to follow the logic flow, since you have to jump around from one small method to the other to understand what's happening. And clear understandable method naming is of limited help, so it turns into this ravioli code anti-pattern, clean on the surface, but semantically all tangled up.
Re:“Clean Code, Horrible Performance” Discussion by pkolaczk on Saturday March 11, 2023 @04:06AM (#35106728)
Ok. Fair enough. I just find that what Uncle Bob calls clean code is often not clean code for me. E.g. preferring inheritance over switch is less readable. Flexibility/extensibility and readability are different things.
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.

Re:“Clean Code, Horrible Performance” Discussion by mejutoco on Saturday March 11, 2023 @09:31AM (#35108529)
Agree. Inheritance is a code smell. And clean code does not smell :)
Re:“Clean Code, Horrible Performance” Discussion by jdlshore on Saturday March 11, 2023 @01:40PM (#35111218)
Polymorphism is useful when switch statements are repeated, as a way of eliminating that duplication, not when there’s a single well-encapsulated switch statement.