From a09df2c8ee7f9a532a37ac40cb9cef938e34ebda Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Mon, 11 Sep 2017 22:40:27 +0300 Subject: [PATCH 01/14] add links to table of contents --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a472e34..03d3f08 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ * [Use searchable names (part 1)](#use-searchable-names-part-1) * [Use searchable names (part 2)](#use-searchable-names-part-2) * [Use explanatory variables](#use-explanatory-variables) + * [Avoid nesting too deeply and return early (part 1)](#avoid_nesting_too_deeply_and_return_early_part_1) + * [Avoid nesting too deeply and return early (part 2)](#avoid_nesting_too_deeply_and_return_early_part_2) * [Avoid Mental Mapping](#avoid-mental-mapping) * [Don't add unneeded context](#dont-add-unneeded-context) * [Use default arguments instead of short circuiting or conditionals](#use-default-arguments-instead-of-short-circuiting-or-conditionals) @@ -180,7 +182,7 @@ saveCityZipCode($matches['city'], $matches['zipCode']); **[⬆ back to top](#table-of-contents)** -### Avoid nesting too deeply and return early +### Avoid nesting too deeply and return early (part 1) Too many if else statemetns can make your code hard to follow. Explicit is better than implicit. @@ -228,6 +230,10 @@ function isShopOpen($day) } ``` +**[⬆ back to top](#table-of-contents)** + +### Avoid nesting too deeply and return early (part 2) + **Bad:** ```php From 21828c784ccdfd80f9383d3a97935b74923ca9d9 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Mon, 11 Sep 2017 22:41:09 +0300 Subject: [PATCH 02/14] correct links --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 03d3f08..82bb3f5 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ * [Use searchable names (part 1)](#use-searchable-names-part-1) * [Use searchable names (part 2)](#use-searchable-names-part-2) * [Use explanatory variables](#use-explanatory-variables) - * [Avoid nesting too deeply and return early (part 1)](#avoid_nesting_too_deeply_and_return_early_part_1) - * [Avoid nesting too deeply and return early (part 2)](#avoid_nesting_too_deeply_and_return_early_part_2) + * [Avoid nesting too deeply and return early (part 1)](#avoid-nesting-too-deeply-and-return-early-part-1) + * [Avoid nesting too deeply and return early (part 2)](#avoid-nesting-too-deeply-and-return-early-part-2) * [Avoid Mental Mapping](#avoid-mental-mapping) * [Don't add unneeded context](#dont-add-unneeded-context) * [Use default arguments instead of short circuiting or conditionals](#use-default-arguments-instead-of-short-circuiting-or-conditionals) From b2dd441eb9fe9df3bab71c58d4a53500a5f877c9 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 14 Sep 2017 13:34:29 +0300 Subject: [PATCH 03/14] add info text for private/protected members --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index c3f82ac..bf43be5 100644 --- a/README.md +++ b/README.md @@ -1159,6 +1159,14 @@ $balance = $bankAccount->getBalance(); ### Make objects have private/protected members +* `public` methods and properties are most dangerous for changes, because some outside code may easily rely on them and you can't control what code relies on them. Modifications in class are dangerous for all users of class. +* `protected` methods are as dangerous as public, because they are available in scope of any child class. This effectively means that difference between public and protected is only in access mechanism, but encapsulation guarantee remains the same. Modifications in class are dangerous for all descendant classes. +* `private` methods are your safe harbor in this doubtful world. They guarantee you that code in your methods is dangerous to modify only in boundaries of single class (which means that when you have tests for your protected/public methods that cover all calls of your private method, and as long as you don't do magic, like side effects or usage of global state, you are safe for modifications and you won't have [Jenga effect](http://www.urbandictionary.com/define.php?term=Jengaphobia&defid=2494196)). + +Therefore, use `private` by default and `public/protected` when you need to provide access for external classes. + +For more informations you can read the [blog post](http://fabien.potencier.org/pragmatism-over-theory-protected-vs-private.html) on this topic written by [Fabien Potencier](https://github.com/fabpot). + **Bad:** ```php From 429d58494cdfcb9e126f937a587a025b444b6965 Mon Sep 17 00:00:00 2001 From: Owen Kieffer-Jones Date: Fri, 15 Sep 2017 10:01:24 +0200 Subject: [PATCH 04/14] Minor grammatical fixes --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 06501cf..b35d636 100644 --- a/README.md +++ b/README.md @@ -989,7 +989,7 @@ The first thing to consider is consistent APIs. function travelToTexas($vehicle): void { if ($vehicle instanceof Bicycle) { - $vehicle->peddleTo(new Location('texas')); + $vehicle->pedalTo(new Location('texas')); } elseif ($vehicle instanceof Car) { $vehicle->driveTo(new Location('texas')); } @@ -1792,7 +1792,7 @@ class Robot implements Employee **Good:** -Not every worker is an employee, but every employee is an worker. +Not every worker is an employee, but every employee is a worker. ```php interface Workable From d6c0ddbec3f1188536a3212eff1566cd3bd2a830 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 15 Sep 2017 12:33:44 +0300 Subject: [PATCH 05/14] add Russian translation --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 06501cf..490032f 100644 --- a/README.md +++ b/README.md @@ -2033,5 +2033,7 @@ This is also available in other languages: * ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese:** * [yangweijie/clean-code-php](https://github.com/yangweijie/clean-code-php) * [php-cpm/clean-code-php](https://github.com/php-cpm/clean-code-php) + * ![ru](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png) **Russian:** + * [peter-gribanov/clean-code-php](https://github.com/peter-gribanov/clean-code-php) **[⬆ back to top](#table-of-contents)** From 6261d5114c270c0bcfe2fc8f4c396ae46f3fa342 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 15 Sep 2017 16:11:03 +0300 Subject: [PATCH 06/14] add Thai language --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 490032f..dd4843b 100644 --- a/README.md +++ b/README.md @@ -2035,5 +2035,7 @@ This is also available in other languages: * [php-cpm/clean-code-php](https://github.com/php-cpm/clean-code-php) * ![ru](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png) **Russian:** * [peter-gribanov/clean-code-php](https://github.com/peter-gribanov/clean-code-php) + * ![th](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Thailand.png) **Thai:** + * [panuwizzle/clean-code-php](https://github.com/panuwizzle/clean-code-php) **[⬆ back to top](#table-of-contents)** From d883307caa527109ac632a2019ffcb4051aabc19 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 15 Sep 2017 16:13:21 +0300 Subject: [PATCH 07/14] add Portuguese language --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index dd4843b..a769ecd 100644 --- a/README.md +++ b/README.md @@ -2035,6 +2035,8 @@ This is also available in other languages: * [php-cpm/clean-code-php](https://github.com/php-cpm/clean-code-php) * ![ru](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png) **Russian:** * [peter-gribanov/clean-code-php](https://github.com/peter-gribanov/clean-code-php) + * ![pt](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **Portuguese:** + * [fabioars/clean-code-php](https://github.com/fabioars/clean-code-php) * ![th](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Thailand.png) **Thai:** * [panuwizzle/clean-code-php](https://github.com/panuwizzle/clean-code-php) From f4724b684c08c5c4854b8803cf87caad5a780b52 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 15 Sep 2017 16:19:48 +0300 Subject: [PATCH 08/14] use emoji --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a769ecd..10b0bb3 100644 --- a/README.md +++ b/README.md @@ -2030,14 +2030,14 @@ function showList(array $employees): void This is also available in other languages: - * ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese:** +* :cn: **Chinese:** * [yangweijie/clean-code-php](https://github.com/yangweijie/clean-code-php) * [php-cpm/clean-code-php](https://github.com/php-cpm/clean-code-php) - * ![ru](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png) **Russian:** +* :ru: **Russian:** * [peter-gribanov/clean-code-php](https://github.com/peter-gribanov/clean-code-php) - * ![pt](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **Portuguese:** +* :brazil: **Portuguese:** * [fabioars/clean-code-php](https://github.com/fabioars/clean-code-php) - * ![th](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Thailand.png) **Thai:** +* :thailand: **Thai:** * [panuwizzle/clean-code-php](https://github.com/panuwizzle/clean-code-php) **[⬆ back to top](#table-of-contents)** From e11b2cef9a5bd3a753b9f8c516213fc3c434bf1a Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 15 Sep 2017 16:24:05 +0300 Subject: [PATCH 09/14] add more translations --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 10b0bb3..aca6b85 100644 --- a/README.md +++ b/README.md @@ -2033,10 +2033,12 @@ This is also available in other languages: * :cn: **Chinese:** * [yangweijie/clean-code-php](https://github.com/yangweijie/clean-code-php) * [php-cpm/clean-code-php](https://github.com/php-cpm/clean-code-php) + * [gbcr/clean-code-php](https://github.com/gbcr/clean-code-php) * :ru: **Russian:** * [peter-gribanov/clean-code-php](https://github.com/peter-gribanov/clean-code-php) * :brazil: **Portuguese:** * [fabioars/clean-code-php](https://github.com/fabioars/clean-code-php) + * [jeanjar/clean-code-php](https://github.com/jeanjar/clean-code-php/tree/pt-br) * :thailand: **Thai:** * [panuwizzle/clean-code-php](https://github.com/panuwizzle/clean-code-php) From cfc058e44c5c0c2eb141aef7490ffe308b8eadbc Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 15 Sep 2017 17:01:02 +0300 Subject: [PATCH 10/14] change description of private/protected --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bf43be5..34fd204 100644 --- a/README.md +++ b/README.md @@ -1159,9 +1159,9 @@ $balance = $bankAccount->getBalance(); ### Make objects have private/protected members -* `public` methods and properties are most dangerous for changes, because some outside code may easily rely on them and you can't control what code relies on them. Modifications in class are dangerous for all users of class. -* `protected` methods are as dangerous as public, because they are available in scope of any child class. This effectively means that difference between public and protected is only in access mechanism, but encapsulation guarantee remains the same. Modifications in class are dangerous for all descendant classes. -* `private` methods are your safe harbor in this doubtful world. They guarantee you that code in your methods is dangerous to modify only in boundaries of single class (which means that when you have tests for your protected/public methods that cover all calls of your private method, and as long as you don't do magic, like side effects or usage of global state, you are safe for modifications and you won't have [Jenga effect](http://www.urbandictionary.com/define.php?term=Jengaphobia&defid=2494196)). +* `public` methods and properties are most dangerous for changes, because some outside code may easily rely on them and you can't control what code relies on them. **Modifications in class are dangerous for all users of class.** +* `protected` methods are as dangerous as public, because they are available in scope of any child class. This effectively means that difference between public and protected is only in access mechanism, but encapsulation guarantee remains the same. **Modifications in class are dangerous for all descendant classes.** +* `private` methods are guarantee you that code in your methods is **dangerous to modify only in boundaries of single class** (you are safe for modifications and you won't have [Jenga effect](http://www.urbandictionary.com/define.php?term=Jengaphobia&defid=2494196)). Therefore, use `private` by default and `public/protected` when you need to provide access for external classes. From 0b06120c1e29e81763fb119b8b34e10584fca52c Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 15 Sep 2017 17:13:52 +0300 Subject: [PATCH 11/14] fix mistakes --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 34fd204..beb9acc 100644 --- a/README.md +++ b/README.md @@ -1160,8 +1160,8 @@ $balance = $bankAccount->getBalance(); ### Make objects have private/protected members * `public` methods and properties are most dangerous for changes, because some outside code may easily rely on them and you can't control what code relies on them. **Modifications in class are dangerous for all users of class.** -* `protected` methods are as dangerous as public, because they are available in scope of any child class. This effectively means that difference between public and protected is only in access mechanism, but encapsulation guarantee remains the same. **Modifications in class are dangerous for all descendant classes.** -* `private` methods are guarantee you that code in your methods is **dangerous to modify only in boundaries of single class** (you are safe for modifications and you won't have [Jenga effect](http://www.urbandictionary.com/define.php?term=Jengaphobia&defid=2494196)). +* `protected` modifier are as dangerous as public, because they are available in scope of any child class. This effectively means that difference between public and protected is only in access mechanism, but encapsulation guarantee remains the same. **Modifications in class are dangerous for all descendant classes.** +* `private` modifier guarantees that code is **dangerous to modify only in boundaries of single class** (you are safe for modifications and you won't have [Jenga effect](http://www.urbandictionary.com/define.php?term=Jengaphobia&defid=2494196)). Therefore, use `private` by default and `public/protected` when you need to provide access for external classes. From a26b56addd220077d25906fb186f108febfbbf84 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Mon, 18 Sep 2017 10:56:17 +0300 Subject: [PATCH 12/14] remove out of date chinese translations --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 3e1a933..9618e66 100644 --- a/README.md +++ b/README.md @@ -2045,9 +2045,7 @@ function showList(array $employees): void This is also available in other languages: * :cn: **Chinese:** - * [yangweijie/clean-code-php](https://github.com/yangweijie/clean-code-php) * [php-cpm/clean-code-php](https://github.com/php-cpm/clean-code-php) - * [gbcr/clean-code-php](https://github.com/gbcr/clean-code-php) * :ru: **Russian:** * [peter-gribanov/clean-code-php](https://github.com/peter-gribanov/clean-code-php) * :brazil: **Portuguese:** From 0b6ab70f45fde17185855c0d9f60c3b16d9f6d85 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Mon, 18 Sep 2017 10:56:55 +0300 Subject: [PATCH 13/14] add Spanish translation --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9618e66..8058ebf 100644 --- a/README.md +++ b/README.md @@ -2048,6 +2048,8 @@ This is also available in other languages: * [php-cpm/clean-code-php](https://github.com/php-cpm/clean-code-php) * :ru: **Russian:** * [peter-gribanov/clean-code-php](https://github.com/peter-gribanov/clean-code-php) +* :es: **Spanish:** + * [fikoborquez/clean-code-php](https://github.com/fikoborquez/clean-code-php) * :brazil: **Portuguese:** * [fabioars/clean-code-php](https://github.com/fabioars/clean-code-php) * [jeanjar/clean-code-php](https://github.com/jeanjar/clean-code-php/tree/pt-br) From 51b4b8782133b821e65124f3803add8c079c69f8 Mon Sep 17 00:00:00 2001 From: Yuriy Zinchenko Date: Mon, 18 Sep 2017 12:38:25 +0300 Subject: [PATCH 14/14] Optimize conditions in 'Avoid nesting too deeply and return early (part 2)' section --- README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3e1a933..8a616b4 100644 --- a/README.md +++ b/README.md @@ -260,12 +260,8 @@ function fibonacci(int $n) ```php function fibonacci(int $n): int { - if ($n === 0) { - return 0; - } - - if ($n === 1) { - return 1; + if ($n === 0 || $n === 1) { + return $n; } if ($n > 50) {