SlideShare a Scribd company logo
© 2019 Magento, Inc. Page 1.1.1
Clean Code in PHP
© 2019 Magento, Inc. Page 1.1.2
• Paras Sood - Software Engineer at Magento
University
• Working on PHP/Magento since 2011
• Passionate about education and training in the
Technology sector
• Twitter - ParasSood29
About Me:
© 2019 Magento, Inc. Page 1.1.3
• What is Clean Code
• Examples
• Naming Variables, Functions, Classes, DB entities
• Comments, Exception handling –try/catch, Tests etc
• Further Reading – concepts and resources
• Questions – feel free to interrupt
Agenda
© 2019 Magento, Inc. Page 1.1.4
What is Clean Code?
© 2019 Magento, Inc. Page 1.1.5
What is Clean Code?
• Readable – easy to read and understand – avoid things like nested
ternary operators
• No ”Smart” tricks – avoid things like magic getters/setters
• Does things explicitly rather than implicitly by avoiding addition of side
effects
• Maintainable
• Easy to add functionality to
• Easy to re-factor – avoid unnecessary coupling
• Easy to test – good dependency management
© 2019 Magento, Inc. Page 1.1.6
Before we proceed….
© 2019 Magento, Inc. Page 1.1.7
Example 1 – return types, use of constants, descriptive names and more
© 2019 Magento, Inc. Page 1.1.8
Example 2 – magic of early returns, variable naming and avoiding mental mapping
© 2019 Magento, Inc. Page 1.1.9
Summary - Variables, DB tables, Columns, Functions, Class naming
• Use descriptive names – even if they are long – getWholesaleCustomerOrders vs getOrders
• Avoid duplicating context
• getOrders is enough if the context is already Whole sale customers by way of a class
etc.
• customer_account table: customer_account_id vs account_id vs id
• Be consistent with naming – get vs fetch vs load should be used consistently and not
interchangeably
• Function names should generally be verbs – fetchOrdersFromERP(), placeOrder(),
instantiateSession(); etc. vs public function erpOrders()
• Class and object names should be nouns unless you are implementing something like a
command pattern – OrderProcessor, OrderSender vs SendOrder
© 2019 Magento, Inc. Page 1.1.10
Example 3: Functions: Single level of abstraction, avoid passing flags to
functions
© 2019 Magento, Inc. Page 1.1.11
Example 4: Use object encapsulation – follow the Open/Closed principle
© 2019 Magento, Inc. Page 1.1.12
Some Comments on well……. Comments
• Do not add comments that state the obvious
• Good code should be self explanatory
• Needing comments to describe what the code does is a code smell –
consider re-factoring
• In Certain conditions comments are acceptable:
• Documenting a certain decision
• Warning other developers of consequences of changing something
• PHPDocs for API methods
• Comments are hard to maintain
• Unnecessary commenting obscures the useful comments
© 2019 Magento, Inc. Page 1.1.13
Try/Catch Blocks and Exception Handling
• Use exceptions rather than return codes – makes the calling code cleaner and
not scattered with error handling logic
• Write the try-catch-finally block first – this helps you be conscious about what
you need to do in case the called code throws an exception
• Provide context with the exception – ‘Unable to connect to the DB’ is better
than ‘Something went wrong!’. Providing more context may also help with
logging in the catch
• Don’t return null from a method – throw an exception instead, or a special case
object
• Wrap third party API calls in a local class
• Not tied to the design choices of the third party
• Better control over exceptions that are thrown to internal code
• Less dependency on the third party API, can substitute with another third
party service in future
• Easier to mock/test with internal code
© 2019 Magento, Inc. Page 1.1.14
Unit Tests
• Keep them clean – need to maintain them later with production code
• What makes a test clean? – Readability
• Write one assert per test
• Follow FIRST –
• F - Fast
• I - Independent
• R – Repeatable
• S – Self Validating
• T – Timely – it’s too hard later to adjust code
© 2019 Magento, Inc. Page 1.1.15
Further Reading
Concepts:
• DRY
• SOLID principles
• Law of Demeter
Books:
• Robert Martin’s Clean Code
© 2019 Magento, Inc. Page 1.1.16
Questions?

More Related Content

