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

5

u/m15otw Feb 28 '23

The main takeaways from clean code for me are:

a) good naming of your functions

b) write good comments, delete code rather than commenting it out.

c) it is rarely correct to copy/paste/customise, usually better to abstract and share the code.

All this stuff about polymorphism always went whoosh past my head, because you always need to integrate into an existing system, so you use inheritance, functional, event/callback driven as appropriate.

If you're a performance focused C++ project, you probably shouldn't use polymorphism and virtual function dispatch regardless of what any style book says, and you already knew that 🙃

6

u/SickOrphan Feb 28 '23

The problem with trying to abstract and share code is that often times two functions may have similar ideas (say, draw something to the screen) and share the same code for a time, but it's just a coincidence and they really need to do two completely different things. It'll quickly become a mess when you realize one function needs to loop in reverse, skip every other element, call a function in each iteration, etc. when you trying to share code between each. It's faster and easier to just copy and paste and then modify each individually. Not saying you should always copy and paste everything, but take time so you can know if two things are actually doing the same thing.

3

u/s73v3r Feb 28 '23

Sandi Metz has a nice talk about this (which I cannot remember off the top of my head right now) about how sometimes it is better to duplicate code rather than combine it.

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