|
14 | 14 | * [Avoid Mental Mapping](#avoid-mental-mapping) |
15 | 15 | * [Don't add unneeded context](#dont-add-unneeded-context) |
16 | 16 | * [Use default arguments instead of short circuiting or conditionals](#use-default-arguments-instead-of-short-circuiting-or-conditionals) |
17 | | - 3. [Functions](#functions) |
| 17 | + 3. [Comparaison](#comparaison) |
| 18 | + * [Use Yodas conditions](#yoda_condition) |
| 19 | + * [Use identical comparison](#identical_comparison) |
| 20 | + 4. [Functions](#functions) |
18 | 21 | * [Function arguments (2 or fewer ideally)](#function-arguments-2-or-fewer-ideally) |
19 | 22 | * [Functions should do one thing](#functions-should-do-one-thing) |
20 | 23 | * [Function names should say what they do](#function-names-should-say-what-they-do) |
|
29 | 32 | * [Avoid type-checking (part 1)](#avoid-type-checking-part-1) |
30 | 33 | * [Avoid type-checking (part 2)](#avoid-type-checking-part-2) |
31 | 34 | * [Remove dead code](#remove-dead-code) |
32 | | - 4. [Objects and Data Structures](#objects-and-data-structures) |
| 35 | + 5. [Objects and Data Structures](#objects-and-data-structures) |
33 | 36 | * [Use object encapsulation](#use-object-encapsulation) |
34 | 37 | * [Make objects have private/protected members](#make-objects-have-privateprotected-members) |
35 | | - 5. [Classes](#classes) |
| 38 | + 6. [Classes](#classes) |
36 | 39 | * [Prefer composition over inheritance](#prefer-composition-over-inheritance) |
37 | 40 | * [Avoid fluent interfaces](#avoid-fluent-interfaces) |
38 | | - 6. [SOLID](#solid) |
| 41 | + 7. [SOLID](#solid) |
39 | 42 | * [Single Responsibility Principle (SRP)](#single-responsibility-principle-srp) |
40 | 43 | * [Open/Closed Principle (OCP)](#openclosed-principle-ocp) |
41 | 44 | * [Liskov Substitution Principle (LSP)](#liskov-substitution-principle-lsp) |
42 | 45 | * [Interface Segregation Principle (ISP)](#interface-segregation-principle-isp) |
43 | 46 | * [Dependency Inversion Principle (DIP)](#dependency-inversion-principle-dip) |
44 | | - 7. [Don’t repeat yourself (DRY)](#dont-repeat-yourself-dry) |
45 | | - 8. [Translations](#translations) |
| 47 | + 8. [Don’t repeat yourself (DRY)](#dont-repeat-yourself-dry) |
| 48 | + 9. [Translations](#translations) |
46 | 49 |
|
47 | 50 | ## Introduction |
48 | 51 |
|
@@ -386,6 +389,59 @@ function createMicrobrewery(string $breweryName = 'Hipster Brew Co.'): void |
386 | 389 |
|
387 | 390 | **[⬆ back to top](#table-of-contents)** |
388 | 391 |
|
| 392 | +## Comparison |
| 393 | + |
| 394 | +### Use [Yoda conditions](https://en.wikipedia.org/wiki/Yoda_conditions) |
| 395 | + |
| 396 | +A Yoda condition places the constant portion of the expression on the left side of the conditional statement |
| 397 | +Yoda conditions are part of the [WordPress](https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/) and [Symfony coding standards](http://symfony.com/doc/current/contributing/code/standards.html). |
| 398 | + |
| 399 | +**Not good:** |
| 400 | + |
| 401 | + |
| 402 | +```php |
| 403 | +if ( $value == 42) |
| 404 | +{ |
| 405 | + // ... |
| 406 | +} |
| 407 | +``` |
| 408 | +Reads like: "If the value equal to 42..." |
| 409 | + |
| 410 | +**Good:** |
| 411 | + |
| 412 | + |
| 413 | +```php |
| 414 | +if ( 42 == $value) |
| 415 | +{ |
| 416 | + // ... |
| 417 | +} |
| 418 | +``` |
| 419 | +Reads like: "If the 42 equal to $value and it's avoid a common mistake 42 = $value" |
| 420 | + |
| 421 | +**[⬆ back to top](#table-of-contents)** |
| 422 | + |
| 423 | +### Use [identical comparison](http://php.net/manual/en/language.operators.comparison.php) |
| 424 | + |
| 425 | +**Not good:** |
| 426 | + |
| 427 | +```php |
| 428 | +if( $a == $b ) |
| 429 | +{ |
| 430 | + //... |
| 431 | +} |
| 432 | +``` |
| 433 | + |
| 434 | +**Good:** |
| 435 | + |
| 436 | +```php |
| 437 | +if( $a === $b ) |
| 438 | +{ |
| 439 | + //... |
| 440 | +} |
| 441 | +``` |
| 442 | +**[⬆ back to top](#table-of-contents)** |
| 443 | + |
| 444 | + |
389 | 445 | ## Functions |
390 | 446 |
|
391 | 447 | ### Function arguments (2 or fewer ideally) |
|
0 commit comments