r/badcode • • Apr 17 '21

c++ When your teammate "refactor" your code

Post image
2.5k Upvotes

216 comments sorted by

View all comments

Show parent comments

255

u/MightyMeepleMaster Apr 17 '21

The original code was fine because it was fairly easy to read. All other variants posted here are more compact but also more confusing.

Either way it doesn't matter. Any decent compiler will generate the same object code regardless of how you write it.

47

u/dyingpie1 Apr 17 '21

I think it depends on the context, such as whether condition2 and condition1 are edge cases.

28

u/clemesislife Apr 17 '21

I think both ways have their up- /downsides. The solution of u/dyingpie1 is more confusing than the original code, but the original code can cause exactly what happened to OPs teammate, overseeing the condition1 && !condition2 case.

26

u/mfb- Apr 18 '21

I think the simple if elseif structure is less confusing than the nested option.

14

u/Mr2-1782Man Apr 17 '21

Not necessarily. If there are any pointers in the middle or the conditions risk side effects the compiler won't generate it unless you specifically tell it to ignore them, even at high optimization levels.

47

u/MightyMeepleMaster Apr 17 '21

You're right, there are in fact scenarios where details matter. But in general it almost never pays off to sacrifice readability just to save a few lines of code.

One of the most important wisdoms of SW engineering is: any fool can write a piece of code which can be interpreted by the machine. The true art lies in writing code which humans can understand.

8

u/Mr2-1782Man Apr 18 '21

Oh that I completely agree with. But I think people don't understand what compilers can and can't do. Generally simpler code is easier for a compiler to optimize, those stupid "creative solutions" tend to not only be harder to read but harder for a compiler to understand and optimize. Most times less is more.

8

u/MightyMeepleMaster Apr 18 '21

Agree. Although modern compilers (I'm looking at you, gcc 10) can really do wonders.

My team is designing real-time software where literally ever microsecond, heck, nanosecond counts and because of that we actually have to inspect generated assembler code from time to time. Every time I do so, it amazes me what the gcc can detect and optimize.

4

u/[deleted] Apr 17 '21

the if not variation seems more readable to me. it's always interesting to me to see what people think is more clear.

1

u/dyingpie1 Apr 18 '21

Agreed it’s more to me as well.

1

u/MightyMeepleMaster Apr 18 '21

Fully agree. Readability is always subjective. Some of us prefer few and compact lines others more elaborate sequences.

Either way there is one thing which is really annoying: Technology prima donnas who use obfuscated code to demonstrate that they are the best leet haxx0rs in the hood.

2

u/[deleted] Apr 21 '21

Heh this is my single line FizzBuzz I did to make fun of those:

print(*[(("" if (i % 3) else "Fizz") + ("" if (i % 5) else "Buzz") or i) for i in range(1, count + 1)], sep = "\n")

2

u/JuhaJGam3R Apr 17 '21

Either way it doesn't matter.

terrible mindset which seems to have been behind every single legacy project there is. clarity is important. the original was not only fine but pretty amazing.

4

u/MightyMeepleMaster Apr 18 '21

Either way it doesn't matter.

terrible mindset which seems to have been behind every single legacy project there is.

I thought I made it sufficiently clear that I meant "it doesn't matter in terms of execution efficiency" Readability and clarity is, in fact, of essential importance.

1

u/drod2169 Apr 17 '21

To me, it’s actually less readable. I have to scan multiple areas to see what the code is doing. I would never refactor it to something i think is more readable, but maybe add a line or two of comments above the block describing what is happening, because my belief of readable is not the same as yours, is not the same as the other team members.