Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing simpler conditions #51

Closed
justintadlock opened this issue Mar 6, 2017 · 1 comment
Closed

Writing simpler conditions #51

justintadlock opened this issue Mar 6, 2017 · 1 comment

Comments

@justintadlock
Copy link

I'm looking over includes/template-tags/helpers.php right now and see a lot of if/else code that looks like this:

if(is_object($tour_operator) && isset($tour_operator->options[$post_type]) && isset($tour_operator->options[$post_type]['disable_single'])){
	return true;
}else{
	return false;
}

That's perfectly valid code. However, it's more complicated than it needs to be. Remember that conditionals are going to return either true or false. There's no need for the if or else. You merely need one line of code to do the above.

return is_object( $tour_operator ) && isset( $tour_operator->options[$post_type] ) && isset( $tour_operator->options[$post_type]['disable_single'] );

Here's a different type of example from the same file:

if(count($children) > 0){
	return $children;
}else{
	return false;
}

Write it as (variable always goes on the right, by the way):

if ( 0 < count( $children ) ) {
	return $children;
}

return false;

There's no need for the else statement there.

A good rule of thumb in programming is that if you have an else in your code, you most likely don't need it.

I didn't want to point out any specific line numbers and files. This sort of thing is done quite a bit in the plugin. It's just extra, unnecessary code. When working with larger plugins, the less code, the better.

It's not something I'd try to fix all at once. Just keep this tip in mind while working on the plugin. If you come across something, fix it then.

@ashleyshaw ashleyshaw modified the milestone: 1.1 Apr 20, 2017
@ashleyshaw ashleyshaw modified the milestones: 1.1, 1.4 Sep 12, 2019
@ashleyshaw ashleyshaw added this to To do in Tour Operator 1.5 Sep 12, 2019
@ashleyshaw ashleyshaw removed this from To do in Tour Operator 1.5 Sep 13, 2019
@github-actions
Copy link

Stale issue message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants