Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Caleb Porzio @calebporzio

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Writing code that speaks

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." Martin Fowler

Slide 8

Slide 8 text

"Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. ... [Therefore,] making it easy to read makes it easier to write." Uncle Bob (Robert C Martin)

Slide 9

Slide 9 text

Give your code a voice

Slide 10

Slide 10 text

1. Avoid meaningless words 2. Strive for consistency 3. Don't make my brain hurt 4. Name according to scope 5. Don't make me look things up 6. Leverage metaphors

Slide 11

Slide 11 text

1. Avoid meaningless words Words that put my brain to sleep !

Slide 12

Slide 12 text

$data (1. Avoid meaningless words)

Slide 13

Slide 13 text

$data $data = ['id' => 1]; $client->get('/user', $data); (1. Avoid meaningless words)

Slide 14

Slide 14 text

$data $payload = ['id' => 1]; $client->get('/user', $payload); (1. Avoid meaningless words)

Slide 15

Slide 15 text

$items $items = getNavLinks(); foreach ($items as $item) {... (1. Avoid meaningless words)

Slide 16

Slide 16 text

$items $links = getNavLinks(); foreach ($links as $link) {... (1. Avoid meaningless words)

Slide 17

Slide 17 text

! The more abstracted your code is, the more abstract your words will be (1. Avoid meaningless words)

Slide 18

Slide 18 text

Drop the type suffix $namesArray = ['jack', 'jill']; (1. Avoid meaningless words)

Slide 19

Slide 19 text

Drop the type suffix $names = ['jack', 'jill']; (1. Avoid meaningless words)

Slide 20

Slide 20 text

Drop the type suffix $createdAtTimestamp = new DateTime; (1. Avoid meaningless words)

Slide 21

Slide 21 text

Drop the type suffix $createdAt = new DateTime; (1. Avoid meaningless words)

Slide 22

Slide 22 text

Hungarian notation ! $aNames = ['bob', 'carol', 'jim']; $sName = 'jack'; $iAge = 23; (1. Avoid meaningless words)

Slide 23

Slide 23 text

Hungarian notation ! $names = ['bob', 'carol', 'jim']; $name = 'jack'; $age = 23; (1. Avoid meaningless words)

Slide 24

Slide 24 text

Service classes $mailService = new MailService; $mailService->send($msg); (1. Avoid meaningless words)

Slide 25

Slide 25 text

Service classes $mailer = new Mailer; $mailer->send($message); (1. Avoid meaningless words)

Slide 26

Slide 26 text

$thing1, $thing2 (1. Avoid meaningless words)

Slide 27

Slide 27 text

$thing1, $thing2 $user1 = factory(User::class); $user2 = factory(User::class); login($user1); $this->assertTrue($user1->loggedIn()); $this->assertFalse($user2->loggedIn()); (1. Avoid meaningless words)

Slide 28

Slide 28 text

$thing1, $thing2 $user = factory(User::class); $visitor = factory(User::class); login($user); $this->assertTrue($user->loggedIn()); $this->assertFalse($visitor->loggedIn()); (1. Avoid meaningless words)

Slide 29

Slide 29 text

1. Avoid meaningless words 2. Strive for consistency

Slide 30

Slide 30 text

Pick a word and stick with it echo $blog->body; echo $comment->text; echo $testimonial->content; (2. Strive for consistency)

Slide 31

Slide 31 text

Pick a word and stick with it echo $blog->body; echo $comment->body; echo $testimonial->body; (2. Strive for consistency)

Slide 32

Slide 32 text

Pick a word and stick with it $post->image; $user->photo; $tag->thumbnail; (2. Strive for consistency)

Slide 33

Slide 33 text

Pick a word and stick with it $post->image; $user->image; $tag->image; (2. Strive for consistency)

Slide 34

Slide 34 text

Pick a word and stick with it service.api_key service.key service.public_key service.secret_key service.api_secret service.api_public service.secret ... (2. Strive for consistency)

Slide 35

Slide 35 text

Pick a word and stick with it service.api_key service.api_secret (2. Strive for consistency)

Slide 36

Slide 36 text

1. Avoid meaningless words 2. Strive for consistency 3. Don't make my brain hurt

Slide 37

Slide 37 text

Abbreviations I'm ok with ! auth authentication // TODO admin administrator id identifier db database i iteration int, bool integer / boolean e, z exception / zonda (3. Don't make my brain hurt)

Slide 38

Slide 38 text

Abbreviations I'm NOT ok with ! addr address attr attribute err error arr array opt option msg message img image (3. Don't make my brain hurt)

Slide 39

Slide 39 text

