Clean code — Name matters. A good naming convention is one of the…
1. Give your names intentions

Name of class, function, variable should inform why was created, what do and how is used. If you need to add a comment above your variable you probably didn’t precise your intentions enough. Choosing the right name is a good investment so take your time. Asking your colleagues for help is a good idea. Remember you are not the only one who will be working with this code.

Bad:

public int $f_cnt; // friends count

Good:

public int $friendsCount;

Variable name f_cnt don’t inform us strictly what kind of information keeps inside. We have to guess or dig deeper into code to find the information we are looking for.

Bad:

public function process(array $u, $a = 18): array

{

$results = [];

foreach ($u as $i) {

if ($i['a'] >= $a) {

$results[] = $i['id'];

}

}



return $results;

}

As you see this code isn’t complicated. It has only one array iteration and one conditional statement but can you deduce what this code does? The problem here is not code complexity but its secretiveness. Names of variables, array keys, and function properties are hiding important information from us. Let’s see at refactored code.

Good:

public function getAdultUsersIds(array $usersList, $adultAge = 18): array

{

$adultUsersIds = [];

foreach ($usersList as $user) {

if ($user['age'] >= $adultAge) {

$adultUsersIds[] = $user['id'];

}

}



return $adultUsersIds;

}

Better immediately. We can now deduce what method does just by reading the name.
['Marcin Worwa']
['Clean Code', 'Software Development', 'Technology', 'PHP', 'Programming']