There is no code without if
statement. There is a high probability that within your first twenty lines of code you would have at least one if
statement. Over the past decade, I have been writing and reading code in C, PHP, and Javascript, and if
statements have always been the best part of every single code I have ever written. The solutions proposed in this post can be applied in any of those languages.
The thing is, somewhere, in some codes, the conditions become so complex and trickier that we blot the code with a whole bunch of if
s. Having many if-else
statements is not a sin, but it has some disadvantages in some circumstances such as code readability and speed.
In this short tutorial, let me show you a few tricks to shorten your if
and if-else
statements.
Trick #1
Sometimes you would love to return a boolean state based on a condition. This is how you can handle that:
<?php
// GOOD
if($condition == true){
return true;
}else{
return false;
}
// BATTER
return $condition;
The variable $condition
in the if
statement will definitely be evaluated to a boolean value. You can go ahead and return that state; so the outcome of the evaluation of $condition
will be returned. This makes your code short and faster.
Trick #2
In some case, we would love to toggle states. We would love to have a state changed based on a condition. Assuming you want to check if a user has provided the right credentials to access a service. If they do we change their connection level to valid
otherwise we set it to invalid
. Let's look at the code:
<?php
// GOOD
$status = null;
if($connection == true){
$status = 'valid';
}else{
$status = 'Invalid';
}
// BETTER
$status = 'invalid';
if($condition) // Trick #1
$status = 'valid';
Since we want to toggle two states we can initialize the $status
variable to the default state. We will only change it if only the condition is satisfied. This could save our code some time since it wouldn't have to do anything unless the status changes. And again, it looks cleaner and shorter.
Trick #3
Sometimes, we might need to check for more than one condition. This situation can push you to use if-else
and else-if
statements a lot. When you see that your if-else
statement goes beyond two think of a switch
instead. Let's look at an example:
<?php
// GOOD
$color = 'Pink';
if($color == 'Green'){
print "You love trees!";
}else if($color = 'Red'){
print "You are a fire spirit!";
}else if($color = 'Pink'){
print "Wow, you are cute!";
}else{
print "You must choose a color to play.";
}
// BETTER
$color = 'Pink';
switch($color){
case 'Green': print "You love trees!";
break;
case 'Red': print "You are a fire spirit!";
break;
case 'Pink': print "Wow, you are cute!";
break;
default:
print "You must choose a color to play.";
}
You can see that by implementing the switch
the code is simpler, readable, even to a person who might not know code. It can make your code grow properly without having to fall in a dust bin of cloudy if-else-else-if
statements.
Trick #4
You might have noticed that I wrote if($connection == true)
in the first and second tricks. This is something we see often in codes. It's actually a redondant condition. Here is how to make it simple:
<?php
// GOOD
if($connection == true){
// instructions
}
if($connection != true){
// instructions
}
// BETTER
if($connection)
// instruction
if(!$connection)
// instruction
The variable connection is internally evaluated as boolean type. If it's null, zero, or negative it will be evaluated as false and true otherwise. So we can lean on that to check the state. The same technique applies when you check again a zero.
<?php
if($status == 0)
// instruction
// DO THIS
// when we NOT 0 it will become 1, making it a true condition
// This is like, when we invert $status, does it become 1?
if(!$status)
// instruction
<?php
if($status != 0)
// instruction
// DO THIS
// This is like, if $status does not hold 0, then it must be 1
if($status)
// instruction
Trick #5
Sometimes we would like to know if a variable equals a certain number of values just like Trick #3. In most cases it would be great to move to a switch
. But switch
too can become combersome when the cases start to increase. One cool solution I use is to arrange all the possible values the element might take in an array
, then I can check if the element is in that array like this:
<?php
// GOOD
switch($fruit){
case 'Watermelon': // instructions
break;
case 'Orange': // instructions
break;
case 'Berries': // instructions
break;
case 'Mango': // instructions
break;
case 'Pear': // instructions
break;
case 'Apple': // instructions
break;
}
// BETTER
$fruits = ['Watermelon','Orange','Berries','Mango','Pear','Apple'];
$user_fruit = 'Pear';
if(in_array($user_fruit, $fruits))
// Instruction
P.S. While this solution looks simple in PHP, it doesn't look as clean in other languages such as C because it doesn't have an in-built function to verify if an array includes an element.
Conclusion
To conclude I expect this to help you improve your code and reduce your load by writing less code. Become more productive.
Last updated 2024-01-04 UTC