SlideShare a Scribd company logo
1 of 31
I Don’t HateYou,
I Just Hate Your Code
— B R I A N R I C H A R D S —
@rzen // WPSessions.com // WebDevStudios.com
— C H A P T E R O N E —
Formatting is
important.
@rzen
— C H A P T E R O N E —
Formatting
indicator of buggy code
#1
Inconsistent spacing is the
@rzen
— C H A P T E R O N E —
Formatting
"When people look under the hood, we want them to be
impressed with the neatness, consistency, and attention to
detail that they perceive.We want them to perceive that
professionals have been at work. If they see a scrambled
mess that looks like it was written by drunken sailors, they
will conclude that the same inattention to detail pervades
every other aspect of the project."
— Robert Martin, Clean Code
— C H A P T E R O N E —
Formatting
If you do nothing else,
at least be consistent.
If you’re going to be consistent,
why not be consistently GOOD?
@rzen
— C H A P T E R T W O —
You’re a decent human being,
you should have standards.
@rzen
— C H A P T E R T W O —
Standards
http://make.wordpress.org/core/handbook/coding-standards/ @rzen
— C H A P T E R T W O —
Standards
http://wp.tutsplus.com/articles/cheat-sheets
/the-wordpress-coding-standards-an-introduction/
Tom McFarlin goes into great
detail about why these matter
and are good for the planet:
@rzen
— C H A P T E R T W O —
Standards
if ( $this_thing == true ) {
// some code
}
if ( true == $this_thing ) {
// some code
}
S T A N D A R D :
Y O D A :
@rzen
— C H A P T E R T H R E E —
Working Fast = Working Clean
@rzen
— C H A P T E R F O U R —
Meaningful Names:
Clarity is King
@rzen
function proj_breadcrumb() {
// some code
}
function proj_get_breadcrumb() {
// some code
}
— C H A P T E R F O U R —
Meaningful Names
Y O U R C O D E :
B E T T E R C O D E :
@rzen
— C H A P T E R F O U R —
Meaningful Names
function do_awesome( $p = 0, $u = 0 ) {
// some code
}
Y O U R C O D E :
function do_awesome( $post_id = 0, $user_id = 0 ) {
// some code
}
B E T T E R C O D E :
@rzen
— C H A P T E R F I V E —
Functions should do one thing,
they should do it well,
they should do it only.
@rzen
— C H A P T E R F I V E —
Functions
The first rule of functions
is that they should be small.
The second rule of functions
is that they should be
smaller than that.
@rzen
— C H A P T E R F I V E —
Functions
BAD Function:
346 Lines, does logic, HTML, JS & CSS
@rzen
— C H A P T E R F I V E —
Functions
/**
* Return a user's points.
*
* @since 1.0.0
*
* @param integer $user_id User ID.
* @return integer Total Points.
*/
function badgeos_get_user_points( $user_id = 0 ) {
if ( ! $user_id ) {
$user_id = get_current_user_id();
}
return absint( get_user_meta( $user_id, '_badgeos_points', true ) );
}
GOOD Function:
4 lines long, does one thing
@rzen
Your functions should accept
as few parameters as possible.
Zero is best, followed by one,
followed by two, followed by
a serious re-evaluation.
— C H A P T E R F I V E —
Functions
@rzen
— C H A P T E R S I X —
Don’t comment bad code,
rewrite it.
@rzen
— C H A P T E R S I X —
Commenting
— P R E F A C E —
You should document
EVERYTHING.
@rzen
— C H A P T E R S I X —
Commenting
1.Obvious behavior
2.Misinformation
3.Information better
stored elsewhere
4.Unused functionality
T H E N , E L I M I N A T E T H E S E C O M M E N T S :
@rzen
— C H A P T E R S I X —
Commenting
function badgeos_get_user_points( $user_id = 0 ) {
// Get the current user ID if none specified
if ( ! $user_id ) {
$user_id = get_current_user_id();
}
// Return points cast as a positive integer (or 0, if invalid data)
return absint( get_user_meta( $user_id, '_badgeos_points', true ) );
}
1.Obvious behavior
@rzen
— C H A P T E R S I X —
Commenting
function badgeos_get_user_points( $user_id = 0 ) {
// Get the site ID first
// Then get the current user ID, if none specified
if ( ! $user_id ) {
$user_id = get_current_user_id();
}
// If user has negative points, set value to zero
return absint( get_user_meta( $user_id, '_badgeos_points', true ) );
}
2.Misinformation
@rzen
— C H A P T E R S I X —
Commenting
function badgeos_get_user_points( $user_id = 0 ) {
// Updated May 10, 2014
if ( ! $user_id ) {
$user_id = get_current_user_id();
}
// Modified by BR
return absint( get_user_meta( $user_id, '_badgeos_points', true ) );
}
3.Information better stored elsewhere
@rzen
— C H A P T E R S I X —
Commenting
function badgeos_get_user_points( $user_id = 0 ) {
// Get current user ID
// Removed May 10, 2014
// if ( ! $user_id ) {
// $user_id = get_current_user_id();
// }
return absint( get_user_meta( $user_id, '_badgeos_points', true ) );
}
4.Unused Functionality
@rzen
— C H A P T E R S E V E N —
Leave code better
than you found it.
@rzen
— C H A P T E R S E V E N —
Be a Good Code Scout
Incremental
improvement
is better than
NO improvement.
@rzen
— C H A P T E R S E V E N —
Be a Good Code Scout
1.Apply standard formatting
2.Rename for clarity
3.Break apart a big function
4.Add a DocBlock
5.Eliminate bad comments
E V E N J U S T O N E F I X I S G R E A T :
@rzen
— C H A P T E R E I G H T —
Clean code
is HardWork™
@rzen
— B R I A N R I C H A R D S —
@ R Z E N
W P S E S S I O N S . C O M
W E B D E V S T U D I O S . C O M
fin.
I Don't Hate You, I Just Hate Your Code

