|
| 1 | +# clean-code-php |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | + 1. [Introduction](#introduction) |
| 5 | + 2. [Variables](#variables) |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +Software engineering principles, from Robert C. Martin's book |
| 10 | +[*Clean Code*](https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882), |
| 11 | +adapted for PHP. This is not a style guide. It's a guide to producing |
| 12 | +readable, reusable, and refactorable software in PHP. |
| 13 | + |
| 14 | +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*. |
| 15 | + |
| 16 | +Inspired from [clean-code-javascript](https://github.com/ryanmcdermott/clean-code-javascript) |
| 17 | + |
| 18 | +## **Variables** |
| 19 | +### Use meaningful and pronounceable variable names |
| 20 | + |
| 21 | +**Bad:** |
| 22 | +```php |
| 23 | +$ymdstr = $moment->format('y-m-d'); |
| 24 | +``` |
| 25 | + |
| 26 | +**Good**: |
| 27 | +```javascript |
| 28 | +$currentDate = $moment->format('y-m-d'); |
| 29 | +``` |
| 30 | +**[⬆ back to top](#table-of-contents)** |
| 31 | + |
| 32 | +### Use the same vocabulary for the same type of variable |
| 33 | + |
| 34 | +**Bad:** |
| 35 | +```php |
| 36 | +getUserInfo(); |
| 37 | +getClientData(); |
| 38 | +getCustomerRecord(); |
| 39 | +``` |
| 40 | + |
| 41 | +**Good**: |
| 42 | +```php |
| 43 | +getUser(); |
| 44 | +``` |
| 45 | +**[⬆ back to top](#table-of-contents)** |
| 46 | + |
| 47 | +### Use searchable names |
| 48 | +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. |
| 49 | +Make your names searchable. |
| 50 | + |
| 51 | +**Bad:** |
| 52 | +```php |
| 53 | +// What the heck is 86400 for? |
| 54 | +addExpireAt(86400); |
| 55 | + |
| 56 | +``` |
| 57 | + |
| 58 | +**Good**: |
| 59 | +```php |
| 60 | +// Declare them as capitalized `const` globals. |
| 61 | +interface DateGlobal { |
| 62 | + const SECONDS_IN_A_DAY = 86400; |
| 63 | +} |
| 64 | + |
| 65 | +addExpireAt(DateGlobal::SECONDS_IN_A_DAY); |
| 66 | +``` |
| 67 | +**[⬆ back to top](#table-of-contents)** |
| 68 | + |
| 69 | + |
| 70 | +### Use explanatory variables |
| 71 | +**Bad:** |
| 72 | +```php |
| 73 | +$address = 'One Infinite Loop, Cupertino 95014'; |
| 74 | +$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/'; |
| 75 | +preg_match($cityZipCodeRegex, $address, $matches); |
| 76 | +saveCityZipCode($matches[1], $matches[2]); |
| 77 | +``` |
| 78 | + |
| 79 | +**Good**: |
| 80 | +```php |
| 81 | +$address = 'One Infinite Loop, Cupertino 95014'; |
| 82 | +$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/'; |
| 83 | +preg_match($cityZipCodeRegex, $address, $matches); |
| 84 | +list(, $city, $zipCode) = $matchers; |
| 85 | +saveCityZipCode(city, zipCode); |
| 86 | +``` |
| 87 | +**[⬆ back to top](#table-of-contents)** |
| 88 | + |
| 89 | +### Avoid Mental Mapping |
| 90 | +Explicit is better than implicit. |
| 91 | + |
| 92 | +**Bad:** |
| 93 | +```php |
| 94 | +$l = ['Austin', 'New York', 'San Francisco']; |
| 95 | +foreach($i=0; $i<count($l); $i++) { |
| 96 | + oStuff(); |
| 97 | + doSomeOtherStuff(); |
| 98 | + // ... |
| 99 | + // ... |
| 100 | + // ... |
| 101 | + // Wait, what is `l` for again? |
| 102 | + dispatch(l); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +**Good**: |
| 107 | +```php |
| 108 | +$locations = ['Austin', 'New York', 'San Francisco']; |
| 109 | +foreach($i=0; $i<count($locations); $i++) { |
| 110 | + $location = $locations[$i]; |
| 111 | + |
| 112 | + doStuff(); |
| 113 | + doSomeOtherStuff(); |
| 114 | + // ... |
| 115 | + // ... |
| 116 | + // ... |
| 117 | + dispatch(location); |
| 118 | +}); |
| 119 | +``` |
| 120 | +**[⬆ back to top](#table-of-contents)** |
| 121 | + |
| 122 | + |
| 123 | +### Don't add unneeded context |
| 124 | +If your class/object name tells you something, don't repeat that in your |
| 125 | +variable name. |
| 126 | + |
| 127 | +**Bad:** |
| 128 | +```php |
| 129 | +$car = [ |
| 130 | + 'carMake' => 'Honda', |
| 131 | + 'carModel' => 'Accord', |
| 132 | + 'carColor' => 'Blue', |
| 133 | +]; |
| 134 | + |
| 135 | +function paintCar(&$car) { |
| 136 | + $car['carColor'] = 'Red'; |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +**Good**: |
| 141 | +```php |
| 142 | +$car = [ |
| 143 | + 'make' => 'Honda', |
| 144 | + 'model' => 'Accord', |
| 145 | + 'color' => 'Blue', |
| 146 | +]; |
| 147 | + |
| 148 | +function paintCar(&$car) { |
| 149 | + $car['color'] = 'Red'; |
| 150 | +} |
| 151 | +``` |
| 152 | +**[⬆ back to top](#table-of-contents)** |
| 153 | + |
| 154 | +### Use default arguments instead of short circuiting or conditionals |
| 155 | + |
| 156 | +**Bad:** |
| 157 | +```php |
| 158 | +function createMicrobrewery($name = null) { |
| 159 | + $breweryName = $name ?: 'Hipster Brew Co.'; |
| 160 | + // ... |
| 161 | +} |
| 162 | + |
| 163 | +``` |
| 164 | + |
| 165 | +**Good**: |
| 166 | +```php |
| 167 | +function createMicrobrewery($breweryName = 'Hipster Brew Co.') { |
| 168 | + // ... |
| 169 | +} |
| 170 | + |
| 171 | +``` |
| 172 | +**[⬆ back to top](#table-of-contents)** |
| 173 | + |
| 174 | + |
0 commit comments