SlideShare a Scribd company logo
WRITING CLEAN AND MAINTAINABLE CODE
WordCamp Norway 2015
Marko Heijnen
• Lead developer of GlotPress

• Core contributor for WordPress

• Working for 1&1 as their
WordPress specialist

• Fan of new technologies
Quick note
This talk is not only for developers.
Knowing how code should look like is important to
judge it’s quality.
Bad code can bring your company down
Clean code
Some parts of this presentation are
from the book “Clean Code” from
Robert C. Martin aka Uncle Bob
So what is clean code?
– Bjarne Stroustrup, inventor of C++
“I like my code be elegant and efficient”
“Clean code does one thing well”
Elegance
– Grady Booch, author of Object Oriented Analysis

and Design With Applications
“Clean code is simple and direct”
“Clean code reads like well-written prose”
Simple, direct, readable
– Dave Thomas, founder of OTI
“Clean code can be read”
“Clean code should be literate”
Literate
– Michael Feathers, author of Working Effectively with Legacy Code
“Clean code always looks like it was written by
someone who cares”
Care
– Ron Jeffries, author of Extreme Programming Installed
“No duplication, one thing, expressiveness, tiny
abstractions.”
Small, expressive, simple
– Ward Cunningham, inventor of Wiki, Fit, coinventor of eXtreme
Programming. Motive force behind Design Patterns
“You know you are working on clean code
when each routing you reads turns out to be
pretty much what you expected.”
What you expected
Writing clean and maintainable code
Variables, functions and methods
Meaningful names
• Intention-Revealing Names
• English Readability -> is_user_logged_in()
• Avoid disinformation
• Use pronounceable names
Meaningful names
• Classes should have a noun or noun phrase names but
should not be a verb
• Method names should have verb or verb phrase names
• Use descriptive names
The code
• Should be very small!

< 150 characters per line and < 20 lines
• Do one thing and do that right
• Limit it to one level of abstraction
• Reading code from top to bottom
• The ideal number of arguments for a function is zero
The code
• Don’t use flag arguments
• passing a boolean into a function
• Pass objects as a function argument to reduce the
arguments
• Have no side effect, does what is expected
• Don’t repeat yourself
• Only return data once
What does it not mean
Having set requirements like PHP 5.4 >

It happens when the functionality is required
Using namespaces
Important for projects with a lot of dependencies
The use of autoloaders
It could enhances startup time but it’s not the place
to do so
Most of WordPress code base
but every release it does get better
What does this mean for WordPress?
What does this mean for WordPress?
• Follow the WordPress coding standards
• A shared code base is an important aspect for clean code
• Have it prefix with something that is unique
What are the violations in WordPress
• A lot of global variables
• Functions/Methods should never echo
• Returning WP_Error instead of exceptions
• WP_DB
Last but not least
Test Driven Development: Unit Tests
The three laws of Test Driven Development
• First law

You may not write production code until you have written
a failing unit test
• Second law

You may not write more of a unit test than is sufficient to
fail, and not compiling is failing
• Third law

You may not write more production code than is sufficient
to pass the currently failing test
Unit tests is the best documentation of your code

It shows how you intended your code to be used
Keep test clean with the same standards as your
production code.
Readability is key

Maybe even more important then

your production code
A single unit test should contain
• Single concept per test
• One assert per test is a good guideline
• Five other rules: F.I.R.S.T
• Fast
• Independent
• Repeatable
• Self-Validating
• Timely
Clean code & me
Clean code & me
• I never studied Computer Science
• I do care about quality and structure
• I refuse to do quick wins or hacks
• Work together with others to become better
• Applying unit tests on existing projects
• A lot of refactoring and WTF moments
WP_Image_Editor
What I have learned from working
together with a Java developer
What I have learned from working
together with a Java developer
• Start applying all the things I discussed
• Using OOP more then just wrapping functions in a class
• Using a Wrapper / Decorator pattern
• Think more in advance how things should work
• And also think what could change
An hour discussion about hooks in WordPress
Writing clean and maintainable code
Thank you for listening
Questions?
@markoheijnen

markoheijnen.com

More Related Content

PPTX
What Is DevOps?
PDF
Introduction to CICD
PDF
DevOps for beginners
PDF
Getting Started with Infrastructure as Code
PPTX
Prometheus design and philosophy
PPT
Monitoring using Prometheus and Grafana
PPTX
PPTX
Jenkins CI
What Is DevOps?
Introduction to CICD
DevOps for beginners
Getting Started with Infrastructure as Code
Prometheus design and philosophy
Monitoring using Prometheus and Grafana
Jenkins CI

What's hot (20)

ODP
Monitoring With Prometheus
PPTX
Introduction to DevOps
PPTX
Introduction to Docker - 2017
PDF
DevOps with GitHub Actions
PPTX
Azure DevOps CI/CD For Beginners
PPTX
Static Analysis Security Testing for Dummies... and You
PPTX
Introduction to CI/CD
PPTX
DevOps: Infrastructure as Code
PDF
Docker & kubernetes
PPTX
Gitlab CI/CD
PPTX
Jenkins CI presentation
PDF
PDF
PDF
Test Driven Development (TDD)
PPTX
Intro to Azure DevOps
PPTX
Kubernetes & Google Kubernetes Engine (GKE)
PPTX
Introduction to helm
PPTX
Terraform
PPTX
Comprehensive Terraform Training
PDF
GitOps with ArgoCD
Monitoring With Prometheus
Introduction to DevOps
Introduction to Docker - 2017
DevOps with GitHub Actions
Azure DevOps CI/CD For Beginners
Static Analysis Security Testing for Dummies... and You
Introduction to CI/CD
DevOps: Infrastructure as Code
Docker & kubernetes
Gitlab CI/CD
Jenkins CI presentation
Test Driven Development (TDD)
Intro to Azure DevOps
Kubernetes & Google Kubernetes Engine (GKE)
Introduction to helm
Terraform
Comprehensive Terraform Training
GitOps with ArgoCD
Ad

Similar to Writing clean and maintainable code (20)

PDF
WordCamp Nashville: Clean Code for WordPress
PPTX
Clean code
PDF
Clean Code
PDF
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
PPT
Clean Code summary
PPT
Coding Standards
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
PPTX
Stop wasting-time-by-applying-clean-code-principles
PDF
Naming Things (with notes)
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
ODP
Clean Code - Part 2
PPTX
Writing Clean Code (Recommendations by Robert Martin)
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
PDF
WordCamp US: Clean Code
PDF
Object Calisthenics for Magento - MageTitans USA 2017
PDF
Clean Code V2
PDF
Clean Code
PPTX
Clean code
WordCamp Nashville: Clean Code for WordPress
Clean code
Clean Code
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
Clean Code summary
Coding Standards
Clean Code. An Agile Guide to Software Craft Kameron H.
Clean Code. An Agile Guide to Software Craft Kameron H.
Stop wasting-time-by-applying-clean-code-principles
Naming Things (with notes)
Clean Code. An Agile Guide to Software Craft Kameron H.
Clean Code - Part 2
Writing Clean Code (Recommendations by Robert Martin)
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
WordCamp US: Clean Code
Object Calisthenics for Magento - MageTitans USA 2017
Clean Code V2
Clean Code
Clean code
Ad

More from Marko Heijnen (20)

PDF
Custom coded projects
PDF
Security, more important than ever!
PDF
My Contributor Story
PDF
WooCommerce & Apple TV
PDF
The moment my site got hacked - WordCamp Sofia
PDF
Mijn site beveiliging
PDF
The moment my site got hacked
PDF
My complicated WordPress site
PDF
Node.js to the rescue
PDF
Protecting your site by detection
PDF
GlotPress aka translate.wordpress.org
PDF
Extending WordPress as a pro
PDF
Let's create a multilingual site in WordPress
PDF
Bootstrapping your plugin
PDF
The development and future of GlotPress
PDF
Why Javascript matters
PDF
The code history of WordPress
PDF
Building plugins like a pro
PDF
Perfect your images using WordPress - WordCamp Europe 2013
PDF
Dealing with media
Custom coded projects
Security, more important than ever!
My Contributor Story
WooCommerce & Apple TV
The moment my site got hacked - WordCamp Sofia
Mijn site beveiliging
The moment my site got hacked
My complicated WordPress site
Node.js to the rescue
Protecting your site by detection
GlotPress aka translate.wordpress.org
Extending WordPress as a pro
Let's create a multilingual site in WordPress
Bootstrapping your plugin
The development and future of GlotPress
Why Javascript matters
The code history of WordPress
Building plugins like a pro
Perfect your images using WordPress - WordCamp Europe 2013
Dealing with media

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PDF
KodekX | Application Modernization Development
PPTX
sap open course for s4hana steps from ECC to s4
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Approach and Philosophy of On baking technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Spectroscopy.pptx food analysis technology
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
KodekX | Application Modernization Development
sap open course for s4hana steps from ECC to s4
NewMind AI Weekly Chronicles - August'25 Week I
“AI and Expert System Decision Support & Business Intelligence Systems”
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Review of recent advances in non-invasive hemoglobin estimation
Spectroscopy.pptx food analysis technology