More Related Content

Viewers also liked

Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsVolodymyr Voytyshyn
 
Principles of microservices velocity
Principles of microservices   velocityPrinciples of microservices   velocity
Principles of microservices velocitySam Newman
 
Back to basics: как ставить задачи?
Back to basics: как ставить задачи?Back to basics: как ставить задачи?
Back to basics: как ставить задачи?Nimax
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricksJavier Eguiluz
 
DevOps модное слово или следующая ступень эволюции
DevOps модное слово или следующая ступень эволюцииDevOps модное слово или следующая ступень эволюции
DevOps модное слово или следующая ступень эволюцииAndrey Rebrov
 
A Beginners Guide to noSQL
A Beginners Guide to noSQLA Beginners Guide to noSQL
A Beginners Guide to noSQLMike Crabb
 
Hadoop Summit Europe 2014: Apache Storm Architecture
Hadoop Summit Europe 2014: Apache Storm ArchitectureHadoop Summit Europe 2014: Apache Storm Architecture
Hadoop Summit Europe 2014: Apache Storm ArchitectureP. Taylor Goetz
 

Viewers also liked (10)

Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design Patterns
 
Principles of microservices velocity
Principles of microservices   velocityPrinciples of microservices   velocity
Principles of microservices velocity
 
Back to basics: как ставить задачи?
Back to basics: как ставить задачи?Back to basics: как ставить задачи?
Back to basics: как ставить задачи?
 
Intro to DevOps
Intro to DevOpsIntro to DevOps
Intro to DevOps
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
 
DevOps модное слово или следующая ступень эволюции
DevOps модное слово или следующая ступень эволюцииDevOps модное слово или следующая ступень эволюции
DevOps модное слово или следующая ступень эволюции
 
A Beginners Guide to noSQL
A Beginners Guide to noSQLA Beginners Guide to noSQL
A Beginners Guide to noSQL
 
Hadoop Summit Europe 2014: Apache Storm Architecture
Hadoop Summit Europe 2014: Apache Storm ArchitectureHadoop Summit Europe 2014: Apache Storm Architecture
Hadoop Summit Europe 2014: Apache Storm Architecture
 