Dates date('j') / date('t') * 100; (3. Don't make my brain hurt)

Slide 40

Slide 40 text

Dates $dayOfMonth = date('j'); $totalDaysInMonth = date('t'); $dayOfMonth / $totalDaysInMonth * 100; (3. Don't make my brain hurt)

Slide 41

Slide 41 text

Dates (new DateTime)->setDate(date('Y'), date('m'), 1)->setTime(12, 0, 0); (3. Don't make my brain hurt)

Slide 42

Slide 42 text

Dates new DateTime('first day of this month midnight'); (3. Don't make my brain hurt)

Slide 43

Slide 43 text

Ugh! Regex preg_replace('/\s\s+/', ' ', $title); (3. Don't make my brain hurt)

Slide 44

Slide 44 text

Ugh! Regex stripOutExtraWhitespace($title); (3. Don't make my brain hurt)

Slide 45

Slide 45 text

Negatives ! if (! $post->isExpired()) { (3. Don't make my brain hurt)

Slide 46

Slide 46 text

Negatives ! if ($post->isActive()) { (3. Don't make my brain hurt)

Slide 47

Slide 47 text

Double Negatives ! ! $users->filter(function ($user) { return $user->group !== 'admin'; }); (3. Don't make my brain hurt)

Slide 48

Slide 48 text

Double Negatives ! ! $users->reject(function ($user) { return $user->group === 'admin'; }); (3. Don't make my brain hurt)

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

1. Avoid meaningless words 2. Strive for consistency 3. Don't make my brain hurt 4. Name according to scope

Slide 51

Slide 51 text

Compound method names class Cart { public function addProduct() {}; public function removeProduct() {}; public function changeProduct() {}; } $cart->addProduct(); (4. Name according to scope)

Slide 52

Slide 52 text

Compound method names class Cart { public products; } class Products { public function add() {}; public function remove() {}; public function change() {}; } $cart->products->add($product); (4. Name according to scope)

Slide 53

Slide 53 text

! Your names may be telling you that your code is ready for a refactoring

Slide 54

Slide 54 text

Compound variable names $transactionId; $transactionDate; $transactionAmount; (4. Name according to scope)

Slide 55

Slide 55 text

Compound variable names $transaction->id; $transaction->date; $transaction->amount; (4. Name according to scope)

Slide 56

Slide 56 text

1. Avoid meaningless words 2. Strive for consistency 3. Don't make my brain hurt 4. Name according to scope 5. Don't make me look things up

Slide 57

Slide 57 text

Bare parameters new Color(0, 12, 66); (5. Don't make me look things up)

Slide 58

Slide 58 text

Bare parameters Color::fromRGB(255, 255, 255); Color::fromHSL(0, 0, 100); (5. Don't make me look things up)

Slide 59

Slide 59 text

Bare parameters new HttpRequest(500); (5. Don't make me look things up)

Slide 60

Slide 60 text

Bare parameters new HttpRequest($timeoutInSeconds = 500); (5. Don't make me look things up)

Slide 61

Slide 61 text

Boolean flags json_decode($json, true); (5. Don't make me look things up)

Slide 62

Slide 62 text

Boolean flags json_decode($json, $returnArray = true); (5. Don't make me look things up)

Slide 63

Slide 63 text

Array Indexes $user = DB::fetchRowArray(... echo $user[2]; (5. Don't make me look things up)

Slide 64

Slide 64 text

Array Indexes $user = DB::fetchRowAssociativeArray(... echo $user['email']; (5. Don't make me look things up)

Slide 65

Slide 65 text

Bare integer $response = $request->post('/users', $payload); $this->assertEquals(201, $response->statusCode()); (5. Don't make me look things up)

Slide 66

Slide 66 text

Bare integer class HttpStatus { const CREATED = 201; } $response = $request->post('/users', $payload); $this->assertEquals(HttpStatus::CREATED, $response->statusCode()); (5. Don't make me look things up)

Slide 67

Slide 67 text

Implicit method name $post->publish(); // queues internally (5. Don't make me look things up)

Slide 68

Slide 68 text

Implicit method name $post->queueAndPublish(); (5. Don't make me look things up)

Slide 69

Slide 69 text

Implicit method name Post::getFirst(); // creates a new post if can't find one (5. Don't make me look things up)

Slide 70

Slide 70 text

Implicit method name Post::getFirstOrCreate(); (5. Don't make me look things up)

Slide 71

Slide 71 text

1. Avoid meaningless words 2. Strive for consistency 3. Don't make my brain hurt 4. Name according to scope 5. Don't make me look things up 6. Leverage metaphors

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

"This transfer of some quality of the known to the unknown is like a beam of light; we "see" (that is, we understand) something about the unknown in the light of the known." Mary Oliver (A Poetry Handbook)

Slide 74

Slide 74 text

Common pattern in apps $admin->logInAsOtherUser($user); (6. Leverage metaphors)

Slide 75

Slide 75 text

Common pattern in apps $admin->impersonate($user); (6. Leverage metaphors)

Slide 76

Slide 76 text

Common pattern in APIs $client->get('/users/currently_logged_in_user'); (6. Leverage metaphors)

Slide 77

Slide 77 text

Common pattern in APIs $client->get('/users/me'); (6. Leverage metaphors)

Slide 78

Slide 78 text

Wrapping it up

Slide 79

Slide 79 text

1. Avoid meaningless words 2. Strive for consistency 3. Don't make my brain hurt 4. Name according to scope 5. Don't make me look things up 6. Leverage metaphors

Slide 80

Slide 80 text

Choosing words is fun and creative

Slide 81

Slide 81 text

Our job is to listen and understand

Slide 82

Slide 82 text

Write code that speaks

Slide 83

Slide 83 text

No content