/Programming Tip: Ditch the Else!

Programming Tip: Ditch the Else!

When I was a kiddo in programming, I remember those days when if statements was the go-to way to solve my problems in the code.

There are better approach in solving something. A little pinch of experience. A good hour of reading code, and a lot of practice; now it’s time for you to ditch the else.

Example:

public function hasName($name) {
   if(empty($name)){
      return false;
   } else {
      return true;
   }
}

Refactoring:

public function hasName($name) {
   if (!empty($name)) {
      return true
   }
   return false;
}

As you can see no need for the else part. Now your code is less clunky and you are ready to shine !

Sum up: Remember, the else statement is not always necessary.