SlideShare a Scribd company logo
1 of 22
Download to read offline
December 5, 2015 Wordcamp U.S.
Clean Code
Michael Toppa @mtoppa toppa.com
Source
ポカヨケ
www.pokayoke.design
@pokayokedesign
Clean code…
That sounds nice, but I have a deadline
I don't have time for clean code!
* The more we rush, the more the code turns into a big ball of mud, making it harder and harder to work with.
* Keeping the code clean lets us go faster, because clean code is flexible code.
* Why is it exactly that trying to go fast ultimately makes us slower?
The ratio of time spent reading code versus
writing is well over 10 to 1.
Therefore, making code easy to read
makes it easier to write.
paraphrased from Clean Code
If you’re working quick and dirty, you’re not writing code that is readable
“We like to think we spend our time power typing,
but we actually spend most of our time
staring into the abyss.”
- Douglas Crockford
principal discoverer of JSON,
creator of JSLint
* But we’re not really aware of how we’re spending our time.
* Going forward, think about the hours you spend debugging, or trying to figure out messy code.
* When you’re trying to decide whether you should spend a little more time writing code more cleanly, remember an ounce of prevention is worth a pound
of cure
10 ways to make your code clean
#1
You
are responsible for the quality of your code
* A lot of what’s in my talk comes from Bob Martin, who wrote a book called Clean Code. He provides some good analogies.
* It’s not up to the project manager or your boss - they are almost always asking you to go faster.
* What would your doctor say if you told her to skip washing her hands, because you’re in a hurry? What would your accountant say if you told him to save
some time by no doing double-entry bookkeeping?
* If we want to be treated as professionals, we need to act like professionals. And part of being a professional is being honest about how long it takes to do
something well, and not accepting the option to do it wrong.
#2
Use
meaningful
names
The name of a variable, function, or class should tell you why it exists, and what it does
Not good
$d; // elapsed time in days
* Imagine a script full of variable names like this - it would be very difficult to understand.
* Mental translating of obscure names to their real meaning distracts from our ability to get an overall understanding of the code
* The days when it was important to use short variable names to save memory space are long gone
* Automated minification tools can shrink javascript code for you when dealing with code being transmitted over slow connections
Good
$elapsed_time_in_days;
$daysSinceCreation;
$days_since_modification;
* Grady Booch, the former chief scientist at IBM said that code should “read like well written prose.”
* So you can think of variable names as the nouns and subjects of your code sentences
#3 -Write code that expresses
intent
public function set_numeric_thumbnail_size($requested_size = 'xsmall') {
if (array_key_exists($requested_size, $this->thumbnail_sizes_map)) {
$this->numeric_thumbnail_size = $this->thumbnail_sizes_map[$requested_size];
}
else {
throw New Exception(__('invalid thumbnail size requested', 'shashin'));
}
return $this->numeric_thumbnail_size;
}
* Now we get to functions, which are our verbs… Take a minute to read this.
* Even without knowing the class or the properties, it's clear what this method does.
* You should use a 21st century IDE, that auto-completes names for you and makes it easy to rename. I use PHP Storm.
* What don’t you see in this code?
#4
Comments are often lies waiting to happen.
Code should speak for itself whenever possible
* The revelation for me in learning clean code techniques is that code can be expressive. That it really can “read like well written prose.”
* Rather than relying on comments to explain your code, the code should explain itself
* Comments become something you have to maintain, and if they become outdated and no longer describe the current behavior of the code, they become
dangerous lies
* But they are sometimes necessary - for example, you may need to explain why you did something a certain way
* The point is to stop and think before you write a comment, and ask yourself if instead you can make the code more expressive
#5
Source
* A common occurrence in software projects is “bit rot” - the code gets messier and buggier over time.
* But how about code that just keeps getting cleaner over time? You can get this by following the “boy scout rule.”
* Keep your new code clean, and while you’re working on it, spend a few minutes cleaning up the code that it works with. Improve a variable name, break
up a function that’s too big, eliminate a small bit of duplication, etc.
* It’s easier to keep your garden healthy and looking nice by weeding it for 10min every day than waiting until it’s a huge overgrown mess.
#6
Source
* Do one thing, do it well, do it only. But what does it mean to do just one thing?
* For functions, this typically means changing the value of only one variable. If you are passing more than 2 or 3 arguments into a function or method, you
are probably doing more than one thing.
* The more arguments you pass to a function, and the longer it is, the harder it is to test and debug when something goes wrong
* Many code review tools will warn you if a function is more than 10 lines long.
* For classes, it means having a single conceptual responsibility. When projects get big, this is how you keep them manageable. Code review tools will
typically warn you if classes are more than 200 lines
#7 -Write tests!
Copyright Lucasfilm Ltd
Test driven development (TDD)
Source
* Learning TDD completely changed how I code. It forces you to think about how your new code will talk to the existing code first. This leads to better
design than just diving in to solve a problem, and only afterwards trying to figure out how all the pieces fit together.
* Having repeatable tests has also saved me literally hundreds of times, when a change I introduced *here* broke something over *there* without me
realizing it.
* Better to find out from a test that you broke something than from an angry customer.
#8 -Work in short cycles:
incremental and iterative
Source
* Develop systems in small portions at a time (incremental), through repeated cycles (iterative), write tests to verify functionality, and take advantage of
what was learned in each cycle (for both features and implementation).
* What I’m describing is an Agile workflow, which would require a whole other talk to explain in detail
* But for now I’ll just say that an Agile workflow complements the clean code style.
* Work on one thing, do it well, then move on to the next thing.
#9 - Independent Architecture
Why am I subjecting you to this awful image?
“Software architectures are structures that
support the use cases of the system...
Frameworks are tools to be used, not
architectures to be conformed to”
- Bob Martin
http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-Architecture.html
* WordPress is essentially a framework, so this could read “WordPress is a tool to be used, not an architecture to be conformed to.” This is from a blog
post called “screaming architecture”
* I wrote a plugin for displaying photos from Picasa, Youtube, and others in WordPress. Its files and code scream “photo management and display!”, with
names like PicasaSynchronizer and PhotoDisplayer. It does not scream “WordPress plugin.” The fact that it is a WordPress plugin is incidental to its
design. I use most of the same practices in my Rails work as my WordPress work.
* What I’m actually talking about here is community, and growing our sense of community. The WordPress community is so big, it’s easy to talk only
amongst ourselves. There’s a lot we can learn about maintainable design from other platforms and languages.
#10 - Practice, practice, practice
Source
* Musicians don’t play only when they’re on stage. To get good at something you need time to refine your craft, when you’re not under pressure to
perform
* [my working on bus example]
December 5, 2015 Wordcamp U.S.
Michael Toppa @mtoppa www.toppa.com