DevOps
DevOpsDevOps
DevOps
 
Introducing DevOps
Introducing DevOpsIntroducing DevOps
Introducing DevOps
 

Similar to I Don't Hate You, I Just Hate Your Code

Cena-DTA PHP Conference 2011 Slides
Cena-DTA PHP Conference 2011 SlidesCena-DTA PHP Conference 2011 Slides
Cena-DTA PHP Conference 2011 SlidesAsao Kamei
 
PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!Blanca Mancilla
 
Asynchronous data processing
Asynchronous data processingAsynchronous data processing
Asynchronous data processingAndrea Giuliano
 
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015Sergii Khomenko
 
Puppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with PuppetPuppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with PuppetPuppet
 
Rails in the enterprise
Rails in the enterpriseRails in the enterprise
Rails in the enterprisealexrothenberg
 
Getting Up and Running with BDD on Rails
Getting Up and Running with BDD on RailsGetting Up and Running with BDD on Rails
Getting Up and Running with BDD on Railselliando dias
 
Getting Up and Running with BDD on Rails
Getting Up and Running with BDD on RailsGetting Up and Running with BDD on Rails
Getting Up and Running with BDD on RailsNicholas Cancelliere
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottO'Reilly Media
 
WordPress in 30 minutes
WordPress in 30 minutesWordPress in 30 minutes
WordPress in 30 minutesOwen Winkler
 

Similar to I Don't Hate You, I Just Hate Your Code (11)

Cena-DTA PHP Conference 2011 Slides
Cena-DTA PHP Conference 2011 SlidesCena-DTA PHP Conference 2011 Slides
Cena-DTA PHP Conference 2011 Slides
 
Clean code
Clean codeClean code
Clean code
 
PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!
 
Asynchronous data processing
Asynchronous data processingAsynchronous data processing
Asynchronous data processing
 
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
 
Puppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with PuppetPuppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with Puppet
 
Rails in the enterprise
Rails in the enterpriseRails in the enterprise
Rails in the enterprise
 
Getting Up and Running with BDD on Rails
Getting Up and Running with BDD on RailsGetting Up and Running with BDD on Rails
Getting Up and Running with BDD on Rails
 
Getting Up and Running with BDD on Rails
Getting Up and Running with BDD on RailsGetting Up and Running with BDD on Rails
Getting Up and Running with BDD on Rails
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter Scott
 
WordPress in 30 minutes
WordPress in 30 minutesWordPress in 30 minutes
WordPress in 30 minutes
 

More from Brian Richards

Awarding Behavior & Altering Businesses with WordPress
Awarding Behavior & Altering Businesses with WordPressAwarding Behavior & Altering Businesses with WordPress
Awarding Behavior & Altering Businesses with WordPressBrian Richards
 
Becoming a Better Developer #WCA2
Becoming a Better Developer #WCA2Becoming a Better Developer #WCA2
Becoming a Better Developer #WCA2Brian Richards
 
Learn Something Useful Every Day
Learn Something Useful Every DayLearn Something Useful Every Day
Learn Something Useful Every DayBrian Richards
 
WPGR - Intro to Git (on the Command Line)
WPGR - Intro to Git (on the Command Line)WPGR - Intro to Git (on the Command Line)
WPGR - Intro to Git (on the Command Line)Brian Richards
 
Using Git on the Command Line
Using Git on the Command LineUsing Git on the Command Line
Using Git on the Command LineBrian Richards
 
Using a Framework Won't Box You In
Using a Framework Won't Box You InUsing a Framework Won't Box You In
Using a Framework Won't Box You InBrian Richards
 
Developing for Success -or- Any Fool Can Do This
Developing for Success -or- Any Fool Can Do ThisDeveloping for Success -or- Any Fool Can Do This
Developing for Success -or- Any Fool Can Do ThisBrian Richards
 

More from Brian Richards (7)

