Péhápkaři - Píšeme čitelný kód #1 by Petr Bechyně, Driveto
Mar. 8, 2018•0 likes•427 views
Download to read offline
Report
Software
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.
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