Good code, Bad code
June 2013 @phplx by Jose da Silva
Agenda
● WTF as a Metric
● Clean Code
● Bad Code Smells
● Meaningful Names and Formatting
● Comments
● Functions
● Error Handling
● Your time :)
About me
Jose 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 y
Twitter: josedasilva
Linkedin: linkedin.com/in/josedasilva
Email: josedasilva@gmail.com
WTF as a metric
How many WTF are enough ?
WTF of the day
How to fix the WTF issue ?
The developer's only amendment to fix the WTF problem
1. Complain less
+
2. Fix the code
+
3. Remove the unneeded WTF
Clean Code
What's clean code anyway ?
Clean Code
What's clean code anyway ?
Clean Code
What's clean code anyway ?
Clean Code
What's clean code anyway ?
Clean Code
What's clean code anyway ?
Formatted , Organized and Easy to read/modify
Clean Code
Advantages
Clean Code
Advantages
● Readable
● Understandable
● Less errors
● Easy to maintain ( fix, modify, learn, support)
● Testable
Clean Code
Advantages
● Readable
● Understandable
● Less errors
● Easy to maintain ( fix, modify, learn, support)
● Testable
Hint: Time = Money
Clean Code
The cost of owning a mess
http://www.informit.com/articles/article.aspx?p=1235624&seqNum=3
Bad Code smells
Mostly found on legacy code
● No separation between PHP and HTML
● Lots of requires
● Global Variables
● No Database Abstraction Layer
http://www.sxc.hu/photo/967754
Bad Code smells
● Looks familiar ?
Meaningful Names
● Self explanatory
○ Variables, classes, functions
● Context aware
● Easy to understand by anyone
● No comments needed
Meaningful Names
Meaningful Names
1. Bad variable name $n
2. Bad variable name $t
3. Dispensable comments
4. Missing code indentation
Meaningful Names
1. Bad variable name $n
2. Bad variable name $t
3. Dispensable comments
4. Missing code indentation
Comments
The main purpose of comment is to explain
code.
Please don't use a comment when you can use a
function or variable.
Comments can contain lines
Comments Do's and don't
Comments Do's and don't
1. Localized comments and names
?
2. Comment adds nothing to the
code
Comments Do's and don't
1. Localized comments and names ?
2. Comment adds nothing to the
code
Comments Do's and don't
1. Inoffensive comments ?
2. Clear code to read ?
Comments Do's and don't
1. Inoffensive comments ?
2. Clear code to read ?
No comments needed !!!
Functions
● Small ( 1K lines of code is not small !!! )
● Do ONE thing
● Reduced number of arguments
● No side effects
Functions
Do ONE thing
Functions
Do ONE thing
Function purpose :
1. Return parts list
2. Maintain cache expiration
control ?
Functions
Do ONE thing
Function purpose :
1. Return parts list
Error Handling
1. Use Exceptions instead of returning values
for error handling
2. Don't return 'null'
3. Don't pass 'null'
Error Handling - Exceptions
Error Handling
Duplicate code
Error Handling
Duplicate code
Null values as error handling
Error Handling
No null returning values
Centralized error handling
Your best friends = Tools
PHPLOC by Sebastian Bergman
https://github.
com/sebastianbergmann/phploc
Quick analysis on the status of the code
Cyclomatic Complexity Analysis
Comment lines of code
Lines of code / Number of methods
... http://techportal.inviqa.com/2010/01/28/phploc-php-lines-of-code/
Your best friends = Tools
PHPMD by Manuel Pichler
http://phpmd.org/
Code Size Rules
Design Rules
Naming Rules
Unused Code Rules
Excessive Parameter List
...
Your best friends = Tools
PHP CodeSniffer
http://pear.php.net/package/PHP_CodeSniffer/
Helps you to keep your code readable.
http://techportal.inviqa.com/2009/10/12/usphp_code_sniffer/
How to keep your code clean ?
1. Refactor
2. Refactor
3. Refactor
4. Frequent Peer Reviews
5. Follow Naming Conventions
6. Don't comment and leave code behind, just
DELETE it
7. Use the tools often (PHPLOC, PHPMD, PHPCodeSniffer)
8. Keep your logs shell open all the time
Recap : Clean Code
What's clean code anyway ?
Formatted , Organized and Easy to read/modify
References
Clean Code by Hendrik Ebel
http://www.slideshare.net/hebel/clean-code
You code sucks, let's fix it by Rafael Dohms
http://www.slideshare.net/rdohms/bettercode-phpbenelux212alternate
Clean Code Barcelona 2009 by Jan W. Eshuis
http://www.slideshare.net/jwenl/clean-code-barcelona-2009
http://techportal.inviqa.com/2010/01/28/phploc-php-lines-of-code/
We're hiring
1. Back-end PHP Engineer
2. Front-end Engineer
3. Software Tester (QA)
4. System Administrator
If you are interested or know someone who, just contact me!
jose@guestcentric.com
Your time
Questions ? Comments ? Both ?
Thank you!
Feedback welcomed
https://joind.in/talk/view/8815
Twitter: josedasilva
Linkedin: linkedin.com/in/josedasilva
Email: josedasilva@gmail.com

