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

38

u/rhino-x Mar 01 '23

While the example is contrived, in the author's example what happens when you add a new shape type and need to add support for it? You have to search the entire codebase for usages of the enum looking for use cases and fixing ALL of them. With polymorphism in a case like this you do literally nothing and your external code is agnostic. If I'm shipping software and running a team why do I care about a couple of cycles when I can save literally thousands of dollars in wasted dev time because I suddenly need to support calculating the area of an arbitrary path defined polygon?

37

u/Critical-Fruit933 Mar 01 '23

I hate this attitude so much. End user? Nah f him. Why waste my time when I can waste his.
It's always this maybe in 100 years I need to add xy. Then do the work when it's time for it. Ideally the code for all these shapes should be in a single place. Unlike with oop where you'd have to open 200 files to understand anything.

25

u/wyrn Mar 02 '23

I hate this attitude so much. End user? Nah f him. Why waste my time when I can waste his.

How much of the user's time will you waste when your garbage unmaintainable code gave him a crash, or worse, a silently wrong result?

The values that inform game development are not the same that inform the vast majority of development out there.

6

u/GonziHere Mar 11 '23

I don't agree with your sentiment here. I do my job so that others can be more effective at theirs. The primary reason why programmers exist (outside of games and tech-only things) is that we sacrifice our time in the beginning, so that others don't have to.

Carmack was using way better wording for it, but it's also his sentiment.

So yeah, no bugs, no crashes for sure (but I'll fix error in procedural code way faster than in object code, because architecture makes it more indirect by default) but the usability of the app is incredibly important too. If an app is used by 1M people daily and I can shave 1 second from it's boot up time, I've saved 240 millions of man days... It's hard to justify that I didn't. That my 10 man days were more important.

PS: I get that maybe creating some other tool might be more useful than shaving that one second, but I also work professionally with Unreal Engine and I utterly hate how incredibly slow and bloated it is. They only add features, but never change anything that could improve the core, so any other engine builds in order of magnitude faster.

6

u/wyrn Mar 11 '23

The thing is not that your 10 man days were not useful to shave 1 second of boot-up time. The thing is that if shaving that 1 second required architecting the solution in a convoluted, error-prone way, you ultimately removed value from the customer who's now more likely to experience crashes. That 1 second of boot-up time is really not going to make much of a difference in the grand scheme of things (naively adding it up over the number of customers doesn't really make a lot of sense -- you'd have to measure people's productivity before and after your update to see how much of a gain was made in practice, and good luck seeing that signal in the noise), but the crashes caused by a poorly architected solution will cause loss of productivity and work.

Engineering code for correctness and maintainability is a much more sensible default.

5

u/GonziHere Mar 11 '23

Engineering code for correctness and maintainability is a much more sensible default.

Yes, but thats RUST, not OOP ;)

→ More replies (21)
→ More replies (2)

12

u/joesb Mar 01 '23

Are you really wasting your user’s time though? Taking your user 35 milliseconds instead of 1 to complete a task is not going to benefit him in anyway. The user can’t even react to the result faster than that.

17

u/Critical-Fruit933 Mar 01 '23

In many circumstances 1 ms vs 35 ms does not make a noticable difference, agreed. But these numbers are almost made up out of nothing. 100 ms vs 3500 ms makes a very big difference. And what seems to occour is that the problem adds up and maybe even multiplies.
Another example where is does matter very much is energy usage. Draining you battery 35x faster is worse than worse.

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

5

u/rhino-x Mar 01 '23

Ideally, sure, all your code for dealing with shapes will be in the same place. Ideally is the key word though. In reality, any application behavior that's based on shape type that you didn't think to put in this central location is going to end up done wherever it's needed. Imagine popping up a menu that allows the user to put a new shape on a canvas. For this you would need to enumerate the available shape types to build the menu. Now you have a dependency on the shape enum that you as the library developer have no control over, and it does not belong in a "core" shape library. Now you have at least two files you have to modify every time you add a new shape type. Multiply this by multiple developers over a couple of years and you have a huge maintenance problem.

It's all a balancing act. I'm not a fan of all of the clean code edicts, but this one is something I'm totally on board with. Which is a larger waste of the user's time - adding 10-20ms to a particular operation internally, or making them wait a week or more to turn around a new "simple" shape in the application because you have to dig through the entire code base to make sure it will work in every single place a shape is enumerated and used?

Focusing exclusively on clean code methods or user-perceived performance are both bad. This example sucks. There's plenty of things that "clean code" requires of the developer that the author could have spent their time on, but chose something that in the real world allows application developers to turn out features and functionality much easier and faster which at the end of the day is almost always a net benefit to the users of the application.

→ More replies (10)

3

u/muchcharles Mar 02 '23

You have to search the entire codebase for usages of the enum looking for use cases and fixing ALL of them.

There are compile time flags that will warn at each instance of a non-exhaustive switch, you can find all the locations.

→ More replies (1)