Top Banner
#WPAZ ADVANCED DEVELOPER MEETUP
27

Reduce, Reuse, Refactor

Jan 27, 2017

Download

Software

cklosowski
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Reduce, Reuse, Refactor

# W PA ZA D VA N C E D D E V E L O P E R M E E T U P

Page 2: Reduce, Reuse, Refactor

C H R I S K L O S O W S K I

• Co-Lead Developer - Easy Digital Downloads

• Founding Developer - Post Promoter Pro

• Writing - KungFuGrep.com

• Home brewing

• Weird Dad

• @cklosowski (usually)

Page 3: Reduce, Reuse, Refactor

cklosowskieasydigitaldownloads.com | kungfugrep.com

Page 4: Reduce, Reuse, Refactor

T I M E I S M O N E YR A N G I N G F R O M $ 3 0 - $ 1 5 0 / H O U R , D E V T I M E I S N ’ T C H E A P

Page 5: Reduce, Reuse, Refactor

P R E V E N T I N E F F I C I E N C YT H R O U G H C O D E , W E C A N R E D U C E D E V T I M E

Reduce Code Complexity Reuse Common Patterns Refactor When Needed

Page 6: Reduce, Reuse, Refactor
Page 7: Reduce, Reuse, Refactor
Page 8: Reduce, Reuse, Refactor

C O N T E X T S W I T C H I N G I S B A DH T T P : / / C H R I S K . I O / I / C O N T E X T

Page 9: Reduce, Reuse, Refactor

I N A P E R F E C T W O R L D …C O N T E X T S W I T C H I N G I S A N E C E S S A R Y E V I L

Page 10: Reduce, Reuse, Refactor

O P T I M I Z I N G W O R K F L O WM A K I N G O U R A C T U A L “ F L O W ” T I M E M O R E E F F E C T I V E

Page 11: Reduce, Reuse, Refactor

R E D U C E C O M P L E X I T YT H E H A R D E R T O R E A D , T H E H A R D E R T O F I X

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by

definition, not smart enough to debug it.”

-Brian Kernighan: Co-Developer of Unix

Page 12: Reduce, Reuse, Refactor

W H AT I S C O M P L E X I T Y ?

• Difficult to read

• “Tricky” solutions

• Cyclomatic complexity

• Examples…?

• You Bet!

Page 13: Reduce, Reuse, Refactor

if ( true === $foo ) {

foreach ( $bar as $key => $baz ) {

$bar[ $key ] = strtolower( $baz );

}

}

H A R D T O R E A Dfunction execute_data_input( $data = array() ) { // Hard to know what is needed to use `execute_data_input` }

function execute_data_input( $arg1 = '', $arg2 = '', $arg3 = '' ) { // Easily readable and defined argument sets }

if($foo==true) { foreach($bar as $key=>$baz) $bar[$key]=strtolower($baz); }

Page 14: Reduce, Reuse, Refactor

T R I C K Y C O D E

// Input $utm = array( 'campaign' => 'PostPromoterPro', 'source' => 'twitter', 'medium' => 'social', );

// Output is a string utm_campaign=PostPromoterPro&utm_source=twitter&utm_medium=social

Page 15: Reduce, Reuse, Refactor

T R I C K Y C O D E

$utm_string .= implode( '&', array_map( function ( $v, $k ) { return 'utm_' . $k . '=' . $v; }, $utm, array_keys( $utm ) ) );

$first = true; foreach ( $utm as $key => $value ) { if ( ! $first ) { $utm_string .= '&'; }

$utm_string .= 'utm_' . $key . '=' . $value; $first = false; }

Page 16: Reduce, Reuse, Refactor

R E U S E C O M M O N PAT T E R N SC R E A T E C O M M O N PA T T E R N S A N D C O N V E N T I O N S

Page 17: Reduce, Reuse, Refactor

R E U S I N G Y O U R PAT T E R N S

• Standardize

• Function Naming

• Variable Naming

• Directory Structure

• Abstract Data Models

• Build Base Classes

Page 18: Reduce, Reuse, Refactor

A L L A B O U T T H AT B A S ES L I D E S W I T H O U T A P U N A R E N ’ T R E A L LY S L I D E S …

‣ EDD_Recurring_Gateway‣ EDD_Recurring_2Checkout extends EDD_Recurring_Gateway‣ EDD_Recurring_Authorize extends EDD_Recurring_Gateway‣ EDD_Recurring_Stripe extends EDD_Recurring_Gateway‣ EDD_Recurring_PayPal extends EDD_Recurring_Gateway‣ …

Object Inheritance - When you extend a class, the subclass inherits all of the public and protected methods

from the parent class. Unless a class overrides those methods, they will retain their original functionality.

Page 19: Reduce, Reuse, Refactor

B U I L D A T E M P L AT EH T T P S : / / G I T H U B . C O M / E A S Y D I G I TA L D O W N L O A D S / E D D - E X T E N S I O N - B O I L E R P L A T E

Page 20: Reduce, Reuse, Refactor

R E FA C T O R I N G I S O KI T ’ S N O T A N E V I L W O R D A N D I S S O M E T I M E S N E C E S S A R Y

Code refactoring is the process of restructuring existing code without changing its external behavior.

Page 21: Reduce, Reuse, Refactor

W H Y R E FA C T O RW E ’ R E N O T M A K I N G I T D O A N Y T H I N G D I F F E R E N T ! ?

Page 22: Reduce, Reuse, Refactor

B E N E F I T S O F R E FA C T O R I N G

• Performance Improvements

• Improve Readability

• Reduce Complexity

• Seeing a Trend?

• Improve Matainability

• Adapt New Best Practices

• Foundation Improvements

• Improve Extensibility

Page 23: Reduce, Reuse, Refactor

S T O R Y T I M EA ‘ R E C U R R I N G PA Y M E N T S ’ C O M I N G O F A G E TA L E

Page 24: Reduce, Reuse, Refactor

August 30, 2012 First Commit, Ever

The Recurring Payments Story

January 23, 2015 Pressnomics 3

May 19, 2015 2.4 Development Starts

Jan 28, 2016 Preview Post

Feb 24, 2016 2.4 Goes Live

Jan 21, 2016 Live on AffiliateWP

Page 25: Reduce, Reuse, Refactor

The Recurring Payments Story

Page 26: Reduce, Reuse, Refactor

The Recurring Payments Story

• No “Backwards Compatibility” • Introduced Gateway Base Class • Introduced Classes for: • Subscriber • Subscription • Emails • Reminders

• Completely Refactored • Raised the Price

Page 27: Reduce, Reuse, Refactor