Writing code is easy but writing maintainable code is almost impossible. During this talk I will discuss some of the principles of coding that relates to WordPress but goes beyond that.
2. Marko Heijnen
• Lead developer of GlotPress
• Core contributor for WordPress
• Working for 1&1 as their
WordPress specialist
• Fan of new technologies
3. Quick note
This talk is not only for developers.
Knowing how code should look like is important to
judge it’s quality.
7. – Bjarne Stroustrup, inventor of C++
“I like my code be elegant and efficient”
“Clean code does one thing well”
Elegance
8. – Grady Booch, author of Object Oriented Analysis
and Design With Applications
“Clean code is simple and direct”
“Clean code reads like well-written prose”
Simple, direct, readable
9. – Dave Thomas, founder of OTI
“Clean code can be read”
“Clean code should be literate”
Literate
10. – Michael Feathers, author of Working Effectively with Legacy Code
“Clean code always looks like it was written by
someone who cares”
Care
11. – Ron Jeffries, author of Extreme Programming Installed
“No duplication, one thing, expressiveness, tiny
abstractions.”
Small, expressive, simple
12. – Ward Cunningham, inventor of Wiki, Fit, coinventor of eXtreme
Programming. Motive force behind Design Patterns
“You know you are working on clean code
when each routing you reads turns out to be
pretty much what you expected.”
What you expected
16. Meaningful names
• Classes should have a noun or noun phrase names but
should not be a verb
• Method names should have verb or verb phrase names
• Use descriptive names
17. The code
• Should be very small!
< 150 characters per line and < 20 lines
• Do one thing and do that right
• Limit it to one level of abstraction
• Reading code from top to bottom
• The ideal number of arguments for a function is zero
18. The code
• Don’t use flag arguments
• passing a boolean into a function
• Pass objects as a function argument to reduce the
arguments
• Have no side effect, does what is expected
• Don’t repeat yourself
• Only return data once
25. What does this mean for WordPress?
• Follow the WordPress coding standards
• A shared code base is an important aspect for clean code
• Have it prefix with something that is unique
26. What are the violations in WordPress
• A lot of global variables
• Functions/Methods should never echo
• Returning WP_Error instead of exceptions
• WP_DB
27. Last but not least
Test Driven Development: Unit Tests
28. The three laws of Test Driven Development
• First law
You may not write production code until you have written
a failing unit test
• Second law
You may not write more of a unit test than is sufficient to
fail, and not compiling is failing
• Third law
You may not write more production code than is sufficient
to pass the currently failing test
29. Unit tests is the best documentation of your code
It shows how you intended your code to be used
30. Keep test clean with the same standards as your
production code.
32. A single unit test should contain
• Single concept per test
• One assert per test is a good guideline
• Five other rules: F.I.R.S.T
• Fast
• Independent
• Repeatable
• Self-Validating
• Timely
34. Clean code & me
• I never studied Computer Science
• I do care about quality and structure
• I refuse to do quick wins or hacks
• Work together with others to become better
• Applying unit tests on existing projects
• A lot of refactoring and WTF moments
36. What I have learned from working
together with a Java developer
37. What I have learned from working
together with a Java developer
• Start applying all the things I discussed
• Using OOP more then just wrapping functions in a class
• Using a Wrapper / Decorator pattern
• Think more in advance how things should work
• And also think what could change