Reduce If else in PHP | Write Clean Code - Part 1
Unfortunately, there is no unique way to write code. Although there are some set of rules of writing code, however, programmers are free to write code in their own style, especially the logical part. We often see the code that contains a lot of if-else
statement, even nested if-else. It’s always been a good practice to reduce the else
part as much as possible. Today, I will show you some practical code where we can reduce if-else
and make code more readable. Let’s start-
Table of Contents
Open Table of Contents
Idea 1
Given I have a code that was written by a developer. This code check whether the logged-in user is admin or user and user has editable permission or not.
public function canUserEdit($userId)
{
if ( Auth::check() ) {
if ( Auth::user()->type == 'admin' ) {
// show edit because admin can edit everything.
} else {
if ( Auth::user()->id === $userId) {
// success and show the edit page
} else {
// not eligible and error message
return redirect()->back();
}
}
} else {
// not eligible and error message
return redirect()->back();
}
}
I am sure, many developers write logic their code like this way. This code works fine, no error what so ever. However, this code can be improved. Let’s refactor this code.
Refactor Idea 1
In Laravel, there are few ways to refactor this code. Most probably you are thinking to use middleware. Maybe a bit more, using some user role package. Of course, you are free to do that. However, let’s refactor without any builtin functionalities or third party packages.
public function canUserEdit($userId)
{
// I use a defensive mood
if ( ! Auth::check() ) {
return redirect()->back();
}
if ( Auth::id() !== $userId ) {
// not eligible and error message
return redirect()->back();
}
if ( Auth::user()->type === 'admin' OR Auth::id() === $userId ) {
// success and show the edit page
}
}
I rewrite the code where I didn’t use else
at all. I think it’s a good practice to be in defensive approach. I believe, this style is more readable and realistic.
Idea 2
Just imagine that you have a code to upload a file. Now, you need to check whether the form field has the file, then you need to check the file has the allowable extension or not. If everything passes, then only you can allow uploading the file. Let’s write a pseudo code for that on PHP.
if (isset($_POST['file'])) {
if (in_array($fileExtension, $allowedFileExtensions)) {
// Update the file
} else {
return;
// Error and redirect
}
} else {
return;
// Error and redirect
}
Enough, right? Many developers think like this way. Surely, this code will work fine. But, again, my concern is how to reduce code and logic from here and to be more defensive? Let’s see.
Refactor Idea 2
Firstly I try to remove the else part from there and then be more defensive. Let’s remove that.
if ( ! isset($_POST['file'])) {
return;
// Error and redirect
}
if ( ! in_array($fileExtension, $allowedFileExtensions)) {
return;
// Error and redirect
}
// Now upload the file
Both of the code will do the same task. But I think, the refactored approach is more readable and maintainable.
Besides, if you have any other thought to reduce code and logic on that and make it better, feel free to share with us in the comment box below.
Thank you.