PDF
The New Way of Developing with AEM 6.0 | Sightly | Beautiful Markup
PPTX
Java
PPTX
Episode 14 - Basics of HTML for Salesforce
PPTX
Magento Technical guidelines
PPTX
Polish Magento Code Quality for Lasting Performance and Maintainability.pptx
PPTX
Virtues of platform development
PPTX
Igor Miniailo - Magento 2 API Design Best Practices
PDF
Magento best practices
The New Way of Developing with AEM 6.0 | Sightly | Beautiful Markup
Java
Episode 14 - Basics of HTML for Salesforce
Magento Technical guidelines
Polish Magento Code Quality for Lasting Performance and Maintainability.pptx
Virtues of platform development
Igor Miniailo - Magento 2 API Design Best Practices
Magento best practices

Similar to PHP Dublin Meetup - Clean Code in PHP (20)

PPTX
Code Quality with Magento 2
PPTX
Testing in Magento 2
PDF
Introduction to Clean Code
PPTX
Magento2.3 API Functional Testing
PPTX
Magento 2 Best Practice Workfow // David Lambauer // Meet Magento 2017 // Lei...
PPTX
API design best practices
PDF
Best Practices for Magento Debugging
PPTX
Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
PPTX
MageConf 2017, Design API Best Practices
PPT
The beautiful Magento module - MageTitans 2014
PPTX
API Design Best Practices by Igor Miniailo
PDF
How I ended up contributing to Magento core
PPTX
Backward Compatibility Developer's Guide in Magento 2
PPTX
Clean code
PDF
The Ultimate Checklist for Hiring Top-Tier Magento Developers
PDF
Vinai Kopp - How i develop M2 modules
PPTX
Awesome Architectures in Magento 2.3
PPTX
php[world] Magento101
PDF
Agile & Test Driven Development: The Ampersand Commerce Approach
PPTX
Magento for newbies by IdeatoLife - Design+Code; ArabNet Beirut 2015
Code Quality with Magento 2
Testing in Magento 2
Introduction to Clean Code
Magento2.3 API Functional Testing
Magento 2 Best Practice Workfow // David Lambauer // Meet Magento 2017 // Lei...
API design best practices
Best Practices for Magento Debugging
Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
MageConf 2017, Design API Best Practices
The beautiful Magento module - MageTitans 2014
API Design Best Practices by Igor Miniailo
How I ended up contributing to Magento core
Backward Compatibility Developer's Guide in Magento 2
Clean code
The Ultimate Checklist for Hiring Top-Tier Magento Developers
Vinai Kopp - How i develop M2 modules
Awesome Architectures in Magento 2.3
php[world] Magento101
Agile & Test Driven Development: The Ampersand Commerce Approach
Magento for newbies by IdeatoLife - Design+Code; ArabNet Beirut 2015
Ad

Recently uploaded (20)

PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
ai tools demonstartion for schools and inter college
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
System and Network Administraation Chapter 3
PDF
System and Network Administration Chapter 2
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Nekopoi APK 2025 free lastest update
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Introduction to Artificial Intelligence
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
history of c programming in notes for students .pptx
PTS Company Brochure 2025 (1).pdf.......
ai tools demonstartion for schools and inter college
Understanding Forklifts - TECH EHS Solution
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administraation Chapter 3
System and Network Administration Chapter 2
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Digital Systems & Binary Numbers (comprehensive )
Nekopoi APK 2025 free lastest update
Computer Software and OS of computer science of grade 11.pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
top salesforce developer skills in 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Introduction to Artificial Intelligence
CHAPTER 2 - PM Management and IT Context
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
history of c programming in notes for students .pptx
Ad

