SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
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
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
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
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringJuanCarlosMorales19600
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxsomshekarkn64
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 

Kürzlich hochgeladen (20)

Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
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...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
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
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineering
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptx
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 

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.