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.
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
, orprint
if 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🤭