Péhápkaři - Píšeme čitelný kód #4 by Petr Bechyně, Driveto

Petr Bechyně
Petr BechyněCTO, Software Developer at Driveto
Píšeme čitelný
a testovatelný kód #4
Jdete testovat!
Známe se?
Píšeme čitelný a testovatelný kód
• #1: Return null a optional arguments
• #2: SRP v praxi
• #3: Buďte striktní!
Důvody proč nepoužít null nebo prchnout
tisíc mil
V code review či při kontrole vlastního commitu narazíme na:
Důvody proč nepoužít null nebo prchnout
tisíc mil
• Nepředstavuje žádný typ (přenáší chyby)
• Nejasný význam
• Nejednoznačné API tříd
Delegate or die()
• Příliš mnoho závislostí v konstruktoru
• nepředpokládáme, že bychom vstřikovali závilosti mimo konstructor pomocí setterů
• DI kontejner opravdu není jedna závislost 
• Chybí jednotkové testy = to nejde
• Příliš mnoho řádků kódu v jedné metodě
• Není možné 1 větou charakterizovat, co daná služba dělá:
• Tato service odesílá emaily
vs.
Tato service sestaví šablonu emailu, vytvoří záznam ve frontě, odešle email a
zaloguje ho
SRP compliance check
• Při čtení těla metody čtu řádek po řádku
• Pokud je volána závislost (delegování), pak řeknu např. deleguji získání
aktivních knih
SRP compliance check
• Tato metoda vrací knížky z daného období
• Deleguji získání knih (z databáze?)
• Deleguji spočítání dekády z číselníku období
• Volám filtrování knih v seznamu knih (BookList)
a ten vracím
Approved!
SRP compliance
check
• Tato metoda vrací knížky z daného období nebo všechny
• Deleguji získání knih (z databáze?) [1]
• Spočítám první rok dekády [2]
• Iteruji nad knížkami
a vybírám knížky s rokem vydání [3]
• V rámci bloku počítám poslední rok dekády [4]
• Volám filtrování knih v seznamu knih (BookList)
a ten vracím
Rejected!
SRP compliance check
Tato metoda vrací knížky daného
autora
• Deleguji získání knih (z databáze?)
• Iteruji nad získanými knihami a
vybírám autor name
• Vracím získané pole knih
Approved!
A co ten null?
Tak se na to pojďme podívat blíž...
Reference
• Další čtení:
• Robert C. Martin: Clean Code
• https://code.tutsplus.com/tutorials/solid-part-1-the-single-responsibility-principle--net-36074
• Testovací repo:
• https://github.com/tuscanicz/writing-readable-code-4
• Předchozí přednášky:
• 1. null
• Repozitář: https://github.com/driveto/writing-readable-code-1
• Slidy: https://www.slideshare.net/PetrBechyn/phpkai-peme-iteln-kd-1-by-petr-bechyn-driveto
• Záznam: https://www.facebook.com/pehapkari/videos/1616785535037917/
• 2. SRP
• Repozitář: https://github.com/driveto/writing-readable-code-2
• Slidy: https://www.slideshare.net/PetrBechyn/phpkai-peme-iteln-kd-2-by-petr-bechyn-driveto
• Záznam: https://www.facebook.com/pehapkari/videos/1652303308152806/
• 3. Strict
• Repozitář: https://github.com/driveto/writing-readable-code-3
• Slidy: https://www.slideshare.net/PetrBechyn/phpkai-peme-iteln-kd-3-by-tom-blha-driveto
• Záznam: https://www.facebook.com/pehapkari/videos/1679377522112051/
Dotazy
• Dotazy, náměty, připomínky…?
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
1 of 13

Recommended

Glami - jak řešíme performance, aneb jak neshodit web TV reklamou by
Glami - jak řešíme performance, aneb jak neshodit web TV reklamouGlami - jak řešíme performance, aneb jak neshodit web TV reklamou
Glami - jak řešíme performance, aneb jak neshodit web TV reklamouPetr Bechyně
134 views22 slides
Adam Szabo: Jak jsme v Driveto zlepšovali konverze by
Adam Szabo: Jak jsme v Driveto zlepšovali konverzeAdam Szabo: Jak jsme v Driveto zlepšovali konverze
Adam Szabo: Jak jsme v Driveto zlepšovali konverzePetr Bechyně
49 views42 slides
Péhápkaři - Píšeme čitelný a testovatelný kód #5 by Driveto dev team by
Péhápkaři - Píšeme čitelný a testovatelný kód #5 by Driveto dev teamPéhápkaři - Píšeme čitelný a testovatelný kód #5 by Driveto dev team
Péhápkaři - Píšeme čitelný a testovatelný kód #5 by Driveto dev teamPetr Bechyně
144 views24 slides
Doctrine Data migrations | May 2017 by
Doctrine Data migrations | May 2017Doctrine Data migrations | May 2017
Doctrine Data migrations | May 2017Petr Bechyně
37 views15 slides
SEO Restart 2015 - Collabim by
SEO Restart 2015 - CollabimSEO Restart 2015 - Collabim
SEO Restart 2015 - CollabimPetr Bechyně
73 views20 slides
Péhápkaři - Píšeme čitelný kód #3 by Driveto by
Péhápkaři - Píšeme čitelný kód #3 by DrivetoPéhápkaři - Píšeme čitelný kód #3 by Driveto
Péhápkaři - Píšeme čitelný kód #3 by DrivetoPetr Bechyně
184 views37 slides

