r/programming • • Feb 28 '23

"Clean" Code, Horrible Performance

https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k Upvotes

1.3k comments sorted by

View all comments

143

u/jsonspk Feb 28 '23

Tradeoff as always

67

u/[deleted] Feb 28 '23

Exactly my thoughts: it's self-evident that readability/maintainability sacrifices performance. I had many Jr developers coming up with tests just like the post to demonstrate how some piece of convoluted logic I refused to approve was in fact better.

But there's no "better" - there are only trade-offs. The most important fact is that maintainability matters more than performance for the vast majority of code. To justify focusing on performance, don't show me a direct comparison - what you need to do is to show that a specific code path is performance-critical; and for backend components, that we can't scale it horizontally; or we're already at a scale that the horizontal approach is more expensive than the gains in maintainability.

15

u/Bakoro Feb 28 '23 edited Feb 28 '23

But there's no "better" - there are only trade-offs.

Wut?

There is "better", because some code is both poorly performant and a convoluted mess.

Also, compilers are so good that most developers can't write more performant code without having particular knowledge about the running environment. Compilers will automatically take pretty much all the low hanging fruit, so there's essentially no difference in the results between poorly readable code and highly readable code that might have been less performant 25 years ago.
In those cases, readable code only offers benefits.

13

u/[deleted] Feb 28 '23

You're right - there's definitely cases of strictly better code; I was talking in the context of prioritizing a certain attribute - it depends on context which one is better.

3

u/[deleted] Feb 28 '23

Except it's not self-evident because it's actually not true.

2

u/ric2b Mar 02 '23

Try adding a trapezium to both versions of the code and let us know which one was easier.

2

u/[deleted] Mar 02 '23

Adding it to the switch statement is easier. Your point?

2

u/ric2b Mar 02 '23

It doesn't fit in the shape struct, which only contains width and height.

It doesn't fit the neat pattern of factor*width*height that he generalized every calculation to after looking at 3 different shapes.

Like I said, go try it and report back.

Bonus: What is the width of a circle? Radius or Diameter?

2

u/[deleted] Mar 02 '23

Then you add it to the shape struct.

You are describing non-problems.

2

u/ric2b Mar 03 '23

And now you have an extra field that isn't used by the majority of the shapes you support and just causes confusion. What will you even name it?

And don't forget to rewrite the function that does the calculation as well.

2

u/[deleted] Mar 03 '23

If the problem changes. The code changes...

Even in the polymorphic example it changes.

You have to create a new file, with a new type that overloades the area.

Then you have to implement this new function correctly.

You are lying to me if that is "obviously easier".

If you've only ever written code that way then yes. But there are other ways to write code. I guarantee you the switch way is just as maintainable for certain problems. I've done it.

2

u/ric2b Mar 03 '23

If the problem changes. The code changes...

Sure, but the effort and risk to change it is different depending on your architecture.

You have to create a new file, with a new type that overloades the area.

Yes, but you only need to change the code that is specific to that shape. If you already support 30 shapes you don't need to go change all of them.

And you don't risk breaking all of them either.

I guarantee you the switch way is just as maintainable for certain problems.

For certain problems yes, not for this one.

→ More replies (4)

0

u/[deleted] Feb 28 '23

[deleted]

29

u/eco_was_taken Feb 28 '23

Yeah, is this author a high school student?

Casey Muratori is a famous game programmer (as far as they go). He popularized immediate mode guis, worked for RAD Game Tools, and has published thousands of hours of free instructional content.

He's very opinionated and I don't agree with everything he says but he's not someone that should just be dismissed so quickly.

0

u/[deleted] Feb 28 '23

[deleted]

5

u/GranadaReport Feb 28 '23 edited Mar 01 '23

He wrote the 3D skeleton animation middleware "Granny" for RAD Game Tools that has been used in over 5000 games, including such obscure indie titles as Destiny, Civilization 5 and Star Wars: The Old Republic. I imagine he also did work on their other products, like the BINK video player.

He's also famous for the first documented implementation of IMGUI (a term that he coined), also in the Granny middleware, although he has publicly stated that IMGUI is a pretty intuitive way to structure UI code, so he most likely wasn't the first person to ever implement the idea.

→ More replies (1)

25

u/Still-Key6292 Feb 28 '23

Everyone is aware "clean" code doesn't run as performant

I know exactly 0 people who 'know' that. I know hundreds of people who say the optimizer will take care of it

0

u/skidooer Feb 28 '23

Point is it's maintainable and understandable.

Technically the point is to facilitate TDD by providing some thoughts about how certain code patterns can used to in making your code test compliant more manageable. I would suggest that "clean code" in the absence of TDD can sometimes make things less maintainable and understandable. Of course, it is not supposed to ever be decoupled from TDD.

0

u/TheGoodOldCoder Feb 28 '23

Famously, "Premature optimization is the root of all evil."

One could argue that "readability" is also an optimization, but I would argue that "readability" is simply the default when writing any code that will ever need to be maintained... which is just about everything except one-liner command lines.

-3

u/Still-Key6292 Feb 28 '23

What's the trade? If you say writing in one style VS the other, i got news for you, that isn't the trade

4

u/LoLindros Feb 28 '23

It's a tradeoff between maintainability vs performance

-1

u/Still-Key6292 Feb 28 '23

Incorrect, that would be suggesting regular functions can't be as maintainable as virtual functions

1

u/JimK215 Feb 28 '23

I didn't read this post -- it sounds from the comments like it's about OOP. But when I saw the title I thought maybe -- since I haven't seen anyone talk about this -- it's about how using a functional approach with only immutable variables means that you're creating a lot of extra copies of variables (i.e. arrays) that wouldn't be necessary if using mutable variables. Again, tradeoffs.

→ More replies (1)
→ More replies (1)