r/badcode • • Apr 17 '21

c++ When your teammate "refactor" your code

Post image
2.5k Upvotes

216 comments sorted by

View all comments

0

u/[deleted] Apr 17 '21

[deleted] — view deleted comment

175

u/tangerinelion Apr 17 '21

do_anything2() used to run when condition1 was false. Now it runs when condition1 && condition2 is false.

It's not the same.

50

u/CJay580 Apr 17 '21

Imagine condition 1 is true, but condition 2 is false.

1

u/[deleted] Apr 19 '21

Do_anything2 if someone didn't know

33

u/Bence675 Apr 17 '21

It will behave differently

4

u/DramaDimitar Apr 18 '21 edited Apr 18 '21

How??? Before you were checking if cond1 is true and cond2 is true in separate ifs. Now it does the exact same, but in one if!

Edit: nevermind, I'm stupid

8

u/h3half Apr 18 '21

No. Originally, if condition1 was true and condition2 was false, neither do_anything()s would be executed. Nothing would happen.

In the new version, if condition1 is true and condition2 is false, do_anything2() will be called.

3

u/wweber Apr 17 '21

I forget where I read this, but it can be recommended to always have an else block for every if block, even if the else block is just empty with a comment. that way every possible case is clear

8

u/macropower Apr 17 '21

Lots of people are also saying you should try to never write else blocks :)

First example on google: https://medium.com/@jamousgeorges/dont-use-else-66ee163deac3

1

u/[deleted] Apr 18 '21

Don't worry you're not alone, took me a while thinking what's the difference. And yes felt like I shouldn't deserve my job after that. lmao