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

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 

Recently uploaded (20)

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 

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.