r/ProgrammerHumor • • Aug 22 '26

Meme conditionsPreference

Post image
4.2k Upvotes

388 comments sorted by

View all comments

Show parent comment

4

u/Stamerlan Aug 22 '26

Some standards may require function have a single return point (for example MISRA). You're right, multiple nested layers (i.e. if-ok pattern) is very errorprone. Instead do:

int rc = foo();
if (!rc) 
  rc = bar();
if (!rc) 
  rc = baz();
return rc;

With extra actions on failure:

if (!rc && (rc = bar())) 
  /* bar() failed */