PHP Dublin Meetup - Clean Code in PHP

  • 1. © 2019 Magento, Inc. Page 1.1.1 Clean Code in PHP
  • 2. © 2019 Magento, Inc. Page 1.1.2 • Paras Sood - Software Engineer at Magento University • Working on PHP/Magento since 2011 • Passionate about education and training in the Technology sector • Twitter - ParasSood29 About Me:
  • 3. © 2019 Magento, Inc. Page 1.1.3 • What is Clean Code • Examples • Naming Variables, Functions, Classes, DB entities • Comments, Exception handling –try/catch, Tests etc • Further Reading – concepts and resources • Questions – feel free to interrupt Agenda
  • 4. © 2019 Magento, Inc. Page 1.1.4 What is Clean Code?
  • 5. © 2019 Magento, Inc. Page 1.1.5 What is Clean Code? • Readable – easy to read and understand – avoid things like nested ternary operators • No ”Smart” tricks – avoid things like magic getters/setters • Does things explicitly rather than implicitly by avoiding addition of side effects • Maintainable • Easy to add functionality to • Easy to re-factor – avoid unnecessary coupling • Easy to test – good dependency management
  • 6. © 2019 Magento, Inc. Page 1.1.6 Before we proceed….
  • 7. © 2019 Magento, Inc. Page 1.1.7 Example 1 – return types, use of constants, descriptive names and more
  • 8. © 2019 Magento, Inc. Page 1.1.8 Example 2 – magic of early returns, variable naming and avoiding mental mapping
  • 9. © 2019 Magento, Inc. Page 1.1.9 Summary - Variables, DB tables, Columns, Functions, Class naming • Use descriptive names – even if they are long – getWholesaleCustomerOrders vs getOrders • Avoid duplicating context • getOrders is enough if the context is already Whole sale customers by way of a class etc. • customer_account table: customer_account_id vs account_id vs id • Be consistent with naming – get vs fetch vs load should be used consistently and not interchangeably • Function names should generally be verbs – fetchOrdersFromERP(), placeOrder(), instantiateSession(); etc. vs public function erpOrders() • Class and object names should be nouns unless you are implementing something like a command pattern – OrderProcessor, OrderSender vs SendOrder
  • 10. © 2019 Magento, Inc. Page 1.1.10 Example 3: Functions: Single level of abstraction, avoid passing flags to functions
  • 11. © 2019 Magento, Inc. Page 1.1.11 Example 4: Use object encapsulation – follow the Open/Closed principle
  • 12. © 2019 Magento, Inc. Page 1.1.12 Some Comments on well……. Comments • Do not add comments that state the obvious • Good code should be self explanatory • Needing comments to describe what the code does is a code smell – consider re-factoring • In Certain conditions comments are acceptable: • Documenting a certain decision • Warning other developers of consequences of changing something • PHPDocs for API methods • Comments are hard to maintain • Unnecessary commenting obscures the useful comments
  • 13. © 2019 Magento, Inc. Page 1.1.13 Try/Catch Blocks and Exception Handling • Use exceptions rather than return codes – makes the calling code cleaner and not scattered with error handling logic • Write the try-catch-finally block first – this helps you be conscious about what you need to do in case the called code throws an exception • Provide context with the exception – ‘Unable to connect to the DB’ is better than ‘Something went wrong!’. Providing more context may also help with logging in the catch • Don’t return null from a method – throw an exception instead, or a special case object • Wrap third party API calls in a local class • Not tied to the design choices of the third party • Better control over exceptions that are thrown to internal code • Less dependency on the third party API, can substitute with another third party service in future • Easier to mock/test with internal code
  • 14. © 2019 Magento, Inc. Page 1.1.14 Unit Tests • Keep them clean – need to maintain them later with production code • What makes a test clean? – Readability • Write one assert per test • Follow FIRST – • F - Fast • I - Independent • R – Repeatable • S – Self Validating • T – Timely – it’s too hard later to adjust code
  • 15. © 2019 Magento, Inc. Page 1.1.15 Further Reading Concepts: • DRY • SOLID principles • Law of Demeter Books: • Robert Martin’s Clean Code
  • 16. © 2019 Magento, Inc. Page 1.1.16 Questions?

Editor's Notes

  • #6: Notes:
  • #7: Notes: I am going to talk about some examples that each cover 2-3 examples of concepts that differentiate clean code from not so clean code. The purpose of the talk is not to be prescriptive about clean code but rather to make developers curious about what exactly makes clean code. So in future their decisions aren’t based on chance but rather on calculated analysis
  • #8: Notes: Use of constants – make the code more searchable and easier to understand Use of descriptive method names No use of short hand names Use PHP Doc and set return types where possible
  • #9: Notes: Better variable names Early returns Reduce the level of indentation in conditional logic
  • #10: Notes:
  • #11: Notes: Single responsibility Principle for methods Avoid passing flags to methods – you perhaps need better abstractions so you can use polymorphism
  • #12: Notes:
  • #13: Notes:
  • #14: Notes:
  • #15: Notes:
  • #16: Notes: