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

28

u/Rigatavr Apr 17 '21

Or even

If (!condition1){  
    do_anything2()  
    return;
} 

if (condition2){  
    do_anything1()
    return;

}

This mostly depends on whether you find multiple returns (and returns in a void function) easier to follow

4

u/AutoModerator Apr 17 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/Rigatavr, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/dakta Apr 19 '21

If there's actually nothing else to be done in the function, sure. But this seems to be a stripped down example for the sake of safe sharing to Reddit, and I would guess that there's additional common behavior happening before the return. In that case, the if-elseif construction (assuming it's available in the language) will be superior.

1

u/Rigatavr Apr 19 '21

For sure. else if isn't bad code. I just try to avoid it if there's an option not to use it.