A Developer’s guide for cleaner code (PHP — Intermediate)

Saud Qureshi
Peak Mind
Published in
5 min readMay 22, 2020

simple yet powerful tricks you can pull out of your sleeves to write cleaner codes.

Welcome to part 2 of A Developer’s guide for cleaner code series.
Today in this article I would display some more tricks and best practices to write cleaner code in PHP (can be used in almost other languages too). But, before reading this article, I suggest you read the beginner’s article if you haven’t already.

This article will be mostly be revolving around if statements and how to clean them up.
I have seen many code-snippets containing horribly nested if statements that were not readable neither maintainable.
So today, I will try to share some practices I personally follow for implementing complex yet cleaner if checks.

1. Avoid unnecessary else statements

There are many devs who use unnecessary else statements in functions/methods to return a default value even though the code is actually more readable without that else block.

Note: We are considering the $gender will hold value such as male, female, trans, bigender, and $marital_status will hold value such as single, married, widowed. Of course, there are many other allowed values for both parameters, but for the purpose of this explanation We will keep it short.

Bad Example:

Uggh! In the above snippet, you can clearly see how not to perform complex checks.
You can clearly see that a default value is returned using an else block, we will start by removing it.

Good Example:

As seen in the above example most of the time people put in unnecessary else block which can be skipped for good.

2. Return early

Structure your code in such a way that allows you to evaluate the easiest checks as early as possible (and return the value), leaving the more complex ones for later.
This will allow us to skip redundant else or elseif statements making code much cleaner.
Let’s take the previous example and enhance it visually.

Previous Example:

The above example returns the value in every check but is still wrapped in numerous elseif and if checks.
Since we are already returning a value when a specified condition is fulfilled we can rewrite the above snippet as.

Better Example:

Explanation:

1st check: Handles the easiest check which is checking if $gender is ‘male’. The salutation remains the same for a male no matter what his $martial_status is.

2nd check: Handles $gender values which are not ‘female’, since we already put a check in place for ‘male’ before this the only values at this point in our $gender variable would be “female, transgender, bigender”, etc. In this check, we are specifically looking for values which are not “female” and returning “Mr./Ms.”, which will remove all other possibilities of the values that can exist in $gender except “female”.

3rd check: Since all the values of $gender except “female” have been handled in the checks above, we are only left with gender “female” and the $marital_status. This case does the checking for single or divorced statuses and returns the value ‘Ms.’.

Default Return: At this point, the value in our $gender will be female and the value in our $marial_status would be “widowed or married”, and in both cases we will return the value “Mrs.”.

3. Use an array-based map:

Ok, so this is quite an amazing trick that I randomly invented during a brainstorming session.
An array-based map is basically an array containing concatenated parameter values of an if check as an array-key, and array-value as the value you want to return for that check.

The previous example is pretty good and readable but the problem with it is, it's still not highly scalable and handling any other possible value to any of those 2 function parameters can increase one or more if checks in the above code.
Let’s take the previous example and enhance it:

Ultimate Example:

In this example, you can see that the combined values of $gender and $marital_status is used to create an array-key that can be used to determine the desired value for multiple combinations of gender and marital status.
The biggest pro of using this approach is, now you can add as many combinations of gender and marital status to $map and associate an appropriate salutation with it, without writing more if checks.

For example:
We can add a new $gender=“transgender-female” and couple it with $marital_status to provide a salutation specifically tailored for them.
Just add the below snippet in your $map and it’s done!

'transgender-female-single'  => 'Ms.',
'transgender-female-married' => 'Mrs.',
'transgender-female-widowed' => 'Mrs.',

So these were the few tricks that I personally use to clean my if checks up.
I hope you liked it! Please leave a clap and share this if you would like to see more articles like this one.

Read previous articles here:

--

--