Skip to content

Commit c59276d

Browse files
Merge branch 'master' into lsp3
2 parents 196837f + 3309c09 commit c59276d

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* [Use searchable names (part 1)](#use-searchable-names-part-1)
1010
* [Use searchable names (part 2)](#use-searchable-names-part-2)
1111
* [Use explanatory variables](#use-explanatory-variables)
12+
* [Avoid nesting too deeply and return early (part 1)](#avoid-nesting-too-deeply-and-return-early-part-1)
13+
* [Avoid nesting too deeply and return early (part 2)](#avoid-nesting-too-deeply-and-return-early-part-2)
1214
* [Avoid Mental Mapping](#avoid-mental-mapping)
1315
* [Don't add unneeded context](#dont-add-unneeded-context)
1416
* [Use default arguments instead of short circuiting or conditionals](#use-default-arguments-instead-of-short-circuiting-or-conditionals)
@@ -55,6 +57,8 @@ years of collective experience by the authors of *Clean Code*.
5557

5658
Inspired from [clean-code-javascript](https://github.com/ryanmcdermott/clean-code-javascript)
5759

60+
Although many developers still use PHP 5, most of the examples in this article only work with PHP 7.1+.
61+
5862
## Variables
5963

6064
### Use meaningful and pronounceable variable names
@@ -180,9 +184,9 @@ saveCityZipCode($matches['city'], $matches['zipCode']);
180184

181185
**[⬆ back to top](#table-of-contents)**
182186

183-
### Avoid nesting too deeply and return early
187+
### Avoid nesting too deeply and return early (part 1)
184188

185-
Too many if else statemetns can make your code hard to follow. Explicit is better
189+
Too many if else statements can make your code hard to follow. Explicit is better
186190
than implicit.
187191

188192
**Bad:**
@@ -224,10 +228,14 @@ function isShopOpen(string $day): bool
224228
'friday', 'saturday', 'sunday'
225229
];
226230

227-
return in_array(strtolower($day), $openingDays);
231+
return in_array(strtolower($day), $openingDays, true);
228232
}
229233
```
230234

235+
**[⬆ back to top](#table-of-contents)**
236+
237+
### Avoid nesting too deeply and return early (part 2)
238+
231239
**Bad:**
232240

233241
```php
@@ -254,12 +262,8 @@ function fibonacci(int $n)
254262
```php
255263
function fibonacci(int $n): int
256264
{
257-
if ($n === 0) {
258-
return 0;
259-
}
260-
261-
if ($n === 1) {
262-
return 1;
265+
if ($n === 0 || $n === 1) {
266+
return $n;
263267
}
264268

265269
if ($n > 50) {
@@ -989,7 +993,7 @@ The first thing to consider is consistent APIs.
989993
function travelToTexas($vehicle): void
990994
{
991995
if ($vehicle instanceof Bicycle) {
992-
$vehicle->peddleTo(new Location('texas'));
996+
$vehicle->pedalTo(new Location('texas'));
993997
} elseif ($vehicle instanceof Car) {
994998
$vehicle->driveTo(new Location('texas'));
995999
}
@@ -1158,6 +1162,14 @@ $balance = $bankAccount->getBalance();
11581162

11591163
### Make objects have private/protected members
11601164

1165+
* `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.**
1166+
* `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.**
1167+
* `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)).
1168+
1169+
Therefore, use `private` by default and `public/protected` when you need to provide access for external classes.
1170+
1171+
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).
1172+
11611173
**Bad:**
11621174

11631175
```php
@@ -1849,7 +1861,7 @@ class Robot implements Employee
18491861

18501862
**Good:**
18511863

1852-
Not every worker is an employee, but every employee is an worker.
1864+
Not every worker is an employee, but every employee is a worker.
18531865

18541866
```php
18551867
interface Workable
@@ -2087,8 +2099,16 @@ function showList(array $employees): void
20872099

20882100
This is also available in other languages:
20892101

2090-
* ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese:**
2091-
* [yangweijie/clean-code-php](https://github.com/yangweijie/clean-code-php)
2102+
* :cn: **Chinese:**
20922103
* [php-cpm/clean-code-php](https://github.com/php-cpm/clean-code-php)
2104+
* :ru: **Russian:**
2105+
* [peter-gribanov/clean-code-php](https://github.com/peter-gribanov/clean-code-php)
2106+
* :es: **Spanish:**
2107+
* [fikoborquez/clean-code-php](https://github.com/fikoborquez/clean-code-php)
2108+
* :brazil: **Portuguese:**
2109+
* [fabioars/clean-code-php](https://github.com/fabioars/clean-code-php)
2110+
* [jeanjar/clean-code-php](https://github.com/jeanjar/clean-code-php/tree/pt-br)
2111+
* :thailand: **Thai:**
2112+
* [panuwizzle/clean-code-php](https://github.com/panuwizzle/clean-code-php)
20932113

20942114
**[⬆ back to top](#table-of-contents)**

0 commit comments

Comments
 (0)