wordpress coding standards & best practices

97
Coding Best Practices Tips & Tricks for Writing Clean Code in WordPress WordCamp Winnipeg October 22, 2016 @ Red River College Blog - shawnhooper.ca Twitter - @shawnhooper

Upload: shawn-hooper

Post on 11-Apr-2017

264 views

Category:

Internet


2 download

TRANSCRIPT

Page 1: WordPress Coding Standards & Best Practices

Coding Best PracticesTips & Tricks for Writing Clean Code in WordPress

WordCamp Winnipeg October 22, 2016 @ Red River College

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 2: WordPress Coding Standards & Best Practices

Director of IT at Actionable.

Happy to be visiting here from Ottawa.

Have used WordPress since 2009 as a blogger, freelance developer, and in a corporate environment.

shawnhooper.ca

Hi, I’m Shawn!

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 3: WordPress Coding Standards & Best Practices

Clean, Standard, Testable Code

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 4: WordPress Coding Standards & Best Practices

Why Do We Write Clean Code?

• It’s easier to read • It reduces bugs • It requires less documentation • It reduces technical debt • Be nice to your teammates (code with empathy!)

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 5: WordPress Coding Standards & Best Practices

Why Do We Write Clean Code?

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

- John Woods

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 6: WordPress Coding Standards & Best Practices

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 7: WordPress Coding Standards & Best Practices

Accessibility, PHP, JavaScript, HTML & CSS

WordPress Coding Standard

https://make.wordpress.org/core/handbook/best-practices/coding-standards/

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 8: WordPress Coding Standards & Best Practices

All new or updated code released in WordPress must conform with the WCAG 2.0 guidelines at

level AA.

Accessibility

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 9: WordPress Coding Standards & Best Practices

• Proper Heading Structure • Semantic Markup • wp.a11y.speak() • Images & Icons. ALT for Images, <title> in SVG. • Labels mandatory, not required to be

visible .screen-reader-text class to hide labels.

Accessibility

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 10: WordPress Coding Standards & Best Practices

PHP

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 11: WordPress Coding Standards & Best Practices

PHPDoc is the standard for comments

Comments

https://phpdoc.org/docs/latest/references/phpdoc/basic-syntax.html

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 12: WordPress Coding Standards & Best Practices

Single vs. Double Quotes

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 13: WordPress Coding Standards & Best Practices

Single vs. Double Quotes

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 14: WordPress Coding Standards & Best Practices

IndentationThe Tabs vs. Spaces Debate

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 15: WordPress Coding Standards & Best Practices

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 16: WordPress Coding Standards & Best Practices

Use Tabs, Not Spaces

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 17: WordPress Coding Standards & Best Practices

Except….Blog - shawnhooper.ca Twitter - @shawnhooper

Page 18: WordPress Coding Standards & Best Practices

While we’re here…

Add a comma after the last item in your array declaration

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 19: WordPress Coding Standards & Best Practices

Dirty…

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 20: WordPress Coding Standards & Best Practices

Clean…

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 21: WordPress Coding Standards & Best Practices

https://www.thewpcrowd.com/wordpress/development/dont-dirty-diff-tips-writing-cleaner-php/

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 22: WordPress Coding Standards & Best Practices

{ }Braces

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 23: WordPress Coding Standards & Best Practices

BracesUse braces instead of single line control structures.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 24: WordPress Coding Standards & Best Practices

BracesWorkaround: Use alternate structures like

if/endif and while/endwhile;

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 25: WordPress Coding Standards & Best Practices

Shorthand PHP Tags

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 26: WordPress Coding Standards & Best Practices

Shorthand PHP Tags

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 27: WordPress Coding Standards & Best Practices

Optional Close PHP Tag

You don’t need a ?> tag at the end of your file. If you, make sure there is no white space after it.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 28: WordPress Coding Standards & Best Practices

Space

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 29: WordPress Coding Standards & Best Practices

Space

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 30: WordPress Coding Standards & Best Practices

Space

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 31: WordPress Coding Standards & Best Practices

Space

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 32: WordPress Coding Standards & Best Practices

Space

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 33: WordPress Coding Standards & Best Practices

Space

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 34: WordPress Coding Standards & Best Practices

Space

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 35: WordPress Coding Standards & Best Practices

SQLIf you have to write SQL Statements,

capitalize SQL keywords like SELECT, FROM, WHERE, ORDER BY.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 36: WordPress Coding Standards & Best Practices

SQLIf you have to write SQL Statements,

capitalize SQL keywords like SELECT, FROM, WHERE, ORDER BY.

In most cases though, you should use the functions provided by the WPDB Class.

or use $wpdb->prepare( $sql, $arg1, $arg2,… );

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 37: WordPress Coding Standards & Best Practices

Naming Things

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 38: WordPress Coding Standards & Best Practices

There are two hard things in computer science: cache invalidation, naming

things, and off-by-one errors.

- Phil Karlton

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 39: WordPress Coding Standards & Best Practices

Naming Thingsfunction names should be lower case with

words separated by underscores.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 40: WordPress Coding Standards & Best Practices

Naming ThingsClass names should be uppercase with

words separated by underscores. Acronyms should be uppercase.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 41: WordPress Coding Standards & Best Practices

Naming ThingsConstants should be uppercase with words

separated by underscores.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 42: WordPress Coding Standards & Best Practices

Naming ThingsFilenames should be in lowercase,

separated by hyphens.

Classes should be prepended by “class-“ and be named with the name of the class.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 43: WordPress Coding Standards & Best Practices

Yoda Conditions

