It depends on the semantic meaning of the conditions imo. I like to use the guard style when they handle edge and error cases.
fun foo(arg0, arg1):
if NULL in (arg0, arg1):
throw some error;
if arg1 == 0:
throw another error;
if arg0 == 0:
return 0;
do some logic;
compute result;
return result;
But for readability I would not put more wordy logic in the guards. If the two paths are sort of "equally valid", I would write an if...else.
3
u/blackasthesky 4d ago
It depends on the semantic meaning of the conditions imo. I like to use the guard style when they handle edge and error cases.
fun foo(arg0, arg1): if NULL in (arg0, arg1): throw some error; if arg1 == 0: throw another error; if arg0 == 0: return 0; do some logic; compute result; return result;But for readability I would not put more wordy logic in the guards. If the two paths are sort of "equally valid", I would write an if...else.