Writing clean and maintainable code

  • 1. WRITING CLEAN AND MAINTAINABLE CODE WordCamp Norway 2015
  • 2. Marko Heijnen • Lead developer of GlotPress • Core contributor for WordPress • Working for 1&1 as their WordPress specialist • Fan of new technologies
  • 3. Quick note This talk is not only for developers. Knowing how code should look like is important to judge it’s quality.
  • 4. Bad code can bring your company down
  • 5. Clean code Some parts of this presentation are from the book “Clean Code” from Robert C. Martin aka Uncle Bob
  • 6. So what is clean code?
  • 7. – Bjarne Stroustrup, inventor of C++ “I like my code be elegant and efficient” “Clean code does one thing well” Elegance
  • 8. – Grady Booch, author of Object Oriented Analysis
 and Design With Applications “Clean code is simple and direct” “Clean code reads like well-written prose” Simple, direct, readable
  • 9. – Dave Thomas, founder of OTI “Clean code can be read” “Clean code should be literate” Literate
  • 10. – Michael Feathers, author of Working Effectively with Legacy Code “Clean code always looks like it was written by someone who cares” Care
  • 11. – Ron Jeffries, author of Extreme Programming Installed “No duplication, one thing, expressiveness, tiny abstractions.” Small, expressive, simple
  • 12. – Ward Cunningham, inventor of Wiki, Fit, coinventor of eXtreme Programming. Motive force behind Design Patterns “You know you are working on clean code when each routing you reads turns out to be pretty much what you expected.” What you expected
  • 15. Meaningful names • Intention-Revealing Names • English Readability -> is_user_logged_in() • Avoid disinformation • Use pronounceable names
  • 16. Meaningful names • Classes should have a noun or noun phrase names but should not be a verb • Method names should have verb or verb phrase names • Use descriptive names
  • 17. The code • Should be very small!
 < 150 characters per line and < 20 lines • Do one thing and do that right • Limit it to one level of abstraction • Reading code from top to bottom • The ideal number of arguments for a function is zero
  • 18. The code • Don’t use flag arguments • passing a boolean into a function • Pass objects as a function argument to reduce the arguments • Have no side effect, does what is expected • Don’t repeat yourself • Only return data once
  • 19. What does it not mean
  • 20. Having set requirements like PHP 5.4 >
 It happens when the functionality is required
  • 21. Using namespaces Important for projects with a lot of dependencies
  • 22. The use of autoloaders It could enhances startup time but it’s not the place to do so
  • 23. Most of WordPress code base but every release it does get better
  • 24. What does this mean for WordPress?
  • 25. What does this mean for WordPress? • Follow the WordPress coding standards • A shared code base is an important aspect for clean code • Have it prefix with something that is unique
  • 26. What are the violations in WordPress • A lot of global variables • Functions/Methods should never echo • Returning WP_Error instead of exceptions • WP_DB
  • 27. Last but not least Test Driven Development: Unit Tests
  • 28. The three laws of Test Driven Development • First law
 You may not write production code until you have written a failing unit test • Second law
 You may not write more of a unit test than is sufficient to fail, and not compiling is failing • Third law
 You may not write more production code than is sufficient to pass the currently failing test
  • 29. Unit tests is the best documentation of your code
 It shows how you intended your code to be used
  • 30. Keep test clean with the same standards as your production code.
  • 31. Readability is key
 Maybe even more important then
 your production code
  • 32. A single unit test should contain • Single concept per test • One assert per test is a good guideline • Five other rules: F.I.R.S.T • Fast • Independent • Repeatable • Self-Validating • Timely
  • 34. Clean code & me • I never studied Computer Science • I do care about quality and structure • I refuse to do quick wins or hacks • Work together with others to become better • Applying unit tests on existing projects • A lot of refactoring and WTF moments
  • 36. What I have learned from working together with a Java developer
  • 37. What I have learned from working together with a Java developer • Start applying all the things I discussed • Using OOP more then just wrapping functions in a class • Using a Wrapper / Decorator pattern • Think more in advance how things should work • And also think what could change
  • 38. An hour discussion about hooks in WordPress
  • 40. Thank you for listening Questions? @markoheijnen markoheijnen.com