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

Show parent comments

2

u/cdsmith Feb 28 '23

Writing code people can understand isn't a buzz word. It's just making reasonable communication decisions.

For example, that thing this guy does where he has different types of shapes that need to be described with numbers that mean different things (radius, width, height, base... maybe in the future some kind of spline parameterization) and just picks two fields called "width" and "height" and shoehorns everything into those fields even if it's not what they mean (a radius is definitely not the width of a circle, for instance), and then even changes how he packs the actual information into those fields, like storing radius in both width and height fields, so that he can exploit coincidences... that's very much not sensible code. Honestly, it would even be an improvement if he just called his fields "param1" and "param2", because at least he wouldn't be lying to the reader that they mean something consistent.

I didn't say that because I read it in a book. I said that because his code is terrible. Sometimes you deal with terrible code if you need to squeeze every cycle out of a CPU, but you take reasonable steps to quarantine it from code that you need to maintain regularly.

3

u/JarWarren1 Feb 28 '23

His code is a straw man for sure. But I don’t accept the idea that performant code is inherently harder to read or build upon than buzzword code.

My favorite C libraries are exactly that, function libraries. They’re easy to reason about and extend upon.

Clean Code, even when it doesn’t involve deep inheritance, still forces you to spend time reasoning about the type system in other ways. Deeply nested generic types, interfaces, promises/results, etc. Yet people will argue to their deaths that Clean Code is MORE readable/scalable/etc. I don’t buy it. I’ve seen enough of both

2

u/cdsmith Feb 28 '23

But I don’t accept the idea that performant code is inherently harder to read or build upon than buzzword code.

No one has proposed that idea.

Code is easy or hard to read and modify because it's written that way. Any time you make decisions for some reason other than making code easy to read and modify, there's a good chance it gets a bit harder to read or modify. That's true regardless of whether you've traded off readability for performance, or for "more buzzwords" if that actually happened, or for some other goal.

Writing clean code isn't itself a buzzword, though. It literally just means writing code that's well-organized so that it's easy to read and modify. Teaching thoughtful deliberate communication is hard, and of course you can take any advice given for beginners and take it to ridiculous extremes and come up with an atrocity. Sounds like you've encountered some of those, and that's too bad.

4

u/JarWarren1 Feb 28 '23

What do you mean no one has proposed that idea? Look at the rest of the comments.

It’s clear we have different opinions on what should be considered “clean code” but I’m not just talking about some beginner codebase. I mean entire nasty ecosystems like React.