More Related Content

Featured

ChatGPT and the Future of Work - Clark Boyd by
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
24.3K views69 slides
Getting into the tech field. what next by
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
5.7K views22 slides
Google's Just Not That Into You: Understanding Core Updates & Search Intent by
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
6.4K views99 slides
How to have difficult conversations by
How to have difficult conversations How to have difficult conversations
How to have difficult conversations Rajiv Jayarajah, MAppComm, ACC
5K views19 slides
Introduction to Data Science by
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data ScienceChristy Abraham Joy
82.3K views51 slides
Time Management & Productivity - Best Practices by
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
169.7K views42 slides

Featured(20)

ChatGPT and the Future of Work - Clark Boyd by Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd24.3K views
Getting into the tech field. what next by Tessa Mero
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero5.7K views
Google's Just Not That Into You: Understanding Core Updates & Search Intent by Lily Ray
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray6.4K views
Time Management & Productivity - Best Practices by Vit Horky
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky169.7K views
The six step guide to practical project management by MindGenius
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius36.6K views
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright... by RachelPearson36
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson3612.7K views
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present... by Applitools
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools55.5K views
12 Ways to Increase Your Influence at Work by GetSmarter
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter401.7K views
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G... by DevGAMM Conference
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference3.6K views
Barbie - Brand Strategy Presentation by Erica Santiago
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago25.1K views
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well by Saba Software
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software25.2K views
Introduction to C Programming Language by Simplilearn
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn8.4K views
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr... by Palo Alto Software
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
Palo Alto Software88.4K views
9 Tips for a Work-free Vacation by Weekdone.com
9 Tips for a Work-free Vacation9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation
Weekdone.com7.2K views
How to Map Your Future by SlideShop.com
How to Map Your FutureHow to Map Your Future
How to Map Your Future
SlideShop.com275.1K views

Péhápkaři - Píšeme čitelný kód #4 by Petr Bechyně, Driveto

  • 1. Píšeme čitelný a testovatelný kód #4 Jdete testovat!
  • 2. Známe se? Píšeme čitelný a testovatelný kód • #1: Return null a optional arguments • #2: SRP v praxi • #3: Buďte striktní!
  • 3. Důvody proč nepoužít null nebo prchnout tisíc mil V code review či při kontrole vlastního commitu narazíme na:
  • 4. Důvody proč nepoužít null nebo prchnout tisíc mil • Nepředstavuje žádný typ (přenáší chyby) • Nejasný význam • Nejednoznačné API tříd
  • 5. Delegate or die() • Příliš mnoho závislostí v konstruktoru • nepředpokládáme, že bychom vstřikovali závilosti mimo konstructor pomocí setterů • DI kontejner opravdu není jedna závislost  • Chybí jednotkové testy = to nejde • Příliš mnoho řádků kódu v jedné metodě • Není možné 1 větou charakterizovat, co daná služba dělá: • Tato service odesílá emaily vs. Tato service sestaví šablonu emailu, vytvoří záznam ve frontě, odešle email a zaloguje ho
  • 6. SRP compliance check • Při čtení těla metody čtu řádek po řádku • Pokud je volána závislost (delegování), pak řeknu např. deleguji získání aktivních knih
  • 7. SRP compliance check • Tato metoda vrací knížky z daného období • Deleguji získání knih (z databáze?) • Deleguji spočítání dekády z číselníku období • Volám filtrování knih v seznamu knih (BookList) a ten vracím Approved!
  • 8. SRP compliance check • Tato metoda vrací knížky z daného období nebo všechny • Deleguji získání knih (z databáze?) [1] • Spočítám první rok dekády [2] • Iteruji nad knížkami a vybírám knížky s rokem vydání [3] • V rámci bloku počítám poslední rok dekády [4] • Volám filtrování knih v seznamu knih (BookList) a ten vracím Rejected!
  • 9. SRP compliance check Tato metoda vrací knížky daného autora • Deleguji získání knih (z databáze?) • Iteruji nad získanými knihami a vybírám autor name • Vracím získané pole knih Approved! A co ten null?
  • 10. Tak se na to pojďme podívat blíž...
  • 11. Reference • Další čtení: • Robert C. Martin: Clean Code • https://code.tutsplus.com/tutorials/solid-part-1-the-single-responsibility-principle--net-36074 • Testovací repo: • https://github.com/tuscanicz/writing-readable-code-4 • Předchozí přednášky: • 1. null • Repozitář: https://github.com/driveto/writing-readable-code-1 • Slidy: https://www.slideshare.net/PetrBechyn/phpkai-peme-iteln-kd-1-by-petr-bechyn-driveto • Záznam: https://www.facebook.com/pehapkari/videos/1616785535037917/ • 2. SRP • Repozitář: https://github.com/driveto/writing-readable-code-2 • Slidy: https://www.slideshare.net/PetrBechyn/phpkai-peme-iteln-kd-2-by-petr-bechyn-driveto • Záznam: https://www.facebook.com/pehapkari/videos/1652303308152806/ • 3. Strict • Repozitář: https://github.com/driveto/writing-readable-code-3 • Slidy: https://www.slideshare.net/PetrBechyn/phpkai-peme-iteln-kd-3-by-tom-blha-driveto • Záznam: https://www.facebook.com/pehapkari/videos/1679377522112051/
  • 12. Dotazy • Dotazy, náměty, připomínky…?
  • 13. 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