Next up we are with Michael Bastos here at WordCamp San Diego.
Quick disclaimer…this talk is very technical, geared towards advanced users.
Testing, refactoring, and more.
Why refactor PHP code? do it well.
* Never “easily” throw away working code
– It’s still existing code that works, don’t “easily” throw it away
* existing production code = MONEY
– If people are using a plugin, it’s time and money that was spent
* opportunity cost is basic economics
* time and good developers are scarce
* working code saves you time and resources
* WooCommerce is a refactor of Jigishop
* WPMU is a refactor of WordPress
* WordPress itself is a refactor of b2
* John O’Nolan wants to fork WP into Ghost
“Why is my code so bad?”
* in general, the php community avoided proper methodologies
* bad habits were due to easy learning
* older version of php lacked real support for Object Oriented (OO) development patterns and designs
* don’t dismiss old themes and plugins, refactor them by first learning what is bad code
Bad code is …
* when you can’t follow the logic
* when you’re afraid to touch it
* A ton of code within a disorganized mess
* functional and procedural
you know it’s time to refactor.
Person Best Practices
* Objectify your theme code, move it out of functions.php into separate php class files
* keep methods at a 5 to 10 line minimum for easy reading and understanding
* if possible, write tests before you start coding
unit testing is the default for WordPress, write tests. there are a number of good examples to build tests for your code.
“Don’t just code for coding sake”
Installing WordPress Tests
* create a test directory inside a new theme directory
* git clone the WP test from github directly, start building out the tests and connected with WP
[Michael is digging deep into actual WordPress unit testing]
If you’re not already writing OO code, you need to start. very important for caching and performance.
[Michael diving deep into OO programming, “Refactoring Inline Methods”]
Breaking down your code so it’s easy to read and simple calculations
“Other things to look for…”
* Remove unnecessary duplication
* dealing with long methods
* handling large classes
* wrangling a long list of parameters
Michael is a huge fan of Child Roots Theme
Michael loves working with WP MVC
This is his own fork of the original.
that’s it folks! Stay tuned for another session or two later today.