I’ve noticed some strange coding styles in PHP/Wordpress code:
1) Returns at start of function:
I’m surprised how often I see PHP/Wordpress code that returns at the start of a function. For example:
function dosomething() {
if (!current_user_can($priv)) {
return;
}
.... continued code
is used instead of what is usually considered good practice, that is, rarely having multiple returns (they are basically gotos):
function dosomething() {
if (current_user_can($priv)) {
... do something
}
... any finishing code
}
2) Lack of brackets in if’s
In most other languages, the single-line if/else statement without brackets is considered messy/dangerous because additional lines may look like they will run when they actually don’t. I’ve seen this used many times in PHP/Wordpress however.
I’m wondering if these different styles have performance benefits, or other specific reasons for their use?
Neither are performance related. The first just guarantees that nothing in the function can be processed by a user who isn’t authorized to do so (to make sure they don’t run “… any finishing code” in your example), and the second is a style preference for simple if/else statements.