Software engineering principles, from Robert C. Martin's book Clean Code, adapted for PHP. This is not a style guide. It's a guide to producing readable, reusable, and refactorable software in PHP.
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.
We will read more code than we will ever write. It's important that the code we do write is readable and searchable. By not naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable.
Bad:
// What the heck is 86400 for?addExpireAt(86400);
Good:
// Declare them as capitalized `const` globals.interfaceDateGlobal {const SECONDS_IN_A_DAY =86400;}addExpireAt(DateGlobal::SECONDS_IN_A_DAY);
Limiting the amount of function parameters is incredibly important because it makes testing your function easier. Having more than three leads to a combinatorial explosion where you have to test tons of different cases with each separate argument.
Zero arguments is the ideal case. One or two arguments is ok, and three should be avoided. Anything more than that should be consolidated. Usually, if you have more than two arguments then your function is trying to do too much. In cases where it's not, most of the time a higher-level object will suffice as an argument.
This is by far the most important rule in software engineering. When functions do more than one thing, they are harder to compose, test, and reason about. When you can isolate a function to just one action, they can be refactored easily and your code will read much cleaner. If you take nothing else away from this guide other than this, you'll be ahead of many developers.
When you have more than one level of abstraction your function is usually doing too much. Splitting up functions leads to reusability and easier testing.
Bad:
functionparseBetterJSAlternative($code) { $regexes = [// ... ]; $statements =split(' ', $code); $tokens = [];foreach($regexes as $regex) {foreach($statements as $statement) {// ... } } $ast = [];foreach($tokens as $token) {// lex... }foreach($ast as $node) {// parse... }}
Do your absolute best to avoid duplicate code. Duplicate code is bad because it means that there's more than one place to alter something if you need to change some logic.
Imagine if you run a restaurant and you keep track of your inventory: all your tomatoes, onions, garlic, spices, etc. If you have multiple lists that you keep this on, then all have to be updated when you serve a dish with tomatoes in them. If you only have one list, there's only one place to update!
Oftentimes you have duplicate code because you have two or more slightly different things, that share a lot in common, but their differences force you to have two or more separate functions that do much of the same things. Removing duplicate code means creating an abstraction that can handle this set of different things with just one function/module/class.
Getting the abstraction right is critical, that's why you should follow the SOLID principles laid out in the Classes section. Bad abstractions can be worse than duplicate code, so be careful! Having said this, if you can make a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself updating multiple places anytime you want to change one thing.
Flags tell your user that this function does more than one thing. Functions should do one thing. Split out your functions if they are following different code paths based on a boolean.
A function produces a side effect if it does anything other than take a value in and return another value or values. A side effect could be writing to a file, modifying some global variable, or accidentally wiring all your money to a stranger.
Now, you do need to have side effects in a program on occasion. Like the previous example, you might need to write to a file. What you want to do is to centralize where you are doing this. Don't have several functions and classes that write to a particular file. Have one service that does it. One and only one.
The main point is to avoid common pitfalls like sharing state between objects without any structure, using mutable data types that can be written to by anything, and not centralizing where your side effects occur. If you can do this, you will be happier than the vast majority of other programmers.
Bad:
// Global variable referenced by following function.// If we had another function that used this name, now it'd be an array and it could break it.$name ='Ryan McDermott';functionsplitIntoFirstAndLastName() { $name =preg_split('/ /', $name);}splitIntoFirstAndLastName();var_dump($name); // ['Ryan', 'McDermott'];
Polluting globals is a bad practice in very languages because you could clash with another library and the user of your API would be none-the-wiser until they get an exception in production. Let's think about an example: what if you wanted to have configuration array. You could write global function like config(), but it could clash with another library that tried to do the same thing. This is why it would be much better to use singleton design pattern and simple set configuration.
This seems like an impossible task. Upon first hearing this, most people say, "how am I supposed to do anything without an if statement?" The answer is that you can use polymorphism to achieve the same task in many cases. The second question is usually, "well that's great but why would I want to do that?" The answer is a previous clean code concept we learned: a function should only do one thing. When you have classes and functions that have if statements, you are telling your user that your function does more than one thing. Remember, just do one thing.
Bad:
classAirplane {// ...publicfunctiongetCruisingAltitude() {switch ($this->type) {case'777':return$this->getMaxAltitude()-$this->getPassengerCount();case'Air Force One':return$this->getMaxAltitude();case'Cessna':return$this->getMaxAltitude()-$this->getFuelExpenditure(); } }}
PHP is untyped, which means your functions can take any type of argument. Sometimes you are bitten by this freedom and it becomes tempting to do type-checking in your functions. There are many ways to avoid having to do this. The first thing to consider is consistent APIs.
If you are working with basic primitive values like strings, integers, and arrays, and you can't use polymorphism but you still feel the need to type-check, you should consider type declaration or strict mode. It provides you with static typing on top of standard PHP syntax. The problem with manually type-checking is that doing it well requires so much extra verbiage that the faux "type-safety" you get doesn't make up for the lost readability. Keep your PHP clean, write good tests, and have good code reviews. Otherwise, do all of that but with PHP strict type declaration or strict mode.
Bad:
functioncombine($val1, $val2) {if (is_numeric($val1)&&is_numeric($val2)) {return $val1 + $val2; }thrownew\Exception('Must be of type Number');}
Dead code is just as bad as duplicate code. There's no reason to keep it in your codebase. If it's not being called, get rid of it! It will still be safe in your version history if you still need it.
As stated in Clean Code, "There should never be more than one reason for a class to change". It's tempting to jam-pack a class with a lot of functionality, like when you can only take one suitcase on your flight. The issue with this is that your class won't be conceptually cohesive and it will give it many reasons to change. Minimizing the amount of times you need to change a class is important. It's important because if too much functionality is in one class and you modify a piece of it, it can be difficult to understand how that will affect other dependent modules in your codebase.
As stated by Bertrand Meyer, "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification." What does that mean though? This principle basically states that you should allow users to add new functionalities without changing existing code.
This is a scary term for a very simple concept. It's formally defined as "If S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.)." That's an even scarier definition.
The best explanation for this is if you have a parent class and a child class, then the base class and child class can be used interchangeably without getting incorrect results. This might still be confusing, so let's take a look at the classic Square-Rectangle example. Mathematically, a square is a rectangle, but if you model it using the "is-a" relationship via inheritance, you quickly get into trouble.
ISP states that "Clients should not be forced to depend upon interfaces that they do not use."
A good example to look at that demonstrates this principle is for classes that require large settings objects. Not requiring clients to setup huge amounts of options is beneficial, because most of the time they won't need all of the settings. Making them optional helps prevent having a "fat interface".
Bad:
interfaceWorkerInterface {publicfunctionwork();publicfunctioneat();}classWorkerimplementsWorkerInterface {publicfunctionwork() {// ....working }publicfunctioneat() {// ...... eating in launch break }}classSuperWorkerimplementsWorkerInterface {publicfunctionwork() {//.... working much more }publicfunctioneat() {//.... eating in launch break }}classManager {/** @varWorkerInterface $worker **/private $worker;public void setWorker(WorkerInterface $worker) {$this->worker = $worker; }publicfunctionmanage() {$this->worker->work(); }}
Good:
interfaceWorkerInterfaceextendsFeedableInterface,WorkableInterface {}interfaceWorkableInterface {publicfunctionwork();}interfaceFeedableInterface {publicfunctioneat();}classWorkerimplementsWorkableInterface,FeedableInterface {publicfunctionwork() {// ....working }publicfunctioneat() {//.... eating in launch break }}classRobotimplementsWorkableInterface {public void work() {// ....working }}classSuperWorkerimplementsWorkerInterface {publicfunctionwork() {//.... working much more }publicfunctioneat() {//.... eating in launch break }}classManager {/** @var $worker WorkableInterface **/private $worker;publicfunctionsetWorker(WorkableInterface $w) {$this->worker = $w; }publicfunctionmanage() {$this->worker->work(); }}
This principle states two essential things: 1. High-level modules should not depend on low-level modules. Both should depend on abstractions. 2. Abstractions should not depend upon details. Details should depend on abstractions.
This can be hard to understand at first, but if you've worked with PHP frameworks (like Symfony), you've seen an implementation of this principle in the form of Dependency Injection (DI). While they are not identical concepts, DIP keeps high-level modules from knowing the details of its low-level modules and setting them up. It can accomplish this through DI. A huge benefit of this is that it reduces the coupling between modules. Coupling is a very bad development pattern because it makes your code hard to refactor.
Bad:
classWorker {publicfunctionwork() {// ....working }}classManager {/** @varWorker $worker **/private $worker;publicfunction__construct(Worker $worker) {$this->worker = $worker; }publicfunctionmanage() {$this->worker->work(); }}classSuperWorkerextendsWorker {publicfunctionwork() {//.... working much more }}
Good:
interfaceWorkerInterface {publicfunctionwork();}classWorkerimplementsWorkerInterface {publicfunctionwork() {// ....working }}classSuperWorkerimplementsWorkerInterface {publicfunctionwork() {//.... working much more }}classManager {/** @varWorker $worker **/private $worker;public void __construct(WorkerInterface $worker) {$this->worker = $worker; }public void manage() {$this->worker->work(); }}
This pattern is very useful and commonly used it many libraries such as PHPUnit and Doctrine. It allows your code to be expressive, and less verbose. For that reason, I say, use method chaining and take a look at how clean your code will be. In your class functions, simply return this at the end of every function, and you can chain further class methods onto it.