Clean code

799 views

Published on

How to create clean PHP code. This is a presentation made by Dmitry Dulepov at TYPO3 developer days in July 2011 in Sursee, Switzerland.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
799
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Clean code

    1. 1. Clean code Dmitry DulepovSnowflake Productions Gmbh
    2. 2. Who am I? • TYPO3 core and security teams member • From Latvia (Lettland) • Work with TYPO3 since 2003 • 38 years old • 38 projects on Forge (RealURL, ...) • Work for Snowflake Productions Gmbh • 1 wife, 2 kids, 1 cat, 1 dog, no coffee machine but “retro” MacBook Pro
    3. 3. Unclean code Clean code
    4. 4. Unclean codeWTF? Clean code
    5. 5. You have 5 seconds!
    6. 6. You have 0 seconds!
    7. 7. You have 5 seconds!
    8. 8. You have 0 seconds!
    9. 9. if (t3lib_div::testInt($theID)) { $theTable = trim($parts[1]) ? strrev(trim($parts[1])) : ($this->secondTable &&$theID < 0 ? $this->secondTable : $this->firstTable); if ((string) $theID != && $theID && $theTable && isset($this->tableArray[$theTable])) { $theID = $this->secondTable ? abs(intval($theID)) : intval($theID); $this->itemArray[$key][id] = $theID; $this->itemArray[$key][table] = $theTable; $this->tableArray[$theTable][] = $theID; $isSet = 1; }}public function getLanguageData($languageFilePath, $languageKey) { $this->initCharsets($languageKey); $this->generateCacheFile($languageFilePath, $languageKey); $languageLabels = $this->getFromCacheFile(); return $languageLabels;}
    10. 10. Clean code: advantages
    11. 11. Clean code: advantagesReadable
    12. 12. Clean code: advantagesReadableUnderstandable (“time is money, baby!”)
    13. 13. Clean code: advantagesReadableUnderstandable (“time is money, baby!”)Less errors
    14. 14. Clean code: advantagesReadableUnderstandable (“time is money, baby!”)Less errorsEasy to fix
    15. 15. Clean code: advantagesReadableUnderstandable (“time is money, baby!”)Less errorsEasy to fixEasy to modify
    16. 16. Clean code: advantagesReadableUnderstandable (“time is money, baby!”)Less errorsEasy to fixEasy to modifyEasy to learn
    17. 17. Clean code: advantagesReadableUnderstandable (“time is money, baby!”)Less errorsEasy to fixEasy to modifyEasy to learnEasy to support
    18. 18. How do I write clean code?
    19. 19. It is easy!...if you know certain rules.
    20. 20. Rules• Naming• Functions• Comments• Classes
    21. 21. NamingConform to CGL
    22. 22. NamingConform to CGL $pid_value $thePID $thePid $pid $PID
    23. 23. NamingAvoid mental mapping
    24. 24. NamingAvoid mental mapping $rS $tempdata $curValueRec and $curValue foreach ($valueArray as $kk => $vv) { $dec = array_pop($valueArray) addPItoST43
    25. 25. NamingDon’t be cute!
    26. 26. NamingDon’t be cute! $crocodile = $dog * $bird $maradona function killEmAll() {
    27. 27. NamingBe consistent!
    28. 28. NamingBe consistent!$pageNummer// Эта функция загружает список строк
    29. 29. FunctionsSmall. t3lib_tcemain::process_cmdmap() 377 lines (7 pages, 4,575px height)
    30. 30. FunctionsDo one thing. public function getLanguageData($languageFilePath, $languageKey) { $this->initCharsets($languageKey); $this->generateCacheFile($languageFilePath, $languageKey); $languageLabels = $this->getFromCacheFile(); return $languageLabels; }
    31. 31. FunctionsAvoid switches public function foo($key) { switch ($key) { case 1: ... case 1001: ... default: throw RuntimeException(‘Key not found’); } }
    32. 32. Functions Few argumentspi_linkTP_keepPIvars($str, $overrulePIvars=array(), $cache=0, $clearAnyway=0,$altPageId=0)implodeArrayForUrl($name, $theArray, $str=, $skipBlank=0,$rawurlencodeParamName=0)addPItoST43($key, $classFile = , $prefix = , $type = list_type, $cached = 0)
    33. 33. FunctionsNo side effects protected function ensureFileExist($fileName) { $fileStatus = $this->cache->getItem($fileName); if (!is_bool($fileStatus)) { $this->cache->clearAll(); } ... }
    34. 34. FunctionsDo not use output arguments protected function doSomething(&$output) { ... }
    35. 35. FunctionsPrefer exceptions protected function getURL($url) { if (!$this->connectToHost($url)) { throw ... } } protected function getFromCache($key) { if (!$this->cache->exists($key)) { throw ... } }
    36. 36. FunctionsPrefer exceptions protected function getURL($url) { if (!$this->connectToHost($url)) { throw ... } } protected function getFromCache($key) { if (!$this->cache->exists($key)) { throw ... } }
    37. 37. CommentsAre they good or bad?
    38. 38. ClassesSmall. t3lib/class.t3lib_div.php: 6095 lines t3lib/class.t3lib_tcemain.php: 7831 lines t3lib/class.t3lib_tceforms.php: 6440 lines
    39. 39. ClassesCohesion. Wikipedia: Cohesion is a measure of how strongly- related or focused the responsibilities of a single module are.
    40. 40. ClassesAvoid dependency magnets. class BaseForm { const SEARCH_FORM = 1; const PROFILE_FORM = 2; } class SearchForm extends BaseForm { ...
    41. 41. Classes Area of responsibility$thumbnailFileName = foo($imageFileName);$thumbnail = ...makeInstance(..., $thumbnailFileName);$thumbnail = ...makeInstance(..., $imageFileName);$thumbnailFileName = $thumbnail->getFileName();
    42. 42. Too much?
    43. 43. Too much? Silver bullet point:Make your code readable.
    44. 44. How to make clean code?
    45. 45. How to make clean code?• Existing code: refactor• New code: just write it clean!
    46. 46. Let’s practice!

    ×