Writing a good code is an art and it can be useful in so trivial things that most developers often ignore. Usually, amateurs are programmed to write an ‘if’ statement which is always accompanied by an ‘else’ . However, it is important to realize that code quality can significantly be improved if else is omitted. For instance, consider this code.
function getUser($id){
if($id < 1){
$user = [];
}else{
//some big codes
}
return $user
}
Here some big codes can be so big that it will return statement will be lost in it. Better would be:
function getUser($id){
if($id < 1){
return [];
}
//some big codes
return $user
}
How to write good “If” statements?
Reduced the readability?
This reminds me how apple blew it up with a wrong if statement.
I just shared this to our newbies! exactly what I was looking for.
This is one of the ‘ah ha’ moments a developer has in his life. ‘return’ statement with if