99
Clean Code Tips & Tricks for Writing Clean Code in WordPress WordCamp Toronto August 7, 2016 @ Ryerson University

Writing Clean, Standards Compliant, Testable Code for WordPress

Embed Size (px)

Citation preview

Clean CodeTips & Tricks for Writing Clean Code in WordPress

WordCamp Toronto August 7, 2016 @ Ryerson University

Chief Technology Officer 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 thewpcrowd.com

Hi, I’m Shawn!

@shawnhooper - shawnhooper.ca

Clean, Standard, Testable Code

@shawnhooper - shawnhooper.ca

Why Do We Write Clean Code?

@shawnhooper - shawnhooper.ca

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

Why Do We Write Clean Code?

@shawnhooper - shawnhooper.ca

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

- John Woods

@shawnhooper - shawnhooper.ca

Accessibility, PHP, JavaScript, HTML & CSS

WordPress Coding Standard

@shawnhooper - shawnhooper.ca

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

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

level AA.

Accessibility

@shawnhooper - shawnhooper.ca

• 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

@shawnhooper - shawnhooper.ca

PHP

@shawnhooper - shawnhooper.ca

PHPDoc is the standard for comments

Comments

@shawnhooper - shawnhooper.ca

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

Single vs. Double Quotes

@shawnhooper - shawnhooper.ca

Single vs. Double Quotes

@shawnhooper - shawnhooper.ca

Indentation

@shawnhooper - shawnhooper.ca

The Tabs vs. Spaces Debate

@shawnhooper - shawnhooper.ca

Use Tabs, Not Spaces

@shawnhooper - shawnhooper.ca

Except….

@shawnhooper - shawnhooper.ca

While we’re here…

@shawnhooper - shawnhooper.ca

Add a comma after the last item in your array declaration

@shawnhooper - shawnhooper.ca

Dirty…

@shawnhooper - shawnhooper.ca

Clean…

@shawnhooper - shawnhooper.ca

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

{ }Braces

@shawnhooper - shawnhooper.ca

Braces

@shawnhooper - shawnhooper.ca

Use braces instead of single line control structures.

Braces

@shawnhooper - shawnhooper.ca

Workaround: Use alternate structures like if/endif and while/endwhile;

Shorthand PHP Tags

@shawnhooper - shawnhooper.ca

Shorthand PHP Tags

@shawnhooper - shawnhooper.ca

Optional Close PHP Tag

@shawnhooper - shawnhooper.ca

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

Space

@shawnhooper - shawnhooper.ca

Space

@shawnhooper - shawnhooper.ca

Space

@shawnhooper - shawnhooper.ca

Space

@shawnhooper - shawnhooper.ca

Space

@shawnhooper - shawnhooper.ca

Space

@shawnhooper - shawnhooper.ca

Space

@shawnhooper - shawnhooper.ca

SQL

@shawnhooper - shawnhooper.ca

If you have to write SQL Statements, capitalize SQL keywords like SELECT,

FROM, WHERE, ORDER BY.

SQL

@shawnhooper - shawnhooper.ca

If 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.

SQL

@shawnhooper - shawnhooper.ca

If 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,… );

Naming Things

@shawnhooper - shawnhooper.ca

@shawnhooper - shawnhooper.ca

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

things, and off-by-one errors.

- Phil Karlton

Naming Things

@shawnhooper - shawnhooper.ca

function names should be lower case with words separated by underscores.

Naming Things

@shawnhooper - shawnhooper.ca

Class names should be uppercase with words separated by underscores. Acronyms

should be uppercase.

Naming Things

@shawnhooper - shawnhooper.ca

Constants should be uppercase with words separated by underscores.

Naming Things

@shawnhooper - shawnhooper.ca

Filenames should be in lowercase, separated by hyphens.

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

Yoda Conditions

@shawnhooper - shawnhooper.ca

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

Ternary Operators

@shawnhooper - shawnhooper.ca

Test that the statement is true, not false.

! empty() is allowed.

Keep Code Simple

@shawnhooper - shawnhooper.ca

No:

Yes:

JavaScript

@shawnhooper - shawnhooper.ca