More Related Content

What's hot

How To Navigate And Extend The Flex Infrastructure
How To Navigate And Extend The Flex InfrastructureHow To Navigate And Extend The Flex Infrastructure
How To Navigate And Extend The Flex Infrastructure
michael.labriola
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles
Serhiy Oplakanets
 
Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -
Theo Jungeblut
 

What's hot (20)

Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
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
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)
 
Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)
Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)
Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)
 
Clean code & design patterns
Clean code & design patternsClean code & design patterns
Clean code & design patterns
 
Designing with tests
Designing with testsDesigning with tests
Designing with tests
 
Clean code
Clean codeClean code
Clean code
 
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
 
S.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software ArchitectsS.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software Architects
 
How To Navigate And Extend The Flex Infrastructure
How To Navigate And Extend The Flex InfrastructureHow To Navigate And Extend The Flex Infrastructure
How To Navigate And Extend The Flex Infrastructure
 
Clean Code for East Bay .NET User Group
Clean Code for East Bay .NET User GroupClean Code for East Bay .NET User Group
Clean Code for East Bay .NET User Group
 
Coding Standards
Coding StandardsCoding Standards
Coding Standards
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementor
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles
 
WordCamp Nashville: Clean Code for WordPress
WordCamp Nashville: Clean Code for WordPressWordCamp Nashville: Clean Code for WordPress
WordCamp Nashville: Clean Code for WordPress
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -
 
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
 

