Slide 1

Slide 1 text

Coding Best Practices Tips & Tricks for Writing Clean Code in WordPress Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 2

Slide 2 text

Director of IT at Actionable.
 WordPress Developer. Happy to be visiting here from Ottawa. Have been a web developer since 1998, and using WordPress since 2009. shawnhooper.ca Hi, I’m Shawn! Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 3

Slide 3 text

Clean, Standard, Testable Code Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 7

Slide 7 text

Accessibility, PHP, JavaScript, HTML & CSS WordPress Coding Standard https://make.wordpress.org/core/handbook/best-practices/ coding-standards/ Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

• Proper Heading Structure • Semantic Markup • wp.a11y.speak() • Images & Icons. ALT for Images, in SVG. • Labels mandatory, not required to be visible .screen-reader-text class to hide labels. Accessibility Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 10

Slide 10 text

PHP Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 11

Slide 11 text

PHPDoc is the standard for comments Comments https://phpdoc.org/docs/latest/references/phpdoc/basic-syntax.html Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 12

Slide 12 text

Single vs. Double Quotes Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 13

Slide 13 text

Single vs. Double Quotes Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 14

Slide 14 text

Indentation The Tabs vs. Spaces Debate Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 15

Slide 15 text

Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 16

Slide 16 text

Use Tabs, Not Spaces Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

While we’re here… Add a comma after the last item in your array declaration Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 19

Slide 19 text

Dirty… Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 20

Slide 20 text

Clean… Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 21

Slide 21 text

https://www.thewpcrowd.com/wordpress/development/dont- dirty-diff-tips-writing-cleaner-php/ Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 22

Slide 22 text

{ } Braces Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 23

Slide 23 text

Braces Use braces instead of single line control structures. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 24

Slide 24 text

Braces Workaround: Use alternate structures like 
 if/endif and while/endwhile; Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 25

Slide 25 text

Shorthand PHP Tags Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 26

Slide 26 text

Shorthand PHP Tags Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Space Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 29

Slide 29 text

Space Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 30

Slide 30 text

Space Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 31

Slide 31 text

Space Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 32

Slide 32 text

Space Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 33

Slide 33 text

Space Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 34

Slide 34 text

Space Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 35

Slide 35 text

SQL If you have to write SQL Statements, capitalize SQL keywords like SELECT, FROM, WHERE, ORDER BY. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 36

Slide 36 text

SQL 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,… ); Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 37

Slide 37 text

Naming Things Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 38

Slide 38 text

There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.
 
 - Phil Karlton Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 39

Slide 39 text

Naming Things function names should be lower case with words separated by underscores. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 40

Slide 40 text

Naming Things Class names should be uppercase with words separated by underscores. Acronyms should be uppercase. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 41

Slide 41 text

Naming Things Constants should be uppercase with words separated by underscores. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 42

Slide 42 text

Naming Things Filenames 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

Slide 43

Slide 43 text

Yoda Conditions Variables on the right, constants and literals on the left. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 44

Slide 44 text

Ternary Operators Test that the statement is true, not false.
 
 ! empty() is allowed. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 45

Slide 45 text

Keep Code Simple No: Yes: Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 46

Slide 46 text

Don’t Extract Don’t use the PHP extract() function to create variables from an array. It’s hard to debug. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 47

Slide 47 text

JavaScript Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 48

Slide 48 text

Spacing Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Naming Conventions Constructors intended for use with new should use UpperCamelCase:
 
 function MyConstructor( ) …. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 51

Slide 51 text

HTML Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 52

Slide 52 text

Validated All HTML pages should be verified against the W3C validator to ensure that the markup is well formed. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 53

Slide 53 text

Self-Closing Elements
Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 54

Slide 54 text

Case For Machines: For Humans: Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 55

Slide 55 text

Quotes Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 56

Slide 56 text

Quotes Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 57

Slide 57 text

Indentation Indent PHP blocks to match HTML. Ident to match logical structure. Use Tabs. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 58

Slide 58 text

CSS Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 59

Slide 59 text

Structure Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 60

Slide 60 text

Selectors Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 61

Slide 61 text

Properties Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 62

Slide 62 text

Media Queries Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 63

Slide 63 text

Comments Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 64

Slide 64 text

Comments Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 65

Slide 65 text

Back to PHP…. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 66

Slide 66 text

Tips & Tricks for Clean Code Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 67

Slide 67 text

Single Purpose Methods Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

Reduce Indentation Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 70

Slide 70 text

Reduce Indentation @shawnhooper - shawnhooper.ca Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 71

Slide 71 text

Reduce Length of Loops Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 72

Slide 72 text

Reduce Length of Loops Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

Avoid Too Many Parameters Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 75

Slide 75 text

Avoid Too Many Parameters Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 76

Slide 76 text

Avoid Too Many Parameters Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 77

Slide 77 text

Avoid Too Many Parameters Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 78

Slide 78 text

Injection Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 79

Slide 79 text

Injection Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 80

Slide 80 text

Injection Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 81

Slide 81 text

Clean Switch Statements Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 82

Slide 82 text

Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 83

Slide 83 text

Automation There are tools available to help you keep your code clean. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 84

Slide 84 text

Code Standards Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 85

Slide 85 text

PHP CodeSniffer Checks that your code validates to a specified PHP Standard! Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 86

Slide 86 text

PHP CodeSniffer Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 87

Slide 87 text

WP Enforcer https://github.com/stevegrunwell/wp-enforcer Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 88

Slide 88 text

JSHint Checks that your JavaScript validates to the standard. Run with Grunt or Gulp Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 89

Slide 89 text

Unit Testing Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 90

Slide 90 text

PHPUnit Unit Testing Framework to perform dynamic tests on your PHP Code. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 91

Slide 91 text

Tape, Mocha, Jasmine Unit Testing Frameworks for JavaScript Code. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 92

Slide 92 text

UI Testing Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 93

Slide 93 text

Selenium Used to test your web application in a real browser. https://www.shawnhooper.ca/2017/07/selenium-ui-testing- wpcampus/ Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 94

Slide 94 text

Tenon An automated testing tool for web accessibility. Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 95

Slide 95 text

Code Reviews Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

Blog - shawnhooper.ca
 Twitter - @shawnhooper

Slide 98

Slide 98 text

ShawnHooper.ca 
 Twitter: @ShawnHooper THANK YOU! Blog - shawnhooper.ca
 Twitter - @shawnhooper