Variables on the right, constants and literals on the left.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 44: WordPress Coding Standards & Best Practices

Ternary Operators

Test that the statement is true, not false.

! empty() is allowed.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 45: WordPress Coding Standards & Best Practices

Keep Code SimpleNo:

Yes:

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 46: WordPress Coding Standards & Best Practices

JavaScript

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 47: WordPress Coding Standards & Best Practices

Spacing

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 48: WordPress Coding Standards & Best Practices

Naming Conventions

Variable and function names should be full words, using camel case with a lowercase

first letter.

myVariable = ‘value’;

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 49: WordPress Coding Standards & Best Practices

Naming Conventions

Constructors intended for use with new should use UpperCamelCase:

function MyConstructor( ) ….

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 50: WordPress Coding Standards & Best Practices

HTML

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 51: WordPress Coding Standards & Best Practices

Validated

All HTML pages should be verified against the W3C validator to ensure that the markup

is well formed.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 52: WordPress Coding Standards & Best Practices

Self-Closing Elements

<br />

<input type=“text” name=“myname” />

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 53: WordPress Coding Standards & Best Practices

CaseFor Machines:

For Humans:

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 54: WordPress Coding Standards & Best Practices

Quotes

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 55: WordPress Coding Standards & Best Practices

Quotes

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 56: WordPress Coding Standards & Best Practices

IndentationIndent PHP blocks to match HTML. Ident to

match logical structure. Use Tabs.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 57: WordPress Coding Standards & Best Practices

CSS

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 58: WordPress Coding Standards & Best Practices

Structure

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 59: WordPress Coding Standards & Best Practices

Selectors

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 60: WordPress Coding Standards & Best Practices

Properties

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 61: WordPress Coding Standards & Best Practices

Media Queries

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 62: WordPress Coding Standards & Best Practices

Comments

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 63: WordPress Coding Standards & Best Practices

Comments

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 64: WordPress Coding Standards & Best Practices

Back to PHP….

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 65: WordPress Coding Standards & Best Practices

Tips & Tricks for Clean Code

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 66: WordPress Coding Standards & Best Practices

Single Purpose MethodsBlog - shawnhooper.ca Twitter - @shawnhooper

Page 67: WordPress Coding Standards & Best Practices

Single Purpose Methods

Your methods should do one thing, and only that one thing. If not:

1. It reduces where it can be used

2. It becomes harder to test

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 68: WordPress Coding Standards & Best Practices

Reduce Indentation

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 69: WordPress Coding Standards & Best Practices

Reduce Indentation

@shawnhooper - shawnhooper.ca

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 70: WordPress Coding Standards & Best Practices

Reduce Length of Loops

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 71: WordPress Coding Standards & Best Practices

Reduce Length of LoopsBlog - shawnhooper.ca Twitter - @shawnhooper

Page 72: WordPress Coding Standards & Best Practices

Keep Methods Short

If your method is longer than 20 lines of code, you can probably split it up into

smaller pieces.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 73: WordPress Coding Standards & Best Practices

Avoid Too Many Parameters

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 74: WordPress Coding Standards & Best Practices

Avoid Too Many Parameters

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 75: WordPress Coding Standards & Best Practices

Avoid Too Many Parameters

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 76: WordPress Coding Standards & Best Practices

Avoid Too Many Parameters

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 77: WordPress Coding Standards & Best Practices

InjectionBlog - shawnhooper.ca Twitter - @shawnhooper

Page 78: WordPress Coding Standards & Best Practices

Injection

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 79: WordPress Coding Standards & Best Practices

Injection

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 80: WordPress Coding Standards & Best Practices

Clean Switch Statements

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 81: WordPress Coding Standards & Best Practices

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 82: WordPress Coding Standards & Best Practices

Automation

There are tools available to help you keep your code clean.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 83: WordPress Coding Standards & Best Practices

Code Standards

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 84: WordPress Coding Standards & Best Practices

PHP CodeSniffer

Checks that your code validates to a specified PHP Standard!

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 85: WordPress Coding Standards & Best Practices

PHP CodeSniffer

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 86: WordPress Coding Standards & Best Practices

WP Enforcer

https://github.com/stevegrunwell/wp-enforcer

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 87: WordPress Coding Standards & Best Practices

JSHint

Checks that your JavaScript validates to the standard.

Run with Grunt or Gulp

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 88: WordPress Coding Standards & Best Practices

Unit Testing

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 89: WordPress Coding Standards & Best Practices

PHPUnit

Unit Testing Framework to perform dynamic tests on your PHP Code.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 90: WordPress Coding Standards & Best Practices

Tape, Mocha, Jasmine

Unit Testing Frameworks for JavaScript Code.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 91: WordPress Coding Standards & Best Practices

UI Testing

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 92: WordPress Coding Standards & Best Practices

SeleniumUsed to test your web application in a

real browser.

http://wordpress.tv/2016/06/20/ben-cool-ui-testing-with-selenium-in-php/

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 93: WordPress Coding Standards & Best Practices

TenonAn automated testing tool for web

accessibility.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 94: WordPress Coding Standards & Best Practices

Code Reviews

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 95: WordPress Coding Standards & Best Practices

Code Reviews

2 sets of eyes are better than one!

Can be done remotely.

Can be baked into pull requests.

Don’t take any criticism personally.

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 96: WordPress Coding Standards & Best Practices

Blog - shawnhooper.ca Twitter - @shawnhooper

Page 97: WordPress Coding Standards & Best Practices

ShawnHooper.ca

Twitter: @ShawnHooper

THANK YOU!

Blog - shawnhooper.ca Twitter - @shawnhooper