SlideShare a Scribd company logo
1 of 40
Download to read offline
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
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
Thank you for listening
Questions?
@markoheijnen

markoheijnen.com

More Related Content

What's hot

Unit/Integration Testing using Spock
Unit/Integration Testing using SpockUnit/Integration Testing using Spock
Unit/Integration Testing using SpockAnuj Aneja
 
Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Asim Rais Siddiqui
 
Coding standards
Coding standardsCoding standards
Coding standardsMimoh Ojha
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practicesmh_azad
 
Introduction to jenkins
Introduction to jenkinsIntroduction to jenkins
Introduction to jenkinsAbe Diaz
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelinesAnkur Goyal
 
C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practicesTan Tran
 
MongoDB Fundamentals
MongoDB FundamentalsMongoDB Fundamentals
MongoDB FundamentalsMongoDB
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Git and Github slides.pdf
Git and Github slides.pdfGit and Github slides.pdf
Git and Github slides.pdfTilton2
 
Introducing GitLab (June 2018)
Introducing GitLab (June 2018)Introducing GitLab (June 2018)
Introducing GitLab (June 2018)Noa Harel
 

What's hot (20)

Unit/Integration Testing using Spock
Unit/Integration Testing using SpockUnit/Integration Testing using Spock
Unit/Integration Testing using Spock
 
Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#
 
Coding standards
Coding standardsCoding standards
Coding standards
 
Git tutorial
Git tutorialGit tutorial
Git tutorial
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
 
Introduction to jenkins
Introduction to jenkinsIntroduction to jenkins
Introduction to jenkins
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
Git training v10
Git training v10Git training v10
Git training v10
 
Learning typescript
Learning typescriptLearning typescript
Learning typescript
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
 
C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practices
 
Coding standard
Coding standardCoding standard
Coding standard
 
Clean code
Clean codeClean code
Clean code
 
MongoDB Fundamentals
MongoDB FundamentalsMongoDB Fundamentals
MongoDB Fundamentals
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Git
GitGit
Git
 
Git101
Git101Git101
Git101
 
Git and Github slides.pdf
Git and Github slides.pdfGit and Github slides.pdf
Git and Github slides.pdf
 
Jenkins Automation
Jenkins AutomationJenkins Automation
Jenkins Automation
 
Introducing GitLab (June 2018)
Introducing GitLab (June 2018)Introducing GitLab (June 2018)
Introducing GitLab (June 2018)
 

Similar to Writing clean and maintainable code

Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven developmentEinar Ingebrigtsen
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference CardSeapine Software
 
Code smells and Other Malodorous Software Odors
Code smells and Other Malodorous Software OdorsCode smells and Other Malodorous Software Odors
Code smells and Other Malodorous Software OdorsClint Edmonson
 
Developers Best Practices
Developers Best PracticesDevelopers Best Practices
Developers Best Practicesaqib javaid
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and AutomationMahesh Salaria
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentShawn Jones
 
Being Good Developer
Being Good DeveloperBeing Good Developer
Being Good DeveloperSally Ahmed
 
Continuous Integration In A PHP World
Continuous Integration In A PHP WorldContinuous Integration In A PHP World
Continuous Integration In A PHP WorldIdaf_1er
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptxAmalEldhose2
 
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019Jason Tice
 
SubmitJS: Is react + redux + typescript a good combination? Dmytro Beseda
SubmitJS: Is react + redux + typescript a good combination? Dmytro BesedaSubmitJS: Is react + redux + typescript a good combination? Dmytro Beseda
SubmitJS: Is react + redux + typescript a good combination? Dmytro BesedaBinary Studio
 

Similar to Writing clean and maintainable code (20)

Clean Code Talk (draft)
Clean Code Talk (draft)Clean Code Talk (draft)
Clean Code Talk (draft)
 
Clean code chpt_1
Clean code chpt_1Clean code chpt_1
Clean code chpt_1
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference Card
 
Code smells and Other Malodorous Software Odors
Code smells and Other Malodorous Software OdorsCode smells and Other Malodorous Software Odors
Code smells and Other Malodorous Software Odors
 
Developers Best Practices
Developers Best PracticesDevelopers Best Practices
Developers Best Practices
 
