Super-power your development skills and write code that is reusable, maintainable and modifiable.
(you probably already use some of them)
Knowing more code is always a benefit when that code can be applied to a problem you are facing. Understanding and applying different development patterns can skill up a developer.
They allow you to:
They don’t dictate how the implementation is done at all. They outline a set of ideas that someone can apply to their current situation.
They create reusable, recognizable & robust solutions that are easier to maintain and quicker to iterate upon.
tech debt
in your code by encouraging good development practices.A code smell is an indicator of a deeper problem than the actual written code.
It could be a sign of overengineering or lack of thoughtful analysis before writing the code.
The cause could be:
The basis of many existing development patterns.
Don’t Repeat Yourself
Simply keeping these statements in mind as you work can help to avoid introducing code smells and avoid expensive maintenance.
Conditional statements are one example of something that varies or needs updated often. Encapsulation can improve the readability of your code by abstracting away the parts that are unimportant for the functionality of the code.
It also:
function add_css_class_for_landing_page($classes) {
if (
is_singluar( 'landing-page' ) ||
is_page_template( 'templates/landing-page.php' ) ||
in_array( get_the_ID(), [
25, 85, 126, 345, 658, 1007
] )
) {
$classes[] = 'a-landing-page';
}
return $classes;
}
add_filter( 'body_class', 'add_css_class_for_landing_page' );
function is_a_landing_page() {
return (
is_singluar( 'landing-page' ) ||
is_page_template( 'templates/landing-page.php' ) ||
in_array( get_the_ID(), [
25, 85, 126, 345, 658, 1007
] )
);
}
function add_css_class_for_landing_page($classes) {
if ( is_a_landing_page() ) {
$classes[] = 'a-landing-page';
}
return $classes;
}
add_filter( 'body_class', 'add_css_class_for_landing_page' );
In WordPress, the most commonly seen patterns are the Singleton, the Observer and the Facade.
By far the most talked-about pattern is the Singleton.
This is a question that doesn’t really have a single answer.
The most well-referenced source of patterns is found in a book written in 1994 by a group of 4 software engineers. The ‘Gang of Four’ as they have come to be know.
The book: Design Patterns: Elements of Reusable Object-Oriented Software contains 2 parts. The first part is a holistic view of building software. The 2nd part is the catalogue of patterns. This catalogue is collected based on common patterns that the authors had seen in their work building software.
Written by:
A singleton solves 2 problems at once:
private static
property to hold its instanceprivate
constructor methodstatic
getter method<?php
class MySingleton {
private static $instance;
private function __construct() {
// construction actions
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
Factory Method Or Simple Factory
<?php
class MyFactory {
public function build_pizza($type) {
switch ($type) {
case 'pinapple':
$pizza = new PinapplePizza();
break;
case 'pepperoni':
$pizza = new PepperoniPizza();
break;
default:
$pizza = new CheesePizza();
}
return $pizza;
}
}
You want to avoid modifying the client code as much as possible. It’s tested and stable.
Then your data provider changes the shape they send it to you in or how you retrieve it. It throws a spanner in your nicely tested code.
The solution is an adapter.
It sits between your client code and the provider and translates the calls between both parts of the system.
Somtimes called a wrapper.
Facades act as the front entrance to a complex system. The Facade is all that is exposed to the public. It might be more limited than the sum of all the parts of the system that it fronts – what is important is that it exposes what the client code needs.
Simple access and operation methods are placed in front of the complexity.
Complex setup or use cases are abstracted away. On the face of it, everything operates simply and efficiently.
Also known as ‘Event-Subscriber’, ‘Publisher/Subscriber’ or ‘Listener’.
The Observer pattern watches another object or event for when it changes and may do something based on the change.
It relies on the system being observed emitting events to expose when it updates.
WordPress includes a system that is basically an event driven architecture.
Actions and Filter hooks are events. When this event happens then do a thing.
In reality, there is no such thing as an action – they are abstractions.
All actions are really filters.
function
add_action( $tag, $function_to_add, $priority
= 10, $accepted_args
= 1 ) {
return
add_filter( $tag, $function_to_add, $priority, $accepted_args
);
}
Code so twisted together and hard to follow it’s like a tangle of spaghetti.
An accident waiting to happen.
Nicely formed classes and objects that make sense on their own but as a whole program.
It is a messy plate of raviolli.
Neatly arranged layers of code on top of each other.
To change one layer you have to adjust the layers either below or above it.
// You may think this code does nothing. You
// would be right. But when we remove this
// code the app crashes. We don't know why.
The biggest signs of The Blob is when one thing:
“We always use the Golden Hammer as a solution”
Most code is written by one person and most packages are maintained by very small teams. You do not know all the code that is out there.
Anytime you run into a problem you have not solved before it is worth your time to do some investigations into what is available and if it can fit your need.
Credits