Good code, Bad Code

  • 1.
    Good code, Badcode June 2013 @phplx by Jose da Silva
  • 2.
    Agenda ● WTF asa Metric ● Clean Code ● Bad Code Smells ● Meaningful Names and Formatting ● Comments ● Functions ● Error Handling ● Your time :)
  • 3.
    About me Jose daSilva ● 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 y Twitter: josedasilva Linkedin: linkedin.com/in/josedasilva Email: josedasilva@gmail.com
  • 4.
    WTF as ametric
  • 5.
    How many WTFare enough ?
  • 6.
  • 7.
    How to fixthe WTF issue ? The developer's only amendment to fix the WTF problem 1. Complain less + 2. Fix the code + 3. Remove the unneeded WTF
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
    Clean Code What's cleancode anyway ? Formatted , Organized and Easy to read/modify
  • 13.
  • 14.
    Clean Code Advantages ● Readable ●Understandable ● Less errors ● Easy to maintain ( fix, modify, learn, support) ● Testable
  • 15.
    Clean Code Advantages ● Readable ●Understandable ● Less errors ● Easy to maintain ( fix, modify, learn, support) ● Testable Hint: Time = Money
  • 16.
    Clean Code The costof owning a mess http://www.informit.com/articles/article.aspx?p=1235624&seqNum=3
  • 17.
    Bad Code smells Mostlyfound on legacy code ● No separation between PHP and HTML ● Lots of requires ● Global Variables ● No Database Abstraction Layer http://www.sxc.hu/photo/967754
  • 18.
    Bad Code smells ●Looks familiar ?
  • 19.
    Meaningful Names ● Selfexplanatory ○ Variables, classes, functions ● Context aware ● Easy to understand by anyone ● No comments needed
  • 20.
  • 21.
    Meaningful Names 1. Badvariable name $n 2. Bad variable name $t 3. Dispensable comments 4. Missing code indentation
  • 22.
    Meaningful Names 1. Badvariable name $n 2. Bad variable name $t 3. Dispensable comments 4. Missing code indentation
  • 23.
    Comments The main purposeof comment is to explain code. Please don't use a comment when you can use a function or variable. Comments can contain lines
  • 24.
  • 25.
    Comments Do's anddon't 1. Localized comments and names ? 2. Comment adds nothing to the code
  • 26.
    Comments Do's anddon't 1. Localized comments and names ? 2. Comment adds nothing to the code
  • 27.
    Comments Do's anddon't 1. Inoffensive comments ? 2. Clear code to read ?
  • 28.
    Comments Do's anddon't 1. Inoffensive comments ? 2. Clear code to read ? No comments needed !!!
  • 29.
    Functions ● Small (1K lines of code is not small !!! ) ● Do ONE thing ● Reduced number of arguments ● No side effects
  • 30.
  • 31.
    Functions Do ONE thing Functionpurpose : 1. Return parts list 2. Maintain cache expiration control ?
  • 32.
    Functions Do ONE thing Functionpurpose : 1. Return parts list
  • 33.
    Error Handling 1. UseExceptions instead of returning values for error handling 2. Don't return 'null' 3. Don't pass 'null'
  • 34.
    Error Handling -Exceptions
  • 35.
  • 36.
    Error Handling Duplicate code Nullvalues as error handling
  • 37.
    Error Handling No nullreturning values Centralized error handling
  • 38.
    Your best friends= Tools PHPLOC by Sebastian Bergman https://github. com/sebastianbergmann/phploc Quick analysis on the status of the code Cyclomatic Complexity Analysis Comment lines of code Lines of code / Number of methods ... http://techportal.inviqa.com/2010/01/28/phploc-php-lines-of-code/
  • 39.
    Your best friends= Tools PHPMD by Manuel Pichler http://phpmd.org/ Code Size Rules Design Rules Naming Rules Unused Code Rules Excessive Parameter List ...
  • 40.
    Your best friends= Tools PHP CodeSniffer http://pear.php.net/package/PHP_CodeSniffer/ Helps you to keep your code readable. http://techportal.inviqa.com/2009/10/12/usphp_code_sniffer/
  • 41.
    How to keepyour code clean ? 1. Refactor 2. Refactor 3. Refactor 4. Frequent Peer Reviews 5. Follow Naming Conventions 6. Don't comment and leave code behind, just DELETE it 7. Use the tools often (PHPLOC, PHPMD, PHPCodeSniffer) 8. Keep your logs shell open all the time
  • 42.
    Recap : CleanCode What's clean code anyway ? Formatted , Organized and Easy to read/modify
  • 43.
    References Clean Code byHendrik Ebel http://www.slideshare.net/hebel/clean-code You code sucks, let's fix it by Rafael Dohms http://www.slideshare.net/rdohms/bettercode-phpbenelux212alternate Clean Code Barcelona 2009 by Jan W. Eshuis http://www.slideshare.net/jwenl/clean-code-barcelona-2009 http://techportal.inviqa.com/2010/01/28/phploc-php-lines-of-code/
  • 44.
    We're hiring 1. Back-endPHP Engineer 2. Front-end Engineer 3. Software Tester (QA) 4. System Administrator If you are interested or know someone who, just contact me! jose@guestcentric.com
  • 45.
    Your time Questions ?Comments ? Both ? Thank you! Feedback welcomed https://joind.in/talk/view/8815 Twitter: josedasilva Linkedin: linkedin.com/in/josedasilva Email: josedasilva@gmail.com