A Developer’s guide for cleaner code (PHP - Beginner)

Saud Qureshi
Peak Mind
Published in
4 min readMay 15, 2020

Simple and easy to understand tricks for writing cleaner code.

Clean code is a dream for every developer out there, there’s just so many ways, patterns, principles that one can follow in order to develop cleaner and manageable codebase. But it can also get overwhelming since it is so easy to over engineer (your codebase) when following such advance patterns and principles.

So today, I would like to share some very “simple” and “easy to understand”
practices that I personally follow when writing codes.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
- Martin Fowler, 2008.

1. Consistent naming convention.

Yeah, I know you didn’t come here to read about how to name your variables and methods in your code base. But, trust me, it is one of the most important practices that you stick to a consistent naming convention through out your codebase.
It’s totally upto you which convention you want to follow. But it is important that whichever convention you adopt, you stick to it.
It is also important that you consistently name the methods according to their behaviour.

For eg. Let’s assume we have a model named User which represents users table in the database.
A user can have:
- orders
- wishlists

Bad: see the inconsistent names of methods even though their behaviour is similar, i.e: getting associated data of a user from the database.

use Illuminate\Database\Eloquent\Model;class User extends Model {
public function getOrders() {
// fetch user orders here
}

public function fetchWishLists() {
// fetch user wish lists here
}
}

Good: the naming of the methods are consistent according to their behaviour.

use Illuminate\Database\Eloquent\Model;class User extends Model {
public function getOrders() {
// fetch user orders here
}

public function getWishLists() {
// fetch wish lists here
}
}

2. Stop using empty().

empty() function in php is used to check if the given variable exists with in the scope and it’s value is empty.
Empty values are:
1. “” (empty string).
2.
false.
3.
0 (or 0.0, 0”).
4. [] (empty array).
5.
null
.

There is no point in using empty() as long as you are sure that the variable you are checking exists in the given scope.
Any empty value (listed above) is already considered as a falsy (equates to false) value in PHP (and many other dynamically typed language).

For eg:

Bad: There’s no point in using empty() here since the variable we are checking exists in the scope.

$var = “”;
if (empty($var) == false) {
//do something here with $var
}

Good: The same result can be achieved by.

$var = “”;
if ($var) {
//do something here with $var
}

3. Use null coalescing operator.

PHP 7 introduced null coalescing operator back in 2015. Yet, many devs are not aware of how convenient it is to use.
instead of having to write isset() with is_null to check if a variable/array is set or is not equal to null, you can achieve the same result with ?? .

For eg:

Bad: this approach is just too verbose for such a simple task.

$data = [];// check if array index is not set or is null.
if (isset($data['name']) == false or is_null($data['name']) == true) {
$data['name'] = 'Leonidus I';
}

Better: this approach uses ?? instead of isset and is_null check, which reduces our boiler plate.

$data = [];// ?? checks if the index does not exist or if the value is null
$data['name'] = $data['name'] ?? 'Alexander';

Good: this approach uses null coalescing assignment which does the same thing but is even more expressive.
Note: can only be used in PHP 7.4+.

$data = [];/*
* ??= checks if index does not exist or if the value is null and
* assigns it to RHS.
*/
$data['name'] ??= 'Alexander';

4. Negation instead of boolean comparison in if().

We do come across cases every now and then where we have to check for truthy or falsy value in if(). What we often do, is use == to check if the value is true or false .
As I mentioned earlier in this article any empty value is considered falsy in PHP. Similarly, any non empty value is considered truthy.
Using ! (not operator) instead of == true or == false , we can reduce the amount of code we write to check the truthiness or falsiness of a value.

For eg:

Okay: Here, we are explicitly using == false to check if the value of $eligibility is false (or falsy).

$eligibility = false;/**
* $eligibility (which is false) when checked against
* false (boolean), returns true.
*/
if ($eligibility == false) {
echo "You are not eligible for this action";
}

Good: Here, we use ! to check if the value is false by basically inversing it.

$eligibility = false;/**
* Explanation, ! inverses your given truthy or falsy value.
* That is, By putting ! before $eligibility (which is false)
* you get true as a result same as above.
* Reads "!$eligibility" as "not false"
*/
if (!$eligibility) {
echo "You are not eligible for this action";
}

I hope these very simple and easy to understand 4 tips will help you to write cleaner code in PHP (and many other dynamically typed language).

I will also publish an intermediate and advanced tips for cleaner codes in near future.

--

--