SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
Péhápkaři - Píšeme čitelný kód #1 by Petr Bechyně, Driveto
První díl seriálu přednášek o psaní čitelného a testovatelného php od Petra Bechyně z Driveto se zaměřuje na chyby, které vznikají handlováním null a používáním optional arguments.
První díl seriálu přednášek o psaní čitelného a testovatelného php od Petra Bechyně z Driveto se zaměřuje na chyby, které vznikají handlováním null a používáním optional arguments.
Péhápkaři - Píšeme čitelný kód #1 by Petr Bechyně, Driveto
1.
Píšeme čitelný kód #1
Million dollar mistake: nezabrušujte se do if null a optional arguments
2.
Čitelný kód
• dobře maintainovatelný
• snadno rozšiřitelný (= nerozbijeme stávající logiku)
3.
Čitelný kód
Ukázka:
https://github.com/tuscanicz/writing-readable-code-1
4.
Null, těší mě?
„I call it my billion-dollar mistake.
At that time, I was designing the first comprehensive type system for references in an object-oriented
language.
My goal was to ensure that all use of references should be absolutely safe, with checking performed
automatically by the compiler. But I couldn’t resist the temptation to put in a null reference,
simply because it was so easy to implement.
This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused
a billion dollars of pain and damage in the last forty years.“
Tony Hoare, vynálezce null reference
5.
Null, těší mě?
• NULL, nil, null, None, Nothing, Nil, nullptr
• Hodnota, co není hodnota
• Co je zle?
• Nepředstavuje žádný typ (string, int)
• potichu ale umí projít přes jakýkoliv typehint (?User $user)
• Má nejasný význam
• if ($string === null || $string === "") { … }
• Jazykové konstrukty situaci ještě zhoršují (isset(), etc)
• Představuje nejednoznačné APIs
• SklikApiService::getKeywordSuggestions(): ?KeywordSuggestionList
• e.g.: UserControllerFacade::createUser(): ?User
6.
Null, těší mě?
• NULL, nil, null, None, Nothing, Nil, nullptr
• Hodnota, co není hodnota
• Co je zle?
• Nepředstavuje žádný typ (string, int)
• potichu ale umí projít přes jakýkoliv typehint (?User $user)
• Má nejasný význam
• if ($string === null || $string === "") { … }
• Jazykové konstrukty situaci ještě zhoršují (isset(), etc)
• Představuje nejednoznačné APIs
• SklikApiService::getKeywordSuggestions(): ?KeywordSuggestionList
• e.g.: UserControllerFacade::createUser(): ?User
7.
Null, těší mě?
• NULL, nil, null, None, Nothing, Nil, nullptr
• Hodnota, co není hodnota
• Co je zle?
• Nepředstavuje žádný typ (string, int)
• potichu ale umí projít přes jakýkoliv typehint (?User $user)
• Má nejasný význam
• if ($string === null || $string === "") { … }
• Jazykové konstrukty situaci ještě zhoršují (isset(), etc)
• Představuje nejednoznačné APIs
• SklikApiService::getKeywordSuggestions(): ?KeywordSuggestionList
• e.g.: UserControllerFacade::createUser(): ?User
8.
Null, těší mě?
• NULL, nil, null, None, Nothing, Nil, nullptr
• Hodnota, co není hodnota
• Co je zle?
• Nepředstavuje žádný typ (string, int)
• potichu ale umí projít přes jakýkoliv typehint (?User $user)
• Má nejasný význam
• if ($string === null || $string === "") { … }
• Jazykové konstrukty situaci ještě zhoršují (isset(), etc)
• Představuje nejednoznačné APIs
• SklikApiService::getKeywordSuggestions(): ?KeywordSuggestionList
• e.g.: UserControllerFacade::createUser(): ?User
9.
Null, těší mě?
• NULL, nil, null, None, Nothing, Nil, nullptr
• Hodnota, co není hodnota
• Co je zle?
• Nepředstavuje žádný typ (string, int)
• potichu ale umí projít přes jakýkoliv typehint (?User $user)
• Má nejasný význam
• if ($string === null || $string === "") { … }
• Jazykové konstrukty situaci ještě zhoršují (isset(), etc)
• Představuje nejednoznačné APIs
• SklikApiService::getKeywordSuggestions(): ?KeywordSuggestionList
• e.g.: UserControllerFacade::createUser(): ?User
10.
Agent null s povolení zabíjet
• Svádí vývojáře používat defaultní stavy
• getRoleByCategory(?Category $category): RoleEnum
...
if ($category === null) {
$category = new Category(CategoryEnum::HOMEPAGE);
}
...
11.
Jak z toho ven?
• Throw Exception (viděli jsme v příkladu)
• Null Objects (viz reference)
• Doctrine entity (ukážeme si)
12.
Kdy zpozornět?
• V code review či při kontrole vlastního commitu:
• return null;
• magicMethodDoingAmazingThings(?string $name): int
• anotherMagicMethod(): ?string
• V logu
• Call to a member function method() on null
15.
Díky za pozornost!
• We are hiring
www.driveto.cz
Petr Bechyně petr.bechyne@driveto.cz
CTO Driveto 736 761 183
github.com/tuscanicz
facebook.com/petrbechynecz
0 likes
Be the first to like this
Views
Total views
429
On SlideShare
0
From Embeds
0
Number of Embeds
0
You have now unlocked unlimited access to 20M+ documents!
Unlimited Reading
Learn faster and smarter from top experts
Unlimited Downloading
Download to take your learnings offline and on the go
You also get free access to Scribd!
Instant access to millions of ebooks, audiobooks, magazines, podcasts and more.
Read and listen offline with any device.
Free access to premium services like Tuneln, Mubi and more.