Good code, Bad Code

2,662 views
2,437 views

Published on

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

No Downloads
Views
Total views
2,662
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
43
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Good code, Bad Code

  1. 1. Good code, Bad codeJune 2013 @phplx by Jose da Silva
  2. 2. Agenda● WTF as a Metric● Clean Code● Bad Code Smells● Meaningful Names and Formatting● Comments● Functions● Error Handling● Your time :)
  3. 3. About meJose da Silva● VP of Engineering @ GuestCentric● Doing good and bad PHP since PHP 3 (1999)● Zend Certified Engineer + Certified Scrum Master● Open Source○ Slimframework.com contributor● Hobbies○ Table tennis player for more than 20 yTwitter: josedasilvaLinkedin: linkedin.com/in/josedasilvaEmail: josedasilva@gmail.com
  4. 4. WTF as a metric
  5. 5. How many WTF are enough ?
  6. 6. WTF of the day
  7. 7. How to fix the WTF issue ?The developers only amendment to fix the WTF problem1. Complain less+2. Fix the code+3. Remove the unneeded WTF
  8. 8. Clean CodeWhats clean code anyway ?
  9. 9. Clean CodeWhats clean code anyway ?
  10. 10. Clean CodeWhats clean code anyway ?
  11. 11. Clean CodeWhats clean code anyway ?
  12. 12. Clean CodeWhats clean code anyway ?Formatted , Organized and Easy to read/modify
  13. 13. Clean CodeAdvantages
  14. 14. Clean CodeAdvantages● Readable● Understandable● Less errors● Easy to maintain ( fix, modify, learn, support)● Testable
  15. 15. Clean CodeAdvantages● Readable● Understandable● Less errors● Easy to maintain ( fix, modify, learn, support)● TestableHint: Time = Money
  16. 16. Clean CodeThe cost of owning a messhttp://www.informit.com/articles/article.aspx?p=1235624&seqNum=3
  17. 17. Bad Code smellsMostly found on legacy code● No separation between PHP and HTML● Lots of requires● Global Variables● No Database Abstraction Layerhttp://www.sxc.hu/photo/967754
  18. 18. Bad Code smells● Looks familiar ?
  19. 19. Meaningful Names● Self explanatory○ Variables, classes, functions● Context aware● Easy to understand by anyone● No comments needed
  20. 20. Meaningful Names
  21. 21. Meaningful Names1. Bad variable name $n2. Bad variable name $t3. Dispensable comments4. Missing code indentation
  22. 22. Meaningful Names1. Bad variable name $n2. Bad variable name $t3. Dispensable comments4. Missing code indentation
  23. 23. CommentsThe main purpose of comment is to explaincode.Please dont use a comment when you can use afunction or variable.Comments can contain lines
  24. 24. Comments Dos and dont
  25. 25. Comments Dos and dont1. Localized comments and names?2. Comment adds nothing to thecode
  26. 26. Comments Dos and dont1. Localized comments and names ?2. Comment adds nothing to thecode
  27. 27. Comments Dos and dont1. Inoffensive comments ?2. Clear code to read ?
  28. 28. Comments Dos and dont1. Inoffensive comments ?2. Clear code to read ?No comments needed !!!
  29. 29. Functions● Small ( 1K lines of code is not small !!! )● Do ONE thing● Reduced number of arguments● No side effects
  30. 30. FunctionsDo ONE thing
  31. 31. FunctionsDo ONE thingFunction purpose :1. Return parts list2. Maintain cache expirationcontrol ?
  32. 32. FunctionsDo ONE thingFunction purpose :1. Return parts list
  33. 33. Error Handling1. Use Exceptions instead of returning valuesfor error handling2. Dont return null3. Dont pass null
  34. 34. Error Handling - Exceptions
  35. 35. Error HandlingDuplicate code
  36. 36. Error HandlingDuplicate codeNull values as error handling
  37. 37. Error HandlingNo null returning valuesCentralized error handling
  38. 38. Your best friends = ToolsPHPLOC by Sebastian Bergmanhttps://github.com/sebastianbergmann/phplocQuick analysis on the status of the codeCyclomatic Complexity AnalysisComment lines of codeLines of code / Number of methods... http://techportal.inviqa.com/2010/01/28/phploc-php-lines-of-code/
  39. 39. Your best friends = ToolsPHPMD by Manuel Pichlerhttp://phpmd.org/Code Size RulesDesign RulesNaming RulesUnused Code RulesExcessive Parameter List...
  40. 40. Your best friends = ToolsPHP CodeSnifferhttp://pear.php.net/package/PHP_CodeSniffer/Helps you to keep your code readable.http://techportal.inviqa.com/2009/10/12/usphp_code_sniffer/
  41. 41. How to keep your code clean ?1. Refactor2. Refactor3. Refactor4. Frequent Peer Reviews5. Follow Naming Conventions6. Dont comment and leave code behind, justDELETE it7. Use the tools often (PHPLOC, PHPMD, PHPCodeSniffer)8. Keep your logs shell open all the time
  42. 42. Recap : Clean CodeWhats clean code anyway ?Formatted , Organized and Easy to read/modify
  43. 43. ReferencesClean Code by Hendrik Ebelhttp://www.slideshare.net/hebel/clean-codeYou code sucks, lets fix it by Rafael Dohmshttp://www.slideshare.net/rdohms/bettercode-phpbenelux212alternateClean Code Barcelona 2009 by Jan W. Eshuishttp://www.slideshare.net/jwenl/clean-code-barcelona-2009http://techportal.inviqa.com/2010/01/28/phploc-php-lines-of-code/
  44. 44. Were hiring1. Back-end PHP Engineer2. Front-end Engineer3. Software Tester (QA)4. System AdministratorIf you are interested or know someone who, just contact me!jose@guestcentric.com
  45. 45. Your timeQuestions ? Comments ? Both ?Thank you!Feedback welcomedhttps://joind.in/talk/view/8815Twitter: josedasilvaLinkedin: linkedin.com/in/josedasilvaEmail: josedasilva@gmail.com

×