SlideShare uma empresa Scribd logo
1 de 45
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Peter Kofler
 
Geecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelieversGeecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelieversBruno Bossola
 
Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Peter Kofler
 
Letter to a Junior Developer: The Engineering Side of Programming
Letter to a Junior Developer: The Engineering Side of ProgrammingLetter to a Junior Developer: The Engineering Side of Programming
Letter to a Junior Developer: The Engineering Side of ProgrammingLazar Kovacevic
 
Refactoring the Tennis Kata (2013)
Refactoring the Tennis Kata (2013)Refactoring the Tennis Kata (2013)
Refactoring the Tennis Kata (2013)Peter Kofler
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Peter Kofler
 
Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Peter Kofler
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming GoWeng Wei
 
Microservices, the lean way
Microservices, the lean wayMicroservices, the lean way
Microservices, the lean wayBruno Bossola
 
Common design patterns in php
Common design patterns in phpCommon design patterns in php
Common design patterns in phpDavid Stockton
 
PHP Code Quality
PHP Code QualityPHP Code Quality
PHP Code QualityUsman Zafar
 
The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)Peter Kofler
 
What is the best programming language for your web product?
What is the best programming language for your web product?What is the best programming language for your web product?
What is the best programming language for your web product?MobiDev
 
Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)Peter Kofler
 
Architecture @ Appknox
Architecture @ AppknoxArchitecture @ Appknox
Architecture @ AppknoxDhilipsiva DS
 
Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)Peter Kofler
 

Mais procurados (20)

Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)
 
Geecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelieversGeecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelievers
 
Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)
 
JetBrains MPS
JetBrains MPSJetBrains MPS
JetBrains MPS
 
Letter to a Junior Developer: The Engineering Side of Programming
Letter to a Junior Developer: The Engineering Side of ProgrammingLetter to a Junior Developer: The Engineering Side of Programming
Letter to a Junior Developer: The Engineering Side of Programming
 
Refactoring the Tennis Kata (2013)
Refactoring the Tennis Kata (2013)Refactoring the Tennis Kata (2013)
Refactoring the Tennis Kata (2013)
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)
 
Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming Go
 
Microservices, the lean way
Microservices, the lean wayMicroservices, the lean way
Microservices, the lean way
 
Phpers day 2019
Phpers day 2019Phpers day 2019
Phpers day 2019
 
Common design patterns in php
Common design patterns in phpCommon design patterns in php
Common design patterns in php
 
PHP Code Quality
PHP Code QualityPHP Code Quality
PHP Code Quality
 
The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)
 
BDD with Behat
BDD with BehatBDD with Behat
BDD with Behat
 
What is the best programming language for your web product?
What is the best programming language for your web product?What is the best programming language for your web product?
What is the best programming language for your web product?
 
Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)
 
Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)
 
Architecture @ Appknox
Architecture @ AppknoxArchitecture @ Appknox
Architecture @ Appknox
 
Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)
 

Semelhante a Good Code Bad Code Tips Clean PHP

高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUU高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUUSu Jan
 
Linters for frontend code review
Linters for frontend code reviewLinters for frontend code review
Linters for frontend code reviewVsevolod Nechaev
 
Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NETDror Helper
 
How to write good comments
How to write good commentsHow to write good comments
How to write good commentsPeter Hilton
 
You cant be agile if your code sucks
You cant be agile if your code sucksYou cant be agile if your code sucks
You cant be agile if your code sucksPeter Gfader
 
Why are you coding like this?
Why are you coding like this?Why are you coding like this?
Why are you coding like this?Yoeung Vibol
 
Managing Technical Debt
Managing Technical DebtManaging Technical Debt
Managing Technical DebtAdam Culp
 
PHP Variables & Comments 01
PHP Variables & Comments 01PHP Variables & Comments 01
PHP Variables & Comments 01Spy Seat
 
What Do Developers Discuss about Code Comments?
What Do Developers Discuss about Code Comments?What Do Developers Discuss about Code Comments?
What Do Developers Discuss about Code Comments?Pooja Rani
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEGavin Pickin
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017Ortus Solutions, Corp
 
How to write good quality code
How to write good quality codeHow to write good quality code
How to write good quality codeHayden Bleasel
 
Writing code for others
Writing code for othersWriting code for others
Writing code for othersAmol Pujari
 
API Centric Development in PHP
API Centric Development in PHPAPI Centric Development in PHP
API Centric Development in PHPJoe Stagner
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementorArc & Codementor
 
