Slide 1

Slide 1 text

How to Write Better 
 Code Automatically Kaspars Dambis WordCamp Rīga, November 2017

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

https://kaspars.net

Slide 4

Slide 4 text

https://youtube.com/kasparsdambis

Slide 5

Slide 5 text

Photo: Maurizio Pesce, https://flic.kr/p/emnk9K

Slide 6

Slide 6 text

Use AI to Help You Write Better Code Computers

Slide 7

Slide 7 text

Use Computers to… Check code for errors Check code formatting and style Test if 1+1=2

Slide 8

Slide 8 text

Use Computers to… Is this supported in PHP 5.3 and PHP 7.2? Does it work with WordPress 5.0 and Jetpack 5.0? Does the JS & CSS work in IE 8? Does it impact the page load time?

Slide 9

Slide 9 text

But How?

Slide 10

Slide 10 text

Start Small!

Slide 11

Slide 11 text

Use a Good Text Editor Atom PHPStorm SublimeText

Slide 12

Slide 12 text

Use a Good Text Editor Atom

Slide 13

Slide 13 text

Use Version Control

Slide 14

Slide 14 text

–User Support Request “All images are broken in the latest version of the plugin! Fix it ASAP!111” Use Version Control

Slide 15

Slide 15 text

Use Version Control

Slide 16

Slide 16 text

Use Version Control

Slide 17

Slide 17 text

Use Version Control Everyone uses Git WordPress uses Subversion (SVN) for plugins and themes

Slide 18

Slide 18 text

Use Git Hooks Runs scripts and checks before every commit! Commit only working code BONUS

Slide 19

Slide 19 text

Use Git Hooks BONUS .git/hooks/pre-commit

Slide 20

Slide 20 text

Use Git Hooks BONUS $ git commit -m "Fix user not found" PHP Parse error: syntax error, unexpected '}', expecting ';' in modules/custom-post-types- taxonomies/module.php on line 27 Errors parsing modules/custom-post-types- taxonomies/module.php

Slide 21

Slide 21 text

Use a Package Manager Composer for PHP dependencies npm for project dependencies

Slide 22

Slide 22 text

Use Node Package Manager

Slide 23

Slide 23 text

Use Node Package Manager $ git clone https://github.com/kas...org.git $ npm install

Slide 24

Slide 24 text

Use GitHub

Slide 25

Slide 25 text

Use GitHub A remote backup for your code Invites contributions Supports Continuous Integration

Slide 26

Slide 26 text

Use GitHub with Travis CI

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Use Code Linters

Slide 29

Slide 29 text

Use Code Linters https://github.com/squizlabs/PHP_CodeSniffer

Slide 30

Slide 30 text

Use Code Linters http://jshint.com

Slide 31

Slide 31 text

Use Code Linters Also available in the “Cloud”

Slide 32

Slide 32 text

Use Code Linters

Slide 33

Slide 33 text

Use Coding Standards Silicon Valley TV Series

Slide 34

Slide 34 text

Use Coding Standards Developers Who Use Spaces Make More Money Than Those Who Use Tabs https://stackoverflow.blog/2017/06/15/developers-use-spaces-make-money-use-tabs/

Slide 35

Slide 35 text

Use Coding Standards • Tabs or spaces for indentation? • Single or double quotes? • Closing PHP tags? • Braces or no braces for one-line conditionals?

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Use Coding Standards phpcs.xml

Slide 38

Slide 38 text

Use Coding Standards linter-phpcs Atom package

Slide 39

Slide 39 text

Use Coding Standards linter-phpcs Atom package

Slide 40

Slide 40 text

Use EditorConfig

Slide 41

Slide 41 text

Use Automated Testing 1 + 1 = 2

Slide 42

Slide 42 text

phpunit.xml Use Automated Testing

Slide 43

Slide 43 text

A very simple PHPUnit test Use Automated Testing

Slide 44

Slide 44 text

Use Automated Testing

Slide 45

Slide 45 text

Use Automated Testing A very simple QUnit test

Slide 46

Slide 46 text

Use Automated Testing

Slide 47

Slide 47 text

Use a Virtual Environment

Slide 48

Slide 48 text

Use a Virtual Environment

Slide 49

Slide 49 text

Use a Virtual Environment Varying Vagrant Vagrants (VVV) https://varyingvagrantvagrants.org Good for WP core development Can’t run two boxes at the same time

Slide 50

Slide 50 text

Use a Virtual Environment http://docs.chassis.io One WP install per virtual box Quick!

Slide 51

Slide 51 text

Use a Virtual Environment

Slide 52

Slide 52 text

Use a good text editor Use Git for version control and hooks Use a package manager Use GitHub for continuous integration and accepting contributions Use code linters (checkers) Use coding standards Use automated testing Use virtual development environment

Slide 53

Slide 53 text

Only a mock-up wptide.org

Slide 54

Slide 54 text

Kaspars Dambis kaspars.net Lovely robots 
 designed by Freepik http://www.freepik.com