Skip to content

Commit 2e03a76

Browse files
committed
yoda condition and identical comparison added
1 parent 2d8720c commit 2e03a76

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

README.md

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
* [Avoid Mental Mapping](#avoid-mental-mapping)
1515
* [Don't add unneeded context](#dont-add-unneeded-context)
1616
* [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)
1821
* [Function arguments (2 or fewer ideally)](#function-arguments-2-or-fewer-ideally)
1922
* [Functions should do one thing](#functions-should-do-one-thing)
2023
* [Function names should say what they do](#function-names-should-say-what-they-do)
@@ -29,20 +32,20 @@
2932
* [Avoid type-checking (part 1)](#avoid-type-checking-part-1)
3033
* [Avoid type-checking (part 2)](#avoid-type-checking-part-2)
3134
* [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)
3336
* [Use object encapsulation](#use-object-encapsulation)
3437
* [Make objects have private/protected members](#make-objects-have-privateprotected-members)
35-
5. [Classes](#classes)
38+
6. [Classes](#classes)
3639
* [Prefer composition over inheritance](#prefer-composition-over-inheritance)
3740
* [Avoid fluent interfaces](#avoid-fluent-interfaces)
38-
6. [SOLID](#solid)
41+
7. [SOLID](#solid)
3942
* [Single Responsibility Principle (SRP)](#single-responsibility-principle-srp)
4043
* [Open/Closed Principle (OCP)](#openclosed-principle-ocp)
4144
* [Liskov Substitution Principle (LSP)](#liskov-substitution-principle-lsp)
4245
* [Interface Segregation Principle (ISP)](#interface-segregation-principle-isp)
4346
* [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)
4649

4750
## Introduction
4851

@@ -386,6 +389,59 @@ function createMicrobrewery(string $breweryName = 'Hipster Brew Co.'): void
386389

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

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+
389445
## Functions
390446

391447
### Function arguments (2 or fewer ideally)

0 commit comments

Comments
 (0)