Slide 1

Slide 1 text

Better Code Design in PHP BETTERCODE() 2023 | ONLINE @[email protected]

Slide 2

Slide 2 text

Anna Filina • Coding since 1997. • PHP, Java, C#, etc. • Legacy archaeology. • Test automation. • Talks and workshops. • YouTube videos. • Filina Consulting.

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Object Mutual reference Collaborator

Slide 5

Slide 5 text

Object One-way reference Collaborator

Slide 6

Slide 6 text

Object Chatty Collaborator Step 1 Step 2 Step 3

Slide 7

Slide 7 text

Object Less chatty Collaborator Task Step 1 Step 2 Step 3

Slide 8

Slide 8 text

Is the complexity justified? Object 1 Object 2 Object 3 Object 4 Bus

Slide 9

Slide 9 text

Design Drive • Avoid complexity until I need it. • Do I understand what’s going on in terms of the business use case?

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Objects should speak business, not technical.

Slide 12

Slide 12 text

$lineItems = $this->orders->getLineItems($pendingOrderNumber); $taxTotals = $this->taxes->calculateForLineItems($lineItems);

Slide 13

Slide 13 text

Project • Marketplace for short-term rental: • Search properties for rent by city, date and number of guests. • Reserve & pay.

Slide 14

Slide 14 text

ReservationService ::reserve() PaymentManager ReservationManager processPayment(data) Templating saveReservation(data) confirmReservation(data) html EmailManager payment savePayment(data) confirmReservation(html)

Slide 15

Slide 15 text

ReservationService ::reserve() PaymentManager ReservationManager processPayment(data) Templating saveReservation(data) confirmReservation(data) html EmailManager payment savePayment(data) confirmReservation(html)

Slide 16

Slide 16 text

MakeReservation ReservationRepository add(paidReservation) ReservationNotifier PaymentProcessor capture(charge) paymentConfirmation notifyBuyer(paidReservation)

Slide 17

Slide 17 text

MakeReservation ReservationRepository add(paidReservation) ReservationNotifier PaymentProcessor capture(charge) paymentConfirmation notifyBuyer(paidReservation)

Slide 18

Slide 18 text

MakeReservation ReservationRepository add(paidReservation) ReservationNotifier PaymentProcessor capture(charge) paymentConfirmation notifyBuyer(paidReservation)

Slide 19

Slide 19 text

MakeReservation ReservationRepository add(paidReservation) notifyBuyer(paidReservation) ReservationNotifier PaymentProcessor capture(charge) paymentConfirmation

Slide 20

Slide 20 text

MakeReservation ReservationRepository add(paidReservation) ReservationNotifier PaymentProcessor capture(charge) paymentConfirmation notifyBuyer(paidReservation)

Slide 21

Slide 21 text

Good design needs no diagrams.

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Simple Code • No need for framework. • No need to design the database. • No need to have a UI. • Code the business rules.

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Handle HTTP Request Return HTTP Response 2 adults in Lviv on Jan 1 to Jan 7 Results containing “Lviv for 4”

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Handle HTTP Request Return HTTP Response 2 adults in Lviv on Jan 1 to Jan 7 Results containing “Lviv for 4”

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

A complex system that works is invariably found to have evolved from a simple system that worked. — John Gall

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Too reusable becomes unusable.

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Input/output Business rules Infrastructure Use case

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

PropertySearchHandler Lviv, Lviv Oblast, Ukraine Validate anyway. Convert to coordinates. CitySearchHandler Lviv Lviv, Lviv Oblast, Ukraine Lviv Boulevard, Oshawa, ON

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

PropertySearchHandler 49.8397, 24.0297 Longitude: -180 to +180 Latitude: -90 to +90 CitySearchHandler Lviv Lviv, Lviv Oblast, Ukraine 49.8397 24.0297

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Code is simply a model of the business rules and knowledge.

Slide 56

Slide 56 text