Slide 1

Slide 1 text

To OOP or not to OOP Nikolay Bachiyski, WordCamp Europe 2013

Slide 2

Slide 2 text

Nikolay

Slide 3

Slide 3 text

Automattic

Slide 4

Slide 4 text

http://extrapolate.me/ @nikolayb github://nb

Slide 5

Slide 5 text

Next 25 minutes:

Slide 6

Slide 6 text

0.Why should we care?

Slide 7

Slide 7 text

1. How WordPress developers see OOP

Slide 8

Slide 8 text

2. Better OOP

Slide 9

Slide 9 text

0. Why should we care?

Slide 10

Slide 10 text

WordPress is BIG

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

120K Lines of PHP

Slide 13

Slide 13 text

Needs some structure Lines of PHP

Slide 14

Slide 14 text

Structure

Slide 15

Slide 15 text

No Structure

Slide 16

Slide 16 text

Not many rules

Slide 17

Slide 17 text

Each small piece is easy to understand

Slide 18

Slide 18 text

Hard to change & improve

Slide 19

Slide 19 text

Impossible to understand deeply

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Structure = Modules + Communication

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Structure → Code

Slide 29

Slide 29 text

Choices in PHP: Procedural and OOP

Slide 30

Slide 30 text

Procedural

Slide 31

Slide 31 text

↑ Perceived simplicity

Slide 32

Slide 32 text

↓ Hard to hide implementation

Slide 33

Slide 33 text

↓ Prefix all the things (<5.3)

Slide 34

Slide 34 text

↓ Data sharing using globals only

Slide 35

Slide 35 text

↓ Hard to test

Slide 36

Slide 36 text

OOP

Slide 37

Slide 37 text

↓ Perceived complicatedness

Slide 38

Slide 38 text

1. How WordPress developers see OOP

Slide 39

Slide 39 text

A big reason WP is so popular is because it is not infatuated with the latest, greatest developer lust objects… “

Slide 40

Slide 40 text

One of the biggest problems I've found with OOP is that it forces the developer to bake in structure long before the developer actually understands what that structure should be… “

Slide 41

Slide 41 text

Classes==Good

Slide 42

Slide 42 text

OOP == Bad

Slide 43

Slide 43 text

?

Slide 44

Slide 44 text

One of the biggest problems I've found with OOP is that it forces the developer to bake in structure long before the developer actually understands what that structure should be… “

Slide 45

Slide 45 text

One of the biggest problems I've found with OOP is that it forces the developer to bake in structure long before the developer actually understands what that structure should be… “

Slide 46

Slide 46 text

Forces = always happens?

Slide 47

Slide 47 text

Building Useful Abstractions is HARD

Slide 48

Slide 48 text

Very Hard

Slide 49

Slide 49 text

Either refuse responsibility

Slide 50

Slide 50 text

Or try

Slide 51

Slide 51 text

Fail

Slide 52

Slide 52 text

Learn

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Try again

Slide 55

Slide 55 text

Fail better

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Succeed

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

OOP can be good

Slide 60

Slide 60 text

Do one thing, do it well

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

class WP_Screen { function get( $hook_name = '' ); function set_current_screen(); function in_admin( $admin = null ); function add_old_compat_help( $screen, $help ); function set_parentage( $parent_file ); function add_option( $option, $args = array() ); function get_option( $option, $key = false ); function get_help_tabs(); function get_help_tab( $id ); function add_help_tab( $args ); function remove_help_tab( $id ); function remove_help_tabs(); function get_help_sidebar(); function set_help_sidebar( $content ); function get_columns(); function render_screen_meta(); function show_screen_options(); function render_screen_options(); function render_screen_layout(); function render_per_page_options(); }

Slide 64

Slide 64 text

class WP_Screen { function get( $hook_name = '' ); function set_current_screen(); function in_admin( $admin = null ); function add_old_compat_help( $screen, $help ); function set_parentage( $parent_file ); function add_option( $option, $args = array() ); function get_option( $option, $key = false ); function get_help_tabs(); function get_help_tab( $id ); function add_help_tab( $args ); function remove_help_tab( $id ); function remove_help_tabs(); function get_help_sidebar(); function set_help_sidebar( $content ); function get_columns(); function render_screen_meta(); function show_screen_options(); function render_screen_options(); function render_screen_layout(); function render_per_page_options(); }

Slide 65

Slide 65 text

Responsible for or Represents a

Slide 66

Slide 66 text

No And’s, Or’s, or But’s

Slide 67

Slide 67 text

Short!

Slide 68

Slide 68 text

The big idea is “messaging” [...] The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be. “

Slide 69

Slide 69 text

Innocuous inheritance

Slide 70

Slide 70 text

WP_Image_Editor

Slide 71

Slide 71 text

WP_Image_Editor_Imagick extends WP_Image_Editor

Slide 72

Slide 72 text

WP_Image_Editor_GD extends WP_Image_Editor

Slide 73

Slide 73 text

Common logic in all subclasses

Slide 74

Slide 74 text

if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, apply_filters( 'jpeg_quality', $this- >quality, 'image_resize' ) ) ) ) $this->image->setImageCompressionQuality ( apply_filters( 'jpeg_quality', $quality, 'image_resize' ) ); GD: ImageMagick:

Slide 75

Slide 75 text

GD: ImageMagick: protected function update_size( $width = null, $height = null ) { … return parent::update_size( $width, $height ); } protected function update_size( $width = null, $height = null ) { … return parent::update_size( $width, $height ); }

Slide 76

Slide 76 text

Change in interface = change in every subclass

Slide 77

Slide 77 text

How to spot?

Slide 78

Slide 78 text

IS-A vs. HAS-A/USES

Slide 79

Slide 79 text

Inheritance for reuse

Slide 80

Slide 80 text

Gravatar widget is a WP Widget ✅✓

Slide 81

Slide 81 text

ImagickMagick is an Image Editor

Slide 82

Slide 82 text

WP Image Editor uses ImagickMagick

Slide 83

Slide 83 text

WP Image Editor uses GD

Slide 84

Slide 84 text

WP Image Editor has an image

Slide 85

Slide 85 text

Composition

Slide 86

Slide 86 text

$this->engine = new WP_Image_Engine_GD(); $this->engine = new WP_Image_Engine_ImageMagick(); $this->image = $this->engine->load( $filename ); $new_size = $this->image->update_size();

Slide 87

Slide 87 text

function save( $filename = null, $mime_type = null ) { … $jpeg_quality = apply_filters( 'jpeg_quality', … ); $this->image->save( $filename, $mime_type, array( 'jpeg_quality' => $jpeg_quality ) ); … }

Slide 88

Slide 88 text

What to remember?

Slide 89

Slide 89 text

Communication between modules is everything

Slide 90

Slide 90 text

Structure is hard

Slide 91

Slide 91 text

We can’t get better without trying and failing first

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Growing Object-Oriented Software, Guided by Tests http://www.growing-object-oriented-software.com/

Slide 94

Slide 94 text

?