Awarding Behavior & Altering Businesses with WordPress
Awarding Behavior & Altering Businesses with WordPressAwarding Behavior & Altering Businesses with WordPress
Awarding Behavior & Altering Businesses with WordPress
 
Becoming a Better Developer #WCA2
Becoming a Better Developer #WCA2Becoming a Better Developer #WCA2
Becoming a Better Developer #WCA2
 
Learn Something Useful Every Day
Learn Something Useful Every DayLearn Something Useful Every Day
Learn Something Useful Every Day
 
WPGR - Intro to Git (on the Command Line)
WPGR - Intro to Git (on the Command Line)WPGR - Intro to Git (on the Command Line)
WPGR - Intro to Git (on the Command Line)
 
Using Git on the Command Line
Using Git on the Command LineUsing Git on the Command Line
Using Git on the Command Line
 
Using a Framework Won't Box You In
Using a Framework Won't Box You InUsing a Framework Won't Box You In
Using a Framework Won't Box You In
 
Developing for Success -or- Any Fool Can Do This
Developing for Success -or- Any Fool Can Do ThisDeveloping for Success -or- Any Fool Can Do This
Developing for Success -or- Any Fool Can Do This
 

Recently uploaded

Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptbibisarnayak0
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxVelmuruganTECE
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfChristianCDAM
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectssuserb6619e
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxNiranjanYadav41
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadaditya806802
 
chpater16.pptxMMMMMMMMMMMMMMMMMMMMMMMMMMM
chpater16.pptxMMMMMMMMMMMMMMMMMMMMMMMMMMMchpater16.pptxMMMMMMMMMMMMMMMMMMMMMMMMMMM
chpater16.pptxMMMMMMMMMMMMMMMMMMMMMMMMMMMNanaAgyeman13
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectErbil Polytechnic University
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 

Recently uploaded (20)

Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.ppt
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptx
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdf
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptx
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasad
 
chpater16.pptxMMMMMMMMMMMMMMMMMMMMMMMMMMM
chpater16.pptxMMMMMMMMMMMMMMMMMMMMMMMMMMMchpater16.pptxMMMMMMMMMMMMMMMMMMMMMMMMMMM
chpater16.pptxMMMMMMMMMMMMMMMMMMMMMMMMMMM
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction Project
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 

