Coding for 20 years | Working for startups for 10 years | Team leader and mentor | More information about me: https://thevaluable.dev/page/about/
Twitter: @Cneude_Matthieu
Oh. An article about PHP. I thought Tailwind / React / JS were eating them all.
I've been advocating the first tip for years. Now, I can't see why it's better anymore. The if else make the code clearer in fact: it's less easy to miss than the return statement.
I think it just pleases our... sense of beauty? Code formatting is 99% subjective IMHO.
I keep going back and forth on the "return early" style.
The main advantage for me is that you get back 4 precious spaces at the beginning of every line in your "main" path. The disadvantage as you said is it's easier to see a return statement at the end of the function and miss the fact that it could have returned something else entirely.
At the end of the day, I guess if either of those is a real issue, it just means your method is too complicated and it's time to refactor by adding a new simpler method.
Oh. An article about PHP. I thought Tailwind / React / JS were eating them all.
I've been advocating the first tip for years. Now, I can't see why it's better anymore. The if else make the code clearer in fact: it's less easy to miss than the return statement.
I think it just pleases our... sense of beauty? Code formatting is 99% subjective IMHO.
I keep going back and forth on the "return early" style.
The main advantage for me is that you get back 4 precious spaces at the beginning of every line in your "main" path. The disadvantage as you said is it's easier to see a return statement at the end of the function and miss the fact that it could have returned something else entirely.
At the end of the day, I guess if either of those is a real issue, it just means your method is too complicated and it's time to refactor by adding a new simpler method.
"Early return" FTW ;)