How to write good “If” statements?

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?

4 thoughts on “How to write good “If” statements?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top