Slide 1

Slide 1 text

How to Write Better 
 Code Automatically Kaspars Dambis WordCamp Oslo, March 2018

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

https://youtube.com/kasparsdambis

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Use AI to Help You Write Better Code Computers

Slide 8

Slide 8 text

10 Ways to Work Less

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

“[..] he spent eight months automating all of the programming tasks. With all of his tasks fully automated by a computer, he was able to literally sit back and do whatever he wanted.”

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Use Computers to… Does it work with 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? Deploy code automatically if tests pass.

Slide 13

Slide 13 text

But How?

Slide 14

Slide 14 text

Start Small!

Slide 15

Slide 15 text

1. Use Good Text Editor Atom PHPStorm SublimeText

Slide 16

Slide 16 text

1. Use Good Text Editor Atom PHPStorm SublimeText

Slide 17

Slide 17 text

2. Use Version Control No!

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

2. Use Version Control

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

3. Use Git Hooks https://www.flickr.com/photos/mag3737/11903509664/

Slide 22

Slide 22 text

3. Use Git Hooks Run scripts before every commit! Commit only working code

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

3. Use Git Hooks $ 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 25

Slide 25 text

This should never happen again…

Slide 26

Slide 26 text

4. Use GitHub

Slide 27

Slide 27 text

4. Use GitHub Remote backup of your code Invites contributions Runs all checks in the cloud

Slide 28

Slide 28 text

4. Use GitHub with Travis CI

Slide 29

Slide 29 text

5. Use a Dependency Manager Composer for PHP dependencies npm for project dependencies

Slide 30

Slide 30 text

5. Use Node Package Manager

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

6. Use Code Linters

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

6. Use Code Linters http://jshint.com

Slide 35

Slide 35 text

6. Use Code Linters Also available in the “Cloud”

Slide 36

Slide 36 text

6. Use Code Linters

Slide 37

Slide 37 text

7. Use Coding Standards Silicon Valley TV Series

Slide 38

Slide 38 text

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

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

7. Use Coding Standards phpcs.xml

Slide 42

Slide 42 text

7. Use Coding Standards linter-phpcs Atom package

Slide 43

Slide 43 text

7. Use Coding Standards linter-phpcs Atom package

Slide 44

Slide 44 text

8. Use EditorConfig

Slide 45

Slide 45 text

9. Use Automated Testing 1 + 1 = 2

Slide 46

Slide 46 text

phpunit.xml 9. Use Automated Testing

Slide 47

Slide 47 text

A very simple PHPUnit test 9. Use Automated Testing

Slide 48

Slide 48 text

9. Use Automated Testing

Slide 49

Slide 49 text

9. Use Automated Testing A very simple QUnit test

Slide 50

Slide 50 text

9. Use Automated Testing

Slide 51

Slide 51 text

10. Use a Virtual Environment

Slide 52

Slide 52 text

10. Use a Virtual Environment

Slide 53

Slide 53 text

10. 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 54

Slide 54 text

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

Slide 55

Slide 55 text

10. Use a Virtual Environment

Slide 56

Slide 56 text

1. Use a good text editor 2. Use Git for version control 3. Use Git hooks for automated checks 4. Use GitHub for continuous integration and accepting contributions 5. Use package managers 6. Use code linters 7. Use coding standards 8. Use .editorconfig 9. Use automated testing 10.Use virtual development environment

Slide 57

Slide 57 text

Doug McIlroy
 Inventor of unix pipes and other wonders “As a programmer, it is your job to put yourself out of business. What you do today can be automated tomorrow.”

Slide 58

Slide 58 text

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