Good code, Bad Code

45
Good code, Bad code June 2013 @phplx by Jose da Silva

description

 

Transcript of Good code, Bad Code

Page 1: Good code, Bad Code

Good code, Bad codeJune 2013 @phplx by Jose da Silva

Page 2: Good code, Bad Code

Agenda● WTF as a Metric● Clean Code● Bad Code Smells● Meaningful Names and Formatting● Comments● Functions● Error Handling● Your time :)

Page 3: Good code, Bad Code

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 y

Twitter: josedasilva

Linkedin: linkedin.com/in/josedasilva

Email: [email protected]

Page 4: Good code, Bad Code

WTF as a metric

Page 5: Good code, Bad Code

How many WTF are enough ?

Page 6: Good code, Bad Code

WTF of the day

Page 7: Good code, Bad Code

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

Page 8: Good code, Bad Code

Clean CodeWhat's clean code anyway ?

Page 9: Good code, Bad Code

Clean CodeWhat's clean code anyway ?

Page 10: Good code, Bad Code

Clean CodeWhat's clean code anyway ?

Page 11: Good code, Bad Code

Clean CodeWhat's clean code anyway ?

Page 12: Good code, Bad Code

Clean CodeWhat's clean code anyway ?

Formatted , Organized and Easy to read/modify

Page 13: Good code, Bad Code

Clean CodeAdvantages

Page 14: Good code, Bad Code

Clean CodeAdvantages

● Readable● Understandable● Less errors● Easy to maintain ( fix, modify, learn, support)● Testable

Page 15: Good code, Bad Code

Clean CodeAdvantages

● Readable● Understandable● Less errors● Easy to maintain ( fix, modify, learn, support)● Testable

Hint: Time = Money

Page 16: Good code, Bad Code

Clean CodeThe cost of owning a mess

http://www.informit.com/articles/article.aspx?p=1235624&seqNum=3

Page 17: Good code, Bad Code

Bad Code smellsMostly 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

Page 18: Good code, Bad Code

Bad Code smells● Looks familiar ?

Page 19: Good code, Bad Code

Meaningful Names

● Self explanatory ○ Variables, classes, functions

● Context aware ● Easy to understand by anyone● No comments needed

Page 20: Good code, Bad Code

Meaningful Names

Page 21: Good code, Bad Code

Meaningful Names

1. Bad variable name $n2. Bad variable name $t3. Dispensable comments4. Missing code indentation

Page 22: Good code, Bad Code

Meaningful Names

1. Bad variable name $n2. Bad variable name $t3. Dispensable comments4. Missing code indentation

Page 23: Good code, Bad Code

CommentsThe 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

Page 24: Good code, Bad Code

Comments Do's and don't

Page 25: Good code, Bad Code

Comments Do's and don't

1. Localized comments and names ?

2. Comment adds nothing to the code

Page 26: Good code, Bad Code

Comments Do's and don't

1. Localized comments and names ?2. Comment adds nothing to the

code

Page 27: Good code, Bad Code

Comments Do's and don't

1. Inoffensive comments ?2. Clear code to read ?

Page 28: Good code, Bad Code

Comments Do's and don't

1. Inoffensive comments ?2. Clear code to read ?

No comments needed !!!

Page 29: Good code, Bad Code

Functions● Small ( 1K lines of code is not small !!! )

● Do ONE thing

● Reduced number of arguments

● No side effects

Page 30: Good code, Bad Code

FunctionsDo ONE thing

Page 31: Good code, Bad Code

FunctionsDo ONE thing

Function purpose :

1. Return parts list2. Maintain cache expiration

control ?

Page 32: Good code, Bad Code

FunctionsDo ONE thing

Function purpose :

1. Return parts list

Page 33: Good code, Bad Code

Error Handling1. Use Exceptions instead of returning values

for error handling2. Don't return 'null'3. Don't pass 'null'

Page 34: Good code, Bad Code

Error Handling - Exceptions

Page 35: Good code, Bad Code

Error HandlingDuplicate code

Page 36: Good code, Bad Code

Error HandlingDuplicate code

Null values as error handling

Page 37: Good code, Bad Code

Error Handling

No null returning valuesCentralized error handling

Page 38: Good code, Bad Code

Your best friends = ToolsPHPLOC by Sebastian Bergmanhttps://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/

Page 39: Good code, Bad Code

Your best friends = ToolsPHPMD by Manuel Pichlerhttp://phpmd.org/

Code Size Rules

Design Rules

Naming Rules

Unused Code Rules

Excessive Parameter List

...

Page 40: Good code, Bad Code

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/

Page 41: Good code, Bad Code

How to keep your code clean ?1. Refactor2. Refactor3. Refactor4. Frequent Peer Reviews5. Follow Naming Conventions6. Don't comment and leave code behind, just

DELETE it7. Use the tools often (PHPLOC, PHPMD, PHPCodeSniffer)8. Keep your logs shell open all the time

Page 42: Good code, Bad Code

Recap : Clean CodeWhat's clean code anyway ?

Formatted , Organized and Easy to read/modify

Page 43: Good code, Bad Code

ReferencesClean Code by Hendrik Ebel http://www.slideshare.net/hebel/clean-code

You code sucks, let's fix it by Rafael Dohmshttp://www.slideshare.net/rdohms/bettercode-phpbenelux212alternate

Clean Code Barcelona 2009 by Jan W. Eshuishttp://www.slideshare.net/jwenl/clean-code-barcelona-2009

http://techportal.inviqa.com/2010/01/28/phploc-php-lines-of-code/

Page 44: Good code, Bad Code

We're hiring1. 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!

[email protected]

Page 45: Good code, Bad Code

Your time

Questions ? Comments ? Both ?

Thank you!

Feedback welcomedhttps://joind.in/talk/view/8815

Twitter: josedasilva

Linkedin: linkedin.com/in/josedasilva

Email: [email protected]