2nd hardest thing in computer science

A presentation at PHPCon PL 2019 in November 2019 in Szczyrk, Poland by Paweł Lewtak

Slide 1

Slide 1

2nd hardest thing in computer science

Slide 2

Slide 2

Definition?

Slide 3

Slide 3

“There are only two hard things in Computer Science: cache invalidation and naming things.” Phil Carton

Slide 4

Slide 4

#2 Naming things*

Slide 5

Slide 5

*things variables methods classes modules comments inline docs commit messages

Slide 6

Slide 6

You don’t code for CPU

Slide 7

Slide 7

You don’t code for interpreter

Slide 8

Slide 8

You don’t code for compiler

Slide 9

Slide 9

You code for people

Slide 10

Slide 10

You code for other developers

Slide 11

Slide 11

You code for your future self

Slide 12

Slide 12

“Don’t code, write prose” Sławomir Sobótka

Slide 13

Slide 13

Slide 14

Slide 14

“Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.” Donald E. Knuth

Slide 15

Slide 15

Comprehension

Slide 16

Slide 16

~70%

Slide 17

Slide 17

function a($b) { sort($b); $c = count($b); if ($c % 2 === 1) { return $b[($c - 1) / 2]; } else { return ($b[$c/2 - 1] + $b[$c/2]) / 2; } }

Slide 18

Slide 18

function median($pool) { sort($pool); $size = count($pool); if ($size % 2 === 1) { return $pool[($size - 1] / 2]; } else { return ($pool[$size/2 - 1] + $pool[$size/2]) / 2; } }

Slide 19

Slide 19

Self-documenting code

Slide 20

Slide 20

Programming is mapping from problem domain via intermediate domain into programming domain

Slide 21

Slide 21

Worst variable name

Slide 22

Slide 22

data

Slide 23

Slide 23

Slide 24

Slide 24

Second worst name?

Slide 25

Slide 25

data2

Slide 26

Slide 26

$total = $price * $qty; $total2 = $total - $discount; $total2 += $total * $taxrate; $total3 = $purchase_order_value + $available_credit; if ($total2 < $total3) { echo “You can’t afford this order.”; }

Slide 27

Slide 27

$order_total = $price * $qty $payable_total = $order_total - $discount $payable_total += $payable_total * $taxrate $available_funds = $purchase_order_value + $availble_credit if ($payable_total < $available_funds) { echo “You can’t afford this order.”; }

Slide 28

Slide 28

“No-one sets out to write legacy code” Rachel Willmer

Slide 29

Slide 29

Broken window theory

Slide 30

Slide 30

Code will decay

Slide 31

Slide 31

Design patterns

Slide 32

Slide 32

Misapplied Java design patterns are the root of all AbstractWordFactoryFactory(“evil”) anonymous HN comment

Slide 33

Slide 33

Naming conventions CamelCaseClass methodName someVariable CAPITAL_CONSTANT

Slide 34

Slide 34

syntax < semanthics

Slide 35

Slide 35

Bad name Does more that what is says Says more than what it does Does the opposite Contains more than what it says Says more than what it contains Contains the opposite

Slide 36

Slide 36

Common issues

Slide 37

Slide 37

Pseudo getter

Slide 38

Slide 38

get_data() with extra operations inside

Slide 39

Slide 39

get_create_object()

Slide 40

Slide 40

Alternatives fetch find lookup create calculate

Slide 41

Slide 41

Not really a boolean

Slide 42

Slide 42

is_active() function is_active() { if (cond) { return ‘false’; } return ‘true’; }

Slide 43

Slide 43

is_valid() function is_valid() { if (input_is_valid) { return true; } }

Slide 44

Slide 44

Plural / singular names function get_person() { return array(‘John Doe’, ‘Jane Doe’); } function get_employers() { return ‘John Doe’; }

Slide 45

Slide 45

Misleading docs

Slide 46

Slide 46

function get_lowest_price($user) { }

Slide 47

Slide 47

/** * Actually it returns the highest price. */ function get_lowest_price($user) { }

Slide 48

Slide 48

/** * Actually it returns the highest price. */ function get_lowest_price($user) { if ($user === null) { return _get_highest_price(); } else { return _get_lowest_price($user); } }

Slide 49

Slide 49

More than one responsibility

Slide 50

Slide 50

Abbreviations pos mod abs auth

Slide 51

Slide 51

Synonyms

Slide 52

Slide 52

<ThatThing>Manager UserManager StringManager ProductManager etc.

Slide 53

Slide 53

Alternatives Builder Writer Adapter Factory Handler Provider Converter

Slide 54

Slide 54

Magic numbers

Slide 55

Slide 55

$client = new GuzzleHttp\Client(); $url = ‘https://2019.phpcon.pl/’; $response = $client->request(‘GET’, $url); $response_code = $response->getStatusCode(); if ($response_code === 200) { echo ‘It works!’; } elseif ($response_code === 418) { // ??? }

Slide 56

Slide 56

$client = new GuzzleHttp\Client(); $url = ‘https://2019.phpcon.pl/’; $response = $client->request(‘GET’, $url); $response_code = $response->getStatusCode(); if ($response_code === StatusCode::OK) { echo ‘Website works!’; } elseif ($response_code === StatusCode::IM_A_TEAPOT) { echo ‘Would like a cup of tea?’; }

Slide 57

Slide 57

Useless comments /** * Returns the data. / function get_data() {} /* * Return maximum ID value from the database. */ function get_max_id_from_db() {}

Slide 58

Slide 58

Explain why why,, not what or how

Slide 59

Slide 59

“Code should have comments, but if your file is more than 25% comments, that’s a red flag: you may be explaining bad code” Adam Culp

Slide 60

Slide 60

Commit messages

Slide 61

Slide 61

Slide 62

Slide 62

Good practices

Slide 63

Slide 63

Define dictionary

Slide 64

Slide 64

Short names

Slide 65

Slide 65

Do not use negation

Slide 66

Slide 66

is_not_enabled() is_disabled()

Slide 67

Slide 67

Consistent names in code & docs

Slide 68

Slide 68

Single responsibility

Slide 69

Slide 69

Domain terms

Slide 70

Slide 70

Think about it

Slide 71

Slide 71

ASCII only $Δ = 1; ++$Δ; echo $Δ;

Slide 72

Slide 72

Slide 73

Slide 73

Tests!

Slide 74

Slide 74

Commit message

Slide 75

Slide 75

Good commit message Speeds up review process Helps write release notes Helps future maintainers

Slide 76

Slide 76

Short (50 chars or less) summary of changes More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together. Further paragraphs come after blank lines. - Bullet points are okay, too - Typically a hyphen or asterisk is used for the bullet, preceded by a single space, with blank lines in between, but conventions vary here

Slide 77

Slide 77

Agree on standards Use english, please

Slide 78

Slide 78

Boy Scout Rule

Slide 79

Slide 79

Practice

Slide 80

Slide 80

Improve vocabulary

Slide 81

Slide 81

Refactor

Slide 82

Slide 82

Code reviews Short, bite size, single logical change

Slide 83

Slide 83

Code ownership

Slide 84

Slide 84

Commit messages

Slide 85

Slide 85

Research papers https://www.cqse.eu/publications/2005-concise-and-consistent-naming.pdf http://www.cs.loyola.edu/~lawrie/papers/lawrieJese07.pdf https://www.researchgate.net/publication/224079441_Relating_Identifier_Naming_Flaws_a http://www.veneraarnaoudova.com/wp-content/uploads/2014/10/2014-EMSE-Arnaodova-e

Slide 86

Slide 86

thank you