Interesting way to check statuses returned by functions

If there is a set of bool functions, then sometimes below trick may be used to make the code that checks those values shorter:

bool status = true;
   
status &= foo_1();
status &= foo_2();
status &= foo_1();

printf(status ? "status: OK\n" : "status: NOK\n");

2 comments:

  1. Interesting, but in most of the real code you shouldn't execute (above) second and third method if the first failed. In addition, it's creepy and obscure ;)

    ReplyDelete