Spacing

@shawnhooper - shawnhooper.ca

Spacing

@shawnhooper - shawnhooper.ca

Naming Conventions

@shawnhooper - shawnhooper.ca

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

first letter.

myVariable = ‘value’;

Naming Conventions

@shawnhooper - shawnhooper.ca

Constructors intended for use with new should use UpperCamelCase:

function MyConstructor( ) ….

HTML

@shawnhooper - shawnhooper.ca

Validated

@shawnhooper - shawnhooper.ca

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

is well formed.

Self-Closing Elements

@shawnhooper - shawnhooper.ca

<br />

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

Case

@shawnhooper - shawnhooper.ca

For Machines:

For Humans:

Quotes

@shawnhooper - shawnhooper.ca

Quotes

@shawnhooper - shawnhooper.ca

Indentation

@shawnhooper - shawnhooper.ca

Indent PHP blocks to match HTML. Ident to match logical structure. Use Tabs.

CSS

@shawnhooper - shawnhooper.ca

Structure

@shawnhooper - shawnhooper.ca

Selectors

@shawnhooper - shawnhooper.ca

Properties

@shawnhooper - shawnhooper.ca

Media Queries

@shawnhooper - shawnhooper.ca

Comments

@shawnhooper - shawnhooper.ca

Comments

@shawnhooper - shawnhooper.ca

Back to PHP….

@shawnhooper - shawnhooper.ca

Tips & Tricks for Clean Code

@shawnhooper - shawnhooper.ca

Single Purpose Methods

@shawnhooper - shawnhooper.ca

Single Purpose Methods

@shawnhooper - shawnhooper.ca

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

Reduce Indentation

@shawnhooper - shawnhooper.ca

Reduce Indentation

@shawnhooper - shawnhooper.ca

Reduce Length of Loops

@shawnhooper - shawnhooper.ca

Reduce Length of Loops

@shawnhooper - shawnhooper.ca

Keep Methods Short

@shawnhooper - shawnhooper.ca

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

smaller pieces.

Avoid Too Many Parameters

@shawnhooper - shawnhooper.ca

Avoid Too Many Parameters

@shawnhooper - shawnhooper.ca

Avoid Too Many Parameters

@shawnhooper - shawnhooper.ca

Avoid Too Many Parameters

@shawnhooper - shawnhooper.ca

Injection

@shawnhooper - shawnhooper.ca

Injection

@shawnhooper - shawnhooper.ca

Injection

@shawnhooper - shawnhooper.ca

Clean Switch Statements

@shawnhooper - shawnhooper.ca

@shawnhooper - shawnhooper.ca

Automation

@shawnhooper - shawnhooper.ca

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

Code Standards

@shawnhooper - shawnhooper.ca

PHP CodeSniffer

@shawnhooper - shawnhooper.ca

Checks that your code validates to a specified PHP Standard!

PHP CodeSniffer

@shawnhooper - shawnhooper.ca

WP Enforcer

@shawnhooper - shawnhooper.ca

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

JSHint

@shawnhooper - shawnhooper.ca

Checks that your JavaScript validates to the standard.

Run with Grunt or Gulp

Unit Testing

@shawnhooper - shawnhooper.ca

PHPUnit

@shawnhooper - shawnhooper.ca

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

Tape, Mocha, Jasmine

@shawnhooper - shawnhooper.ca

Unit Testing Frameworks for JavaScript Code.

UI Testing

@shawnhooper - shawnhooper.ca

Selenium

@shawnhooper - shawnhooper.ca

Used to test your web application in a real browser.

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

Tenon

@shawnhooper - shawnhooper.ca

An automated testing tool for web accessibility.

Code Reviews

@shawnhooper - shawnhooper.ca

Code Reviews

@shawnhooper - shawnhooper.ca

2 sets of eyes are better than one!

Can be done remotely.

Can be baked into pull requests.

Don’t take any criticism personally.

@shawnhooper - shawnhooper.ca

ShawnHooper.ca TheWPCrowd.com

Twitter:

@ShawnHooper

THANK YOU!

@shawnhooper - shawnhooper.ca