Viewers also liked

Java Code Quality Tools
Java Code Quality ToolsJava Code Quality Tools
Java Code Quality Tools
Orest Ivasiv
 
Testing Frameworks And Methodologies
Testing Frameworks And MethodologiesTesting Frameworks And Methodologies
Testing Frameworks And Methodologies
Steven Cahill
 

Viewers also liked (15)

Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Clean Code
Clean CodeClean Code
Clean Code
 
Oplæg til ArbejdsmiljøNETs Årskonference, April 2015
Oplæg til ArbejdsmiljøNETs Årskonference, April 2015Oplæg til ArbejdsmiljøNETs Årskonference, April 2015
Oplæg til ArbejdsmiljøNETs Årskonference, April 2015
 
Zero downtime deployment
Zero downtime deploymentZero downtime deployment
Zero downtime deployment
 
Framework-less Applications
Framework-less ApplicationsFramework-less Applications
Framework-less Applications
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Introduction to FindBugs
Introduction to FindBugsIntroduction to FindBugs
Introduction to FindBugs
 
iks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltag
iks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltagiks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltag
iks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltag
 
.Net Unit Testing with Visual Studio 2010
.Net Unit Testing with Visual Studio 2010.Net Unit Testing with Visual Studio 2010
.Net Unit Testing with Visual Studio 2010
 
Java Code Quality Tools
Java Code Quality ToolsJava Code Quality Tools
Java Code Quality Tools
 
Ch24 quality management
Ch24 quality managementCh24 quality management
Ch24 quality management
 
Testing Frameworks And Methodologies
Testing Frameworks And MethodologiesTesting Frameworks And Methodologies
Testing Frameworks And Methodologies
 
Why Use MVC?
Why Use MVC?Why Use MVC?
Why Use MVC?
 
Clean code
Clean codeClean code
Clean code
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 

Similar to WordCamp US: Clean Code

Scottish Ruby Conference 2014
Scottish Ruby Conference  2014Scottish Ruby Conference  2014
Scottish Ruby Conference 2014
michaelag1971
 
Software Development Essential Skills
Software Development Essential SkillsSoftware Development Essential Skills
Software Development Essential Skills
John Choi
 

Similar to WordCamp US: Clean Code (20)

Clean Code Software Engineering
Clean Code Software Engineering Clean Code Software Engineering
Clean Code Software Engineering
 
10 Big Ideas from Industry
10 Big Ideas from Industry10 Big Ideas from Industry
10 Big Ideas from Industry
 
Scottish Ruby Conference 2014
Scottish Ruby Conference  2014Scottish Ruby Conference  2014
Scottish Ruby Conference 2014
 
Put to the Test
Put to the TestPut to the Test
Put to the Test
 
Software Development Essential Skills
Software Development Essential SkillsSoftware Development Essential Skills
Software Development Essential Skills
 
“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY
“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY
“Don’t Repeat Yourself”: 4 Process Street Features to Keep Work DRY
 
Good Coding Practices with JavaScript
Good Coding Practices with JavaScriptGood Coding Practices with JavaScript
Good Coding Practices with JavaScript
 
Untangling spring week8
Untangling spring week8Untangling spring week8
Untangling spring week8
 
How have we developed product without bugs
How have we developed product without bugsHow have we developed product without bugs
How have we developed product without bugs
 
YAGNI Principle and Clean Code
YAGNI Principle and Clean CodeYAGNI Principle and Clean Code
YAGNI Principle and Clean Code
 
