Clean Code Concepts Adapted for PHP

News

September 6th, 2017

clean-code-concepts-php.png

Clean Code PHP (jupeter/clean-code-php), is a guide based on the book Clean Code: A Handbook of Agile Software Craftmanship, a classic programming book about writing maintainable code by Uncle Bob Martin.

The clean-code-php guide is inspired by a JavaScript adaptation, clean-code-javascript with PHP-specific features.

Here are a few of my favorite adaptations from the clean-code-php repository:

Don’t add unneeded context

Bad

1<?php
2
3class Car
4{
5 public $carMake;
6 public $carModel;
7 public $carColor;
8
9 //...
10}

Good

1<?php
2
3class Car
4{
5 public $make;
6 public $model;
7 public $color;
8
9 //...
10}

View Don’t add unneeded context in the guide.

Function Arguments (2 or fewer ideally)

Bad

1<?php
2
3function createMenu($title, $body, $buttonText, $cancellable) {
4 // ...
5}

Good

1<?php
2
3class MenuConfig
4{
5 public $title;
6 public $body;
7 public $buttonText;
8 public $cancellable = false;
9}
10
11$config = new MenuConfig();
12$config->title = 'Foo';
13$config->body = 'Bar';
14$config->buttonText = 'Baz';
15$config->cancellable = true;
16
17function createMenu(MenuConfig $config) {
18 // ...
19}

View Function Arguments in the guide.

Functions Should Do One Thing

Bad

1<?php
2
3function emailClients($clients) {
4 foreach ($clients as $client) {
5 $clientRecord = $db->find($client);
6 if ($clientRecord->isActive()) {
7 email($client);
8 }
9 }
10}

Good

1function emailClients($clients) {
2 $activeClients = activeClients($clients);
3 array_walk($activeClients, 'email');
4}
5
6function activeClients($clients) {
7 return array_filter($clients, 'isClientActive');
8}
9
10function isClientActive($client) {
11 $clientRecord = $db->find($client);
12 return $clientRecord->isActive();
13}

View View Functions Should Do One Thing in the guide.

Opinions, Opinions, Opinions

The author outlines the following about the purpose of the guide:

Not every principle herein has to be strictly followed, and even fewer will be universally agreed upon. These are guidelines and nothing more, but they are ones codified over many years of collective experience by the authors of Clean Code.

In a dynamic language like PHP (or any language for that matter), developers will disagree with some (or many) of the points made about the concepts demonstrated. I think the point I’d make is not to write off all ideas contained in the guide if you disagree with one or more ideas.

Learn More about Clean Code

I highly recommend that you read the Clean Code book to get a deeper understanding of the guidelines demonstrated in the PHP adaptation. On its own, the PHP guide is a good read, but part of the journey is learning how to spot code that needs some work and then adapting it.

Another great book related to Clean Code, is The Clean Coder: A Code of Conduct for Professional Programmers which focuses on you, the coder. In my opinion, these two books should be a must-read for programmers of any experience level looking to improve their code craftsmanship.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.