Comment soup with a pinch of types, served in a leaky bowl
Comment soup with a pinch of types, served in a leaky bowlComment soup with a pinch of types, served in a leaky bowl
Comment soup with a pinch of types, served in a leaky bowlPharo
 
Documenting code yapceu2016
Documenting code yapceu2016Documenting code yapceu2016
Documenting code yapceu2016Søren Lund
 

Semelhante a Good Code Bad Code Tips Clean PHP (20)

高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUU高品質軟體的基本動作 101 + 102 for NUU
高品質軟體的基本動作 101 + 102 for NUU
 
Linters for frontend code review
Linters for frontend code reviewLinters for frontend code review
Linters for frontend code review
 
Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NET
 
How to write good comments
How to write good commentsHow to write good comments
How to write good comments
 
You cant be agile if your code sucks
You cant be agile if your code sucksYou cant be agile if your code sucks
You cant be agile if your code sucks
 
Why are you coding like this?
Why are you coding like this?Why are you coding like this?
Why are you coding like this?
 
Managing Technical Debt
Managing Technical DebtManaging Technical Debt
Managing Technical Debt
 
PHP Variables & Comments 01
PHP Variables & Comments 01PHP Variables & Comments 01
PHP Variables & Comments 01
 
Code Review
Code ReviewCode Review
Code Review
 
What Do Developers Discuss about Code Comments?
What Do Developers Discuss about Code Comments?What Do Developers Discuss about Code Comments?
What Do Developers Discuss about Code Comments?
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
 
How to write good quality code
How to write good quality codeHow to write good quality code
How to write good quality code
 
Writing code for others
Writing code for othersWriting code for others
Writing code for others
 
API Centric Development in PHP
API Centric Development in PHPAPI Centric Development in PHP
API Centric Development in PHP
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementor
 
Introduction to-php
Introduction to-phpIntroduction to-php
Introduction to-php
 
Make a better with clean code
Make a better with clean codeMake a better with clean code
Make a better with clean code
 
Comment soup with a pinch of types, served in a leaky bowl
Comment soup with a pinch of types, served in a leaky bowlComment soup with a pinch of types, served in a leaky bowl
Comment soup with a pinch of types, served in a leaky bowl
 
Documenting code yapceu2016
Documenting code yapceu2016Documenting code yapceu2016
Documenting code yapceu2016
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Último (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Good Code Bad Code Tips Clean PHP

  • 1. Good code, Bad code June 2013 @phplx by Jose da Silva
  • 2. Agenda ● WTF as a Metric ● Clean Code ● Bad Code Smells ● Meaningful Names and Formatting ● Comments ● Functions ● Error Handling ● Your time :)
  • 3. 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
  • 4. WTF as a metric
  • 5. How many WTF are enough ?
  • 7. 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
  • 8. Clean Code What's clean code anyway ?
  • 9. Clean Code What's clean code anyway ?
  • 10. Clean Code What's clean code anyway ?
  • 11. Clean Code What's clean code anyway ?
  • 12. Clean Code What's clean code anyway ? Formatted , Organized and Easy to read/modify
  • 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 cost of owning a mess http://www.informit.com/articles/article.aspx?p=1235624&seqNum=3
  • 17. 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
  • 18. Bad Code smells ● Looks familiar ?
  • 19. Meaningful Names ● Self explanatory ○ Variables, classes, functions ● Context aware ● Easy to understand by anyone ● No comments needed
  • 21. Meaningful Names 1. Bad variable name $n 2. Bad variable name $t 3. Dispensable comments 4. Missing code indentation
  • 22. Meaningful Names 1. Bad variable name $n 2. Bad variable name $t 3. Dispensable comments 4. Missing code indentation
  • 23. 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
  • 25. Comments Do's and don't 1. Localized comments and names ? 2. Comment adds nothing to the code
  • 26. Comments Do's and don't 1. Localized comments and names ? 2. Comment adds nothing to the code
  • 27. Comments Do's and don't 1. Inoffensive comments ? 2. Clear code to read ?
  • 28. Comments Do's and don'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
  • 31. Functions Do ONE thing Function purpose : 1. Return parts list 2. Maintain cache expiration control ?
  • 32. Functions Do ONE thing Function purpose : 1. Return parts list
  • 33. Error Handling 1. Use Exceptions instead of returning values for error handling 2. Don't return 'null' 3. Don't pass 'null'
  • 34. Error Handling - Exceptions
  • 36. Error Handling Duplicate code Null values as error handling
  • 37. Error Handling No null returning 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 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
  • 42. Recap : Clean Code What's clean code anyway ? Formatted , Organized and Easy to read/modify
  • 43. 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/
  • 44. 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
  • 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