Php rules
Php rulesPhp rules
Php rules
 
Php rules
Php rulesPhp rules
Php rules
 
Prefer Code to Comments
Prefer Code to CommentsPrefer Code to Comments
Prefer Code to Comments
 
11 rules for programmer should live by
11 rules for programmer should live by11 rules for programmer should live by
11 rules for programmer should live by
 
Best Practices For Writing Super Readable Code
Best Practices For Writing Super Readable CodeBest Practices For Writing Super Readable Code
Best Practices For Writing Super Readable Code
 
Importance of Documentation for programmers
Importance of Documentation for programmers Importance of Documentation for programmers
Importance of Documentation for programmers
 
Best Practices in Software Development
Best Practices in Software DevelopmentBest Practices in Software Development
Best Practices in Software Development
 
API Workshop: Deep dive into code samples
API Workshop: Deep dive into code samplesAPI Workshop: Deep dive into code samples
API Workshop: Deep dive into code samples
 
How to tell a better story (in code)(final)
How to tell a better story (in code)(final)How to tell a better story (in code)(final)
How to tell a better story (in code)(final)
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patterns
 

More from mtoppa

WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress ConsultantsWordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
mtoppa
 
Dependency Injection for Wordpress
Dependency Injection for WordpressDependency Injection for Wordpress
Dependency Injection for Wordpress
mtoppa
 
Clean code for WordPress
Clean code for WordPressClean code for WordPress
Clean code for WordPress
mtoppa
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHP
mtoppa
 

More from mtoppa (20)

RubyConf 2022 - From beginner to expert, and back again
RubyConf 2022 - From beginner to expert, and back againRubyConf 2022 - From beginner to expert, and back again
RubyConf 2022 - From beginner to expert, and back again
 
RailsConf 2022 - Upgrading Rails: The Dual Boot Way
RailsConf 2022 - Upgrading Rails: The Dual Boot WayRailsConf 2022 - Upgrading Rails: The Dual Boot Way
RailsConf 2022 - Upgrading Rails: The Dual Boot Way
 
Applying Omotenashi (Japanese customer service) to your work
Applying Omotenashi (Japanese customer service) to your workApplying Omotenashi (Japanese customer service) to your work
Applying Omotenashi (Japanese customer service) to your work
 
Talking to strangers causes train wrecks
Talking to strangers causes train wrecksTalking to strangers causes train wrecks
Talking to strangers causes train wrecks
 
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between: accessib...
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between:  accessib...A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between:  accessib...
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between: accessib...
 
The promise and peril of Agile and Lean practices
The promise and peril of Agile and Lean practicesThe promise and peril of Agile and Lean practices
The promise and peril of Agile and Lean practices
 
Why do planes crash? Lessons for junior and senior developers
Why do planes crash? Lessons for junior and senior developersWhy do planes crash? Lessons for junior and senior developers
Why do planes crash? Lessons for junior and senior developers
 
Boston Ruby Meetup: The promise and peril of Agile and Lean practices
Boston Ruby Meetup: The promise and peril of Agile and Lean practicesBoston Ruby Meetup: The promise and peril of Agile and Lean practices
Boston Ruby Meetup: The promise and peril of Agile and Lean practices
 
A real-life overview of Agile and Scrum
A real-life overview of Agile and ScrumA real-life overview of Agile and Scrum
A real-life overview of Agile and Scrum
 
WordCamp Nashville 2016: The promise and peril of Agile and Lean practices
WordCamp Nashville 2016: The promise and peril of Agile and Lean practicesWordCamp Nashville 2016: The promise and peril of Agile and Lean practices
WordCamp Nashville 2016: The promise and peril of Agile and Lean practices
 
WordCamp Boston 2015: Agile Contracts for WordPress Consultants
WordCamp Boston 2015: Agile Contracts for WordPress ConsultantsWordCamp Boston 2015: Agile Contracts for WordPress Consultants
WordCamp Boston 2015: Agile Contracts for WordPress Consultants
 
WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress ConsultantsWordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
 
Rails testing: factories or fixtures?
Rails testing: factories or fixtures?Rails testing: factories or fixtures?
Rails testing: factories or fixtures?
 
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
 
A real-life overview of Agile workflow practices
A real-life overview of Agile workflow practicesA real-life overview of Agile workflow practices
A real-life overview of Agile workflow practices
 
Why Agile? Why Now?
Why Agile? Why Now?Why Agile? Why Now?
Why Agile? Why Now?
 
Object Oriented Programming for WordPress Plugin Development
Object Oriented Programming for WordPress Plugin DevelopmentObject Oriented Programming for WordPress Plugin Development
Object Oriented Programming for WordPress Plugin Development
 
Dependency Injection for Wordpress
Dependency Injection for WordpressDependency Injection for Wordpress
Dependency Injection for Wordpress
 
Clean code for WordPress
Clean code for WordPressClean code for WordPress
Clean code for WordPress
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHP
 

Recently uploaded

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

WordCamp US: Clean Code

  • 1. December 5, 2015 Wordcamp U.S. Clean Code Michael Toppa @mtoppa toppa.com Source
  • 3. Clean code… That sounds nice, but I have a deadline I don't have time for clean code!
  • 4. * The more we rush, the more the code turns into a big ball of mud, making it harder and harder to work with. * Keeping the code clean lets us go faster, because clean code is flexible code. * Why is it exactly that trying to go fast ultimately makes us slower?
  • 5. The ratio of time spent reading code versus writing is well over 10 to 1. Therefore, making code easy to read makes it easier to write. paraphrased from Clean Code If you’re working quick and dirty, you’re not writing code that is readable
  • 6. “We like to think we spend our time power typing, but we actually spend most of our time staring into the abyss.” - Douglas Crockford principal discoverer of JSON, creator of JSLint * But we’re not really aware of how we’re spending our time. * Going forward, think about the hours you spend debugging, or trying to figure out messy code. * When you’re trying to decide whether you should spend a little more time writing code more cleanly, remember an ounce of prevention is worth a pound of cure
  • 7. 10 ways to make your code clean
  • 8. #1 You are responsible for the quality of your code * A lot of what’s in my talk comes from Bob Martin, who wrote a book called Clean Code. He provides some good analogies. * It’s not up to the project manager or your boss - they are almost always asking you to go faster. * What would your doctor say if you told her to skip washing her hands, because you’re in a hurry? What would your accountant say if you told him to save some time by no doing double-entry bookkeeping? * If we want to be treated as professionals, we need to act like professionals. And part of being a professional is being honest about how long it takes to do something well, and not accepting the option to do it wrong.
  • 9. #2 Use meaningful names The name of a variable, function, or class should tell you why it exists, and what it does
  • 10. Not good $d; // elapsed time in days * Imagine a script full of variable names like this - it would be very difficult to understand. * Mental translating of obscure names to their real meaning distracts from our ability to get an overall understanding of the code * The days when it was important to use short variable names to save memory space are long gone * Automated minification tools can shrink javascript code for you when dealing with code being transmitted over slow connections
  • 11. Good $elapsed_time_in_days; $daysSinceCreation; $days_since_modification; * Grady Booch, the former chief scientist at IBM said that code should “read like well written prose.” * So you can think of variable names as the nouns and subjects of your code sentences
  • 12. #3 -Write code that expresses intent public function set_numeric_thumbnail_size($requested_size = 'xsmall') { if (array_key_exists($requested_size, $this->thumbnail_sizes_map)) { $this->numeric_thumbnail_size = $this->thumbnail_sizes_map[$requested_size]; } else { throw New Exception(__('invalid thumbnail size requested', 'shashin')); } return $this->numeric_thumbnail_size; } * Now we get to functions, which are our verbs… Take a minute to read this. * Even without knowing the class or the properties, it's clear what this method does. * You should use a 21st century IDE, that auto-completes names for you and makes it easy to rename. I use PHP Storm. * What don’t you see in this code?
  • 13. #4 Comments are often lies waiting to happen. Code should speak for itself whenever possible * The revelation for me in learning clean code techniques is that code can be expressive. That it really can “read like well written prose.” * Rather than relying on comments to explain your code, the code should explain itself * Comments become something you have to maintain, and if they become outdated and no longer describe the current behavior of the code, they become dangerous lies * But they are sometimes necessary - for example, you may need to explain why you did something a certain way * The point is to stop and think before you write a comment, and ask yourself if instead you can make the code more expressive
  • 14. #5 Source * A common occurrence in software projects is “bit rot” - the code gets messier and buggier over time. * But how about code that just keeps getting cleaner over time? You can get this by following the “boy scout rule.” * Keep your new code clean, and while you’re working on it, spend a few minutes cleaning up the code that it works with. Improve a variable name, break up a function that’s too big, eliminate a small bit of duplication, etc. * It’s easier to keep your garden healthy and looking nice by weeding it for 10min every day than waiting until it’s a huge overgrown mess.
  • 15. #6 Source * Do one thing, do it well, do it only. But what does it mean to do just one thing? * For functions, this typically means changing the value of only one variable. If you are passing more than 2 or 3 arguments into a function or method, you are probably doing more than one thing. * The more arguments you pass to a function, and the longer it is, the harder it is to test and debug when something goes wrong * Many code review tools will warn you if a function is more than 10 lines long. * For classes, it means having a single conceptual responsibility. When projects get big, this is how you keep them manageable. Code review tools will typically warn you if classes are more than 200 lines
  • 16. #7 -Write tests! Copyright Lucasfilm Ltd
  • 17. Test driven development (TDD) Source * Learning TDD completely changed how I code. It forces you to think about how your new code will talk to the existing code first. This leads to better design than just diving in to solve a problem, and only afterwards trying to figure out how all the pieces fit together. * Having repeatable tests has also saved me literally hundreds of times, when a change I introduced *here* broke something over *there* without me realizing it. * Better to find out from a test that you broke something than from an angry customer.
  • 18. #8 -Work in short cycles: incremental and iterative Source * Develop systems in small portions at a time (incremental), through repeated cycles (iterative), write tests to verify functionality, and take advantage of what was learned in each cycle (for both features and implementation). * What I’m describing is an Agile workflow, which would require a whole other talk to explain in detail * But for now I’ll just say that an Agile workflow complements the clean code style. * Work on one thing, do it well, then move on to the next thing.
  • 19. #9 - Independent Architecture Why am I subjecting you to this awful image?
  • 20. “Software architectures are structures that support the use cases of the system... Frameworks are tools to be used, not architectures to be conformed to” - Bob Martin http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-Architecture.html * WordPress is essentially a framework, so this could read “WordPress is a tool to be used, not an architecture to be conformed to.” This is from a blog post called “screaming architecture” * I wrote a plugin for displaying photos from Picasa, Youtube, and others in WordPress. Its files and code scream “photo management and display!”, with names like PicasaSynchronizer and PhotoDisplayer. It does not scream “WordPress plugin.” The fact that it is a WordPress plugin is incidental to its design. I use most of the same practices in my Rails work as my WordPress work. * What I’m actually talking about here is community, and growing our sense of community. The WordPress community is so big, it’s easy to talk only amongst ourselves. There’s a lot we can learn about maintainable design from other platforms and languages.
  • 21. #10 - Practice, practice, practice Source * Musicians don’t play only when they’re on stage. To get good at something you need time to refine your craft, when you’re not under pressure to perform * [my working on bus example]
  • 22. December 5, 2015 Wordcamp U.S. Michael Toppa @mtoppa www.toppa.com