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.
no, this is not a templating engine, this is just PHP making things simple for us.
insert obligatory "But PHP is a templating engine" remark here. It hasn't been used like one in years, but originally it was a templating engine, which is why we can do this, and why to this day every php file has to start with <?php
Yes, PHP is a very fun programming language, this one of my last which I started to learn, the most favorite by far, and nice article by the way, I enjoyed reading it. These tips are very useful, because a lot of beginners get stuck in situations like this one, and have no idea what to do. I became a fan of coding, after I got A/B testing service getparthenon.com, for my website on which I was working. I was a beginner at that moment and know almost nothing, and the guys helped me to set up everything and get it running.
The first tip is a really important one, although, with that particular example, with something so simple, a ternary would be more appropriate.
functionoutput_gender(bool$user_is_male){return$user_is_male?"User is male":"User is female";}
Also, one more tip related to this example: careful with how you name stuff. This function is called output_gender, but it doesn't actually output anything. It simply returns a string indicating the gender. Only call something output, or print if it actually does that.
I also prefer camelCase, but that's just a personal preference, and doesn't really affect readability or anything.
There's also a whole other discussion about whether it's appropriate to make assumptions about binary gender in your code. But that's not exactly the point of this discussion.
Programming is easy like riding a bike. Except the bike is on fire and you're on fire and everything is on fire and you're actually in hell (@matixmatix)
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 ;)
insert obligatory "But PHP is a templating engine" remark here. It hasn't been used like one in years, but originally it was a templating engine, which is why we can do this, and why to this day every php file has to start with
<?phpπππ Good one. However, PHP has moved on from that little extension it used to be.
Yes, PHP is a very fun programming language, this one of my last which I started to learn, the most favorite by far, and nice article by the way, I enjoyed reading it. These tips are very useful, because a lot of beginners get stuck in situations like this one, and have no idea what to do. I became a fan of coding, after I got A/B testing service getparthenon.com, for my website on which I was working. I was a beginner at that moment and know almost nothing, and the guys helped me to set up everything and get it running.
The first tip is a really important one, although, with that particular example, with something so simple, a ternary would be more appropriate.
Also, one more tip related to this example: careful with how you name stuff. This function is called
output_gender, but it doesn't actually output anything. It simply returns a string indicating the gender. Only call somethingoutput, orprintif it actually does that.All in all, I would rewrite this function as:
I also prefer camelCase, but that's just a personal preference, and doesn't really affect readability or anything.
There's also a whole other discussion about whether it's appropriate to make assumptions about binary gender in your code. But that's not exactly the point of this discussion.
Thanksπ I agree that ternary would be better for that example, but I was just trying to make a point. A little secret, I prefer camel case tooπ€
Loved It
Great tips. Somo of them I already use and the others I'll try to use for now on.
Very nice article! I didnβt know about the endif, thanks!
Interesting read and great article π
Thanksπ
Nice one!!
nice article! I think this is enough for my dinner haha...