Eurosport's Kodakademi #2
Eurosport's Kodakademi #2Eurosport's Kodakademi #2
Eurosport's Kodakademi #2
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and Automation
 
Clean code
Clean codeClean code
Clean code
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
 
Enterprise PHP
Enterprise PHPEnterprise PHP
Enterprise PHP
 
Tdd
TddTdd
Tdd
 
Tdd and bdd
Tdd and bddTdd and bdd
Tdd and bdd
 
Being Good Developer
Being Good DeveloperBeing Good Developer
Being Good Developer
 
Continuous Integration In A PHP World
Continuous Integration In A PHP WorldContinuous Integration In A PHP World
Continuous Integration In A PHP World
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx
 
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
 
SubmitJS: Is react + redux + typescript a good combination? Dmytro Beseda
SubmitJS: Is react + redux + typescript a good combination? Dmytro BesedaSubmitJS: Is react + redux + typescript a good combination? Dmytro Beseda
SubmitJS: Is react + redux + typescript a good combination? Dmytro Beseda
 
Code Review for iOS
Code Review for iOSCode Review for iOS
Code Review for iOS
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 

More from Marko Heijnen

Custom coded projects
Custom coded projectsCustom coded projects
Custom coded projectsMarko Heijnen
 
Security, more important than ever!
Security, more important than ever!Security, more important than ever!
Security, more important than ever!Marko Heijnen
 
My Contributor Story
My Contributor StoryMy Contributor Story
My Contributor StoryMarko Heijnen
 
WooCommerce & Apple TV
WooCommerce & Apple TVWooCommerce & Apple TV
WooCommerce & Apple TVMarko Heijnen
 
The moment my site got hacked - WordCamp Sofia
The moment my site got hacked - WordCamp SofiaThe moment my site got hacked - WordCamp Sofia
The moment my site got hacked - WordCamp SofiaMarko Heijnen
 
Mijn site beveiliging
Mijn site beveiligingMijn site beveiliging
Mijn site beveiligingMarko Heijnen
 
The moment my site got hacked
The moment my site got hackedThe moment my site got hacked
The moment my site got hackedMarko Heijnen
 
My complicated WordPress site
My complicated WordPress siteMy complicated WordPress site
My complicated WordPress siteMarko Heijnen
 
Node.js to the rescue
Node.js to the rescueNode.js to the rescue
Node.js to the rescueMarko Heijnen
 
Protecting your site by detection
Protecting your site by detectionProtecting your site by detection
Protecting your site by detectionMarko Heijnen
 
GlotPress aka translate.wordpress.org
GlotPress aka translate.wordpress.orgGlotPress aka translate.wordpress.org
GlotPress aka translate.wordpress.orgMarko Heijnen
 
Extending WordPress as a pro
Extending WordPress as a proExtending WordPress as a pro
Extending WordPress as a proMarko Heijnen
 
Let's create a multilingual site in WordPress
Let's create a multilingual site in WordPressLet's create a multilingual site in WordPress
Let's create a multilingual site in WordPressMarko Heijnen
 
Bootstrapping your plugin
Bootstrapping your pluginBootstrapping your plugin
Bootstrapping your pluginMarko Heijnen
 
The development and future of GlotPress
The development and future of GlotPressThe development and future of GlotPress
The development and future of GlotPressMarko Heijnen
 
Why Javascript matters
Why Javascript mattersWhy Javascript matters
Why Javascript mattersMarko Heijnen
 
The code history of WordPress
The code history of WordPressThe code history of WordPress
The code history of WordPressMarko Heijnen
 
Building plugins like a pro
Building plugins like a proBuilding plugins like a pro
Building plugins like a proMarko Heijnen
 
Perfect your images using WordPress - WordCamp Europe 2013
Perfect your images using WordPress - WordCamp Europe 2013Perfect your images using WordPress - WordCamp Europe 2013
Perfect your images using WordPress - WordCamp Europe 2013Marko Heijnen
 

More from Marko Heijnen (20)

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

Recently uploaded

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 

Recently uploaded (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

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
  • 13.
  • 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
  • 39.
  • 40. Thank you for listening Questions? @markoheijnen markoheijnen.com