Upload
shawn-hooper
View
136
Download
4
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
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
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
PHPDoc is the standard for comments
Comments
@shawnhooper - shawnhooper.ca
https://phpdoc.org/docs/latest/references/phpdoc/basic-syntax.html
While we’re here…
@shawnhooper - shawnhooper.ca
Add a comma after the last item in your array declaration
@shawnhooper - shawnhooper.ca
https://www.thewpcrowd.com/wordpress/development/dont-dirty-diff-tips-writing-cleaner-php/
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;
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.
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,… );
@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.
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( ) ….
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” />
Indentation
@shawnhooper - shawnhooper.ca
Indent PHP blocks to match HTML. Ident to match logical structure. Use Tabs.
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
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.
Automation
@shawnhooper - shawnhooper.ca
There are tools available to help you keep your code clean.
PHP CodeSniffer
@shawnhooper - shawnhooper.ca
Checks that your code validates to a specified PHP Standard!
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
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.
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
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.ca TheWPCrowd.com
Twitter:
@ShawnHooper
THANK YOU!
@shawnhooper - shawnhooper.ca