commit 8148c50a59b0fd895af6e25e8c1f7ecee516bb60 Author: Piotr Plenik Date: Mon Jan 16 12:20:24 2017 +0100 initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..b1d6160 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.md linguist-documentation=false +*.md linguist-language=PHP diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0575c8e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Ryan McDermott + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE diff --git a/README.md b/README.md new file mode 100644 index 0000000..591f927 --- /dev/null +++ b/README.md @@ -0,0 +1,174 @@ +# clean-code-php + +## Table of Contents + 1. [Introduction](#introduction) + 2. [Variables](#variables) + +## Introduction + +Software engineering principles, from Robert C. Martin's book +[*Clean Code*](https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882), +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*. + +Inspired from [clean-code-javascript](https://github.com/ryanmcdermott/clean-code-javascript) + +## **Variables** +### Use meaningful and pronounceable variable names + +**Bad:** +```php +$ymdstr = $moment->format('y-m-d'); +``` + +**Good**: +```javascript +$currentDate = $moment->format('y-m-d'); +``` +**[⬆ back to top](#table-of-contents)** + +### Use the same vocabulary for the same type of variable + +**Bad:** +```php +getUserInfo(); +getClientData(); +getCustomerRecord(); +``` + +**Good**: +```php +getUser(); +``` +**[⬆ back to top](#table-of-contents)** + +### Use searchable names +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:** +```php +// What the heck is 86400 for? +addExpireAt(86400); + +``` + +**Good**: +```php +// Declare them as capitalized `const` globals. +interface DateGlobal { + const SECONDS_IN_A_DAY = 86400; +} + +addExpireAt(DateGlobal::SECONDS_IN_A_DAY); +``` +**[⬆ back to top](#table-of-contents)** + + +### Use explanatory variables +**Bad:** +```php +$address = 'One Infinite Loop, Cupertino 95014'; +$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/'; +preg_match($cityZipCodeRegex, $address, $matches); +saveCityZipCode($matches[1], $matches[2]); +``` + +**Good**: +```php +$address = 'One Infinite Loop, Cupertino 95014'; +$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/'; +preg_match($cityZipCodeRegex, $address, $matches); +list(, $city, $zipCode) = $matchers; +saveCityZipCode(city, zipCode); +``` +**[⬆ back to top](#table-of-contents)** + +### Avoid Mental Mapping +Explicit is better than implicit. + +**Bad:** +```php +$l = ['Austin', 'New York', 'San Francisco']; +foreach($i=0; $i 'Honda', + 'carModel' => 'Accord', + 'carColor' => 'Blue', +]; + +function paintCar(&$car) { + $car['carColor'] = 'Red'; +} +``` + +**Good**: +```php +$car = [ + 'make' => 'Honda', + 'model' => 'Accord', + 'color' => 'Blue', +]; + +function paintCar(&$car) { + $car['color'] = 'Red'; +} +``` +**[⬆ back to top](#table-of-contents)** + +### Use default arguments instead of short circuiting or conditionals + +**Bad:** +```php +function createMicrobrewery($name = null) { + $breweryName = $name ?: 'Hipster Brew Co.'; + // ... +} + +``` + +**Good**: +```php +function createMicrobrewery($breweryName = 'Hipster Brew Co.') { + // ... +} + +``` +**[⬆ back to top](#table-of-contents)** + +