I Don't Hate You, I Just Hate Your Code

  • 1. I Don’t HateYou, I Just Hate Your Code — B R I A N R I C H A R D S — @rzen // WPSessions.com // WebDevStudios.com
  • 2. — C H A P T E R O N E — Formatting is important. @rzen
  • 3. — C H A P T E R O N E — Formatting indicator of buggy code #1 Inconsistent spacing is the @rzen
  • 4. — C H A P T E R O N E — Formatting "When people look under the hood, we want them to be impressed with the neatness, consistency, and attention to detail that they perceive.We want them to perceive that professionals have been at work. If they see a scrambled mess that looks like it was written by drunken sailors, they will conclude that the same inattention to detail pervades every other aspect of the project." — Robert Martin, Clean Code
  • 5. — C H A P T E R O N E — Formatting If you do nothing else, at least be consistent. If you’re going to be consistent, why not be consistently GOOD? @rzen
  • 6. — C H A P T E R T W O — You’re a decent human being, you should have standards. @rzen
  • 7. — C H A P T E R T W O — Standards http://make.wordpress.org/core/handbook/coding-standards/ @rzen
  • 8. — C H A P T E R T W O — Standards http://wp.tutsplus.com/articles/cheat-sheets /the-wordpress-coding-standards-an-introduction/ Tom McFarlin goes into great detail about why these matter and are good for the planet: @rzen
  • 9. — C H A P T E R T W O — Standards if ( $this_thing == true ) { // some code } if ( true == $this_thing ) { // some code } S T A N D A R D : Y O D A : @rzen
  • 10. — C H A P T E R T H R E E — Working Fast = Working Clean @rzen
  • 11. — C H A P T E R F O U R — Meaningful Names: Clarity is King @rzen
  • 12. function proj_breadcrumb() { // some code } function proj_get_breadcrumb() { // some code } — C H A P T E R F O U R — Meaningful Names Y O U R C O D E : B E T T E R C O D E : @rzen
  • 13. — C H A P T E R F O U R — Meaningful Names function do_awesome( $p = 0, $u = 0 ) { // some code } Y O U R C O D E : function do_awesome( $post_id = 0, $user_id = 0 ) { // some code } B E T T E R C O D E : @rzen
  • 14. — C H A P T E R F I V E — Functions should do one thing, they should do it well, they should do it only. @rzen
  • 15. — C H A P T E R F I V E — Functions The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that. @rzen
  • 16. — C H A P T E R F I V E — Functions BAD Function: 346 Lines, does logic, HTML, JS & CSS @rzen
  • 17. — C H A P T E R F I V E — Functions /** * Return a user's points. * * @since 1.0.0 * * @param integer $user_id User ID. * @return integer Total Points. */ function badgeos_get_user_points( $user_id = 0 ) { if ( ! $user_id ) { $user_id = get_current_user_id(); } return absint( get_user_meta( $user_id, '_badgeos_points', true ) ); } GOOD Function: 4 lines long, does one thing @rzen
  • 18. Your functions should accept as few parameters as possible. Zero is best, followed by one, followed by two, followed by a serious re-evaluation. — C H A P T E R F I V E — Functions @rzen
  • 19. — C H A P T E R S I X — Don’t comment bad code, rewrite it. @rzen
  • 20. — C H A P T E R S I X — Commenting — P R E F A C E — You should document EVERYTHING. @rzen
  • 21. — C H A P T E R S I X — Commenting 1.Obvious behavior 2.Misinformation 3.Information better stored elsewhere 4.Unused functionality T H E N , E L I M I N A T E T H E S E C O M M E N T S : @rzen
  • 22. — C H A P T E R S I X — Commenting function badgeos_get_user_points( $user_id = 0 ) { // Get the current user ID if none specified if ( ! $user_id ) { $user_id = get_current_user_id(); } // Return points cast as a positive integer (or 0, if invalid data) return absint( get_user_meta( $user_id, '_badgeos_points', true ) ); } 1.Obvious behavior @rzen
  • 23. — C H A P T E R S I X — Commenting function badgeos_get_user_points( $user_id = 0 ) { // Get the site ID first // Then get the current user ID, if none specified if ( ! $user_id ) { $user_id = get_current_user_id(); } // If user has negative points, set value to zero return absint( get_user_meta( $user_id, '_badgeos_points', true ) ); } 2.Misinformation @rzen
  • 24. — C H A P T E R S I X — Commenting function badgeos_get_user_points( $user_id = 0 ) { // Updated May 10, 2014 if ( ! $user_id ) { $user_id = get_current_user_id(); } // Modified by BR return absint( get_user_meta( $user_id, '_badgeos_points', true ) ); } 3.Information better stored elsewhere @rzen
  • 25. — C H A P T E R S I X — Commenting function badgeos_get_user_points( $user_id = 0 ) { // Get current user ID // Removed May 10, 2014 // if ( ! $user_id ) { // $user_id = get_current_user_id(); // } return absint( get_user_meta( $user_id, '_badgeos_points', true ) ); } 4.Unused Functionality @rzen
  • 26. — C H A P T E R S E V E N — Leave code better than you found it. @rzen
  • 27. — C H A P T E R S E V E N — Be a Good Code Scout Incremental improvement is better than NO improvement. @rzen
  • 28. — C H A P T E R S E V E N — Be a Good Code Scout 1.Apply standard formatting 2.Rename for clarity 3.Break apart a big function 4.Add a DocBlock 5.Eliminate bad comments E V E N J U S T O N E F I X I S G R E A T : @rzen
  • 29. — C H A P T E R E I G H T — Clean code is HardWork™ @rzen
  • 30. — B R I A N R I C H A R D S — @ R Z E N W P S E S S I O N S . C O M W E B D E V S T U D I O S . C O M fin.