Slide 1

Slide 1 text

Clean Code A short version of: Let’s code professional

Slide 2

Slide 2 text

Erik Witthauer PHP developer for > 10 years aka: dknx01 Twitter: @ewnx01 speakerdeck: speakerdeck.com/dknx01 currently working at: FTI

Slide 3

Slide 3 text

Introduction ● Good developers, but still learning (your whole life) ● Writing code is easy, but what about ○ easy maintenance ○ readability ○ documentation (self or external ) ○ easy extending or changing (refactoring!?) ○ remembering what was last week/month/year in this software => We need this for professional software (and other stuff)

Slide 4

Slide 4 text

Clean Code Solution:

Slide 5

Slide 5 text

Meaningful names

Slide 6

Slide 6 text

Meaningful names ● names are everything and everywhere (variables, methods, classes, folders, namespace ...) ● intension revealing ● should answer all big questions (why it exists, what it does, how to use it) ○ what is this and do I know this in the next lines ○ do other understand it immediately ○ no confusion ○ double interpretation

Slide 7

Slide 7 text

Meaningful names ● comments should be avoided -> rethink the name ● short is not always good, long and meaningful is better ● avoid unnecessary information ○ e.g. the type: $registrationsArray = []; public function (array $fooArr) {...} ○ namespace ● avoid (own) abbreviations ● stay in the domain language (problem, solution, maybe business) ● searchable and pronounceable names

Slide 8

Slide 8 text

Meaningful names ● classes: only single nouns or noun phrases (Customer) ● methods: verbs or verb phrases (processCustomerAddress) ● (mostly) hide providers: sendViaGmail -> sendEmail

Slide 9

Slide 9 text

Methods / Functions

Slide 10

Slide 10 text

Methods / Functions ● small!!!!!!!!!! or even smaller!!!!! ● one things only, but his good (incl. error handling) ● Never use “And” or “Or” in naming -> doing too much ● top to bottom ● one abstraction layer ● switch: no big blocks -> call function ● if: own function, if possible

Slide 11

Slide 11 text

Methods / Functions ● descriptive names ● few arguments: 0 == ideal, 1 == perfect, 2 == ok, 3 == avoid it, >3 == very bad and explanation needed (what about an object?) ● avoid null arguments ● no side effects: (hidden calls or changes) ● only one visible output (avoid references, one type) ● command query separation (do something or answer something) ● use exceptions

Slide 12

Slide 12 text

Classes

Slide 13

Slide 13 text

Classes ● should be small ● Single Responsibility Principle ● less cohesion ● organize for change: changes are not a high risk

Slide 14

Slide 14 text

Comments

Slide 15

Slide 15 text

Comments ● necessary evil at best ● less is more ● no excuse for bad writing/coding (complex, unreadable ...) ● are NOT documentation ● be clear ● no redundancy, misleading, out dated ● avoid inline (hard to find), class/method docblock should be enough

Slide 16

Slide 16 text

Formatting

Slide 17

Slide 17 text

Formatting ● use standards if exists (e.g. PSR2) ● blank line between functions ● blanks line for structuring (not to much) ● must increase readability

Slide 18

Slide 18 text

Object and Data structures

Slide 19

Slide 19 text

Object and Data structures

Slide 20

Slide 20 text

Object and Data structures ● abstraction ● keep private stuff private ● avoid (too many) public properties ● use accessor methods (getter, setter, adder, remover ...) ● expose behaviour, hide data

Slide 21

Slide 21 text

Error handling

Slide 22

Slide 22 text

Error handling ● use exceptions ● avoid “magic” numbers ● provide descriptive error message (and context) ● exceptions for the callers need, not generics ● provide help for the normal way ● no return null; ● checking arguments (type hinting or check value )

Slide 23

Slide 23 text

Boundaries

Slide 24

Slide 24 text

Boundaries ● wrapper for external/3rd party classes ● use interfaces, bridges, (abstract) adapters ● clean boundaries everywhere ● do not rely to much on code out of your control

Slide 25

Slide 25 text

Unit testing

Slide 26

Slide 26 text

Unit testing ● define what a unit is in the team ● small, but many: one test one thing ● meaningful ● mock/stub anything outside the unit ● no external services ● keep them clean and understandable: readability, readability, readability ● brocken test are fine, but must be fixed (there are valid reason for the broken things)

Slide 27

Slide 27 text

Unit testing ● F.I.R.S.T. ○ Fast (frequent running, not long-running) ○ Independent (no running order, single run) ○ Repeatable (in any environment, at any time) ○ Self-Validating (boolean output) ○ Timely (write before production)

Slide 28

Slide 28 text

Unit testing ● Not business/use cases, they test the implementation ○ -> Testing pyramide ● forget a case, write a new test ● test domain specific language are fine ● tests are not documentation, but helping understanding ● TDD? ● write them

Slide 29

Slide 29 text

● Robert C. Martin: Clean Code ● Blogs: https://martinfowler. com ● conference talks at youtube Further reading

Slide 30

Slide 30 text

Questions? Comments? Discussions? Don’t beat the presenter, he could be right (?) Slide: https://speakerdeck.com/dknx01/clean-code-for-beginners

Slide 31

Slide 31 text

Thank you