DEV Community

Discussion on: PHP Tips and Tricks

Collapse
 
phantas0s profile image
Matthieu Cneude

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.

Collapse
 
nicolus profile image
Nicolas Bailly

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.

Collapse
 
leob profile image
leob

"Early return" FTW ;)