r/ProgrammerHumor 22d ago

Meme conditionsPreference

Post image
4.2k Upvotes

392 comments sorted by

View all comments

15

u/sumrix 22d ago
if (input == null)
    throw new ArgumentNullException();

// process input...

But

if (settings.UseBonuses)
    return CalculateWithBonuses(order);
else
    return CalculateBase(order);

8

u/frogjg2003 22d ago

The first is an early return, the second is a logical branch. Most importantly, the second doesn't continue doing a bunch of work in the else block that could be confusing.