As long as every branch returns then the 2nd version is fine with me.
(Although I'd still use brackets instead of guard clauses because of personal preference regarding readability)
It becomes a problem when the if does not return and gets more complex than a simple one liner.
I.e.
if(env.DEBUG){Logger.log(/* message */);}// Rest of logic here
would obviously be okay, but
letvar1;letvar2;letvar3;if(/* Some condition */){// Many lines// of code// that affect // the state of // the variables}// Rest of logic// working on the // variables unconditionally,// herereturnresult;
would not be okay, because it makes it really hard to reason about the output of the function and the logical flow of your data can't be parsed at a glance.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
As long as every branch returns then the 2nd version is fine with me.
(Although I'd still use brackets instead of guard clauses because of personal preference regarding readability)
It becomes a problem when the
if
does not return and gets more complex than a simple one liner.I.e.
would obviously be okay, but
would not be okay, because it makes it really hard to reason about the output of the function and the logical flow of your data can't be parsed at a glance.