Slide 1

Slide 1 text

Clean Code

Slide 2

Slide 2 text

Complexity keeps rising

Slide 3

Slide 3 text

Code = Communication

Slide 4

Slide 4 text

Code = Communication “Any fool can write code that a computer can understand. 
 Good programmers write code that humans can understand.” —Refactoring, Martin Fowler with Kent Beck, 1996

Slide 5

Slide 5 text

Speed & Quality

Slide 6

Slide 6 text

SDO Performance Organizational performance Industry (control) Enterprise (control) CULTURE OF PSYCHOLOGICAL SAFETY Burnout Construct Control variable Second-order construct Common goal for team or organization Predictive relationship Mixed results Negative predictive relationship Continuous delivery Loosely coupled architecture Monitoring Deployment automation HEAVYWEIGHT CHANGE PROCESS CODE MAINTAINABILITY Trunk-based development CLEAR CHANGE PROCESS Continuous integration Automated testing Cloud - DISASTER RECOVERY TESTING - - - Availability Software delivery performance

Slide 7

Slide 7 text

SDO Performance Burnout Continuous delivery Loosely coupled architecture Monitoring Deployment automation CODE MAINTAINABILITY Trunk-based development Continuous integration Automated testing Cloud DISASTER RECOVERY TESTING - - Availability Software delivery performance

Slide 8

Slide 8 text

Clean Code is the basis for Secure Code

Slide 9

Slide 9 text

10x more time reading “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.” ― Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship

Slide 10

Slide 10 text

10x more time reading

Slide 11

Slide 11 text

Optimize? 10x more time reading

Slide 12

Slide 12 text

@nicokrijnen nkrijnen nicokrijnen Nico Krijnen

Slide 13

Slide 13 text

Value of software

Slide 14

Slide 14 text

Value of software Flexible

Slide 15

Slide 15 text

Value of software Works Can change

Slide 16

Slide 16 text

Value of software Works Can change =

Slide 17

Slide 17 text

Value of software Works Can change =

Slide 18

Slide 18 text

Value of software Works Can change =

Slide 19

Slide 19 text

Value of software Works Can change =

Slide 20

Slide 20 text

Code that is easy to change

Slide 21

Slide 21 text

Code that is easy to change • Easy to read and understand intention

Slide 22

Slide 22 text

Code that is easy to change • Easy to read and understand intention • Version control

Slide 23

Slide 23 text

Code that is easy to change • Easy to read and understand intention • Version control • Automated tests

Slide 24

Slide 24 text

Code that is easy to change • Easy to read and understand intention • Version control • Automated tests Safety net }

Slide 25

Slide 25 text

Clean Code?

Slide 26

Slide 26 text

Clean Code • Polite • Self-explaining Robert C. Martin – Uncle Bob

Slide 27

Slide 27 text

Clean Code • Polite • Self-explaining • Code without surprises Robert C. Martin – Uncle Bob Daniel Terhorst-North

Slide 28

Slide 28 text

Clean Code • Polite • Self-explaining • Code without surprises

Slide 29

Slide 29 text

Clean Code • Naming • Structure

Slide 30

Slide 30 text

Clean Code • Naming • Structure • TDD Kent Beck

Slide 31

Slide 31 text

Clean Code • Naming • Structure • TDD • Refactoring Kent Beck Martin Fowler

Slide 32

Slide 32 text

Victor Rentea Kent Beck

Slide 33

Slide 33 text

SOLID • Single-responsibility principle • Open–closed principle • Liskov substitution principle • Interface segregation principle • Dependency inversion principle Robert C. Martin – Uncle Bob

Slide 34

Slide 34 text

CUPID – for joyful coding • Composable • Unix philosophy • Predictable • Idiomatic • Domain-based https://dannorth.net/2022/02/10/cupid-for-joyful-coding/ Daniel Terhorst-North

Slide 35

Slide 35 text

CUPID – Composable Plays well with others • Small surface area • Intention-revealing • Minimal dependencies Daniel Terhorst-North

Slide 36

Slide 36 text

CUPID – Unix philosophy Does one thing well • A simple, consistent model • Single purpose vs. single responsibility Daniel Terhorst-North

Slide 37

Slide 37 text

CUPID – Predictable Does what you expect • Behaves as expected • Deterministic • Observable Daniel Terhorst-North

Slide 38

Slide 38 text

CUPID – Idiomatic Feels natural • Language idioms • Local idioms Daniel Terhorst-North

Slide 39

Slide 39 text

CUPID – Domain-based The solution domain models the problem domain 
 in language and structure • Domain-based language • Domain-based structure • Domain-based boundaries Daniel Terhorst-North

Slide 40

Slide 40 text

CUPID – for joyful coding • Composable • Unix philosophy • Predictable • Idiomatic • Domain-based https://dannorth.net/2022/02/10/cupid-for-joyful-coding/ Daniel Terhorst-North

Slide 41

Slide 41 text

Code is not done until it: 1. ... 2. ... 3. ... 4. ...

Slide 42

Slide 42 text

Absolutely gorgeous Code is not done until it: 1. Works 2. Communicates 3. No duplicates 4. Fewest methods and classes Fred George Kent Beck

Slide 43

Slide 43 text

Code That Fits in Your Head No more than 7 (±2) Mark Seemann

Slide 44

Slide 44 text

Test Driven Development • TDD helps code design • Intention = captured and re-enforced by tests • Tests make code safer and easier to change Kent Beck

Slide 45

Slide 45 text

Domain-Driven Design Keep systems as simple as possible 
 to solve real problems for your domain. Eric Evans

Slide 46

Slide 46 text

What if you have no tests? "There is only one way to eat an elephant: a bite at a time." understandlegacycode.com “Legacy Code is code without tests” Michael Feathers: Working Effectively with Legacy Code

Slide 47

Slide 47 text

Clean Code!

Slide 48

Slide 48 text

void printOwing() { printBanner(); // Print details. System.out.println("name: " + name); System.out.println("amount: " + getOutstanding()); } void printOwing() { printBanner(); printDetails(getOutstanding()); } void printDetails(double outstanding) { System.out.println("name: " + name); System.out.println("amount: " + outstanding); } vs

Slide 49

Slide 49 text

var d; // elapsed time in days var elapsedTimeInDays; var daysSinceCreation; var daysSinceModification; vs

Slide 50

Slide 50 text

vs if (age >= 18) { ... } else { ... } boolean isOldEnoughToBuyAlcohol = age >= 18; if (isOldEnoughToBuyAlcohol) { ... } else { ... } if (isOldEnoughToBuyAlcohol(age)) { ... } else { ... } public boolean isOldEnoughToBuyAlcohol(int age) { return age >= 18; } vs

Slide 51

Slide 51 text

public boolean checkPasswordStrength(String password) { boolean isNotBlank = password != null && !password.isBlank(); if (isNotBlank) { boolean hasSpecialChar = password.chars().anyMatch(Character::isLetterOrDigit); if (hasSpecialChar) { boolean longEnough = password.length() >= 10; if (longEnough) { return true; } else { throw new IllegalArgumentException("Must be at least 10 characters"); } } else { throw new IllegalArgumentException("Must contain at least one special character"); } } else { throw new IllegalArgumentException("Password must not be blank"); } } public void checkPasswordStrength(String password) { if (password == null || password.isBlank()) throw new IllegalArgumentException("Password must not be blank"); if (password.chars().noneMatch(Character::isLetterOrDigit)) throw new IllegalArgumentException("Must contain at least one special character"); if (password.length() < 10) throw new IllegalArgumentException("Must be at least 10 characters"); } vs

Slide 52

Slide 52 text

public void checkPasswordStrength(String password) { checkNotBlank(password); checkHasSpecialCharacter(password); checkHasMinimumLength(password); } private void checkNotBlank(String password) { if (password == null || password.isBlank()) throw new IllegalArgumentException("Password must not be blank"); } private void checkHasSpecialCharacter(String password) { if (password.chars().noneMatch(Character::isLetterOrDigit)) throw new IllegalArgumentException("Must contain at least one special character"); } private void checkHasMinimumLength(String password) { if (password.length() < 10) throw new IllegalArgumentException("Must be at least 10 characters"); } public void checkPasswordStrength(String password) { if (password == null || password.isBlank()) throw new IllegalArgumentException("Password must not be blank"); if (password.chars().noneMatch(Character::isLetterOrDigit)) { throw new IllegalArgumentException("Must contain at least one special character"); } if (password.length() < 10) { throw new IllegalArgumentException("Must be at least 10 characters"); } } vs

Slide 53

Slide 53 text

class Password { private final String password; public Password(String password) { this.password = password; checkNotBlank(); checkHasSpecialCharacter(); checkHasMinimumLength(); } private void checkNotBlank() { if (password == null || password.isBlank()) throw new IllegalArgumentException("Password must not be blank"); } private void checkHasSpecialCharacter() { if (password.chars().noneMatch(Character::isLetterOrDigit)) throw new IllegalArgumentException("Must contain at least one special character"); } private void checkHasMinimumLength() { if (password.length() < 10) throw new IllegalArgumentException("Must be at least 10 characters"); } ... }

Slide 54

Slide 54 text

2000 classes 25 lines per class 1 to 3 lines per method <2 cyclomatic complexity vs 100 classes 500 lines per class 5 - 80+ cyclomatic complexity

Slide 55

Slide 55 text

Why is not all code clean?

Slide 56

Slide 56 text

Business / Time pressure? • The need to go fast is real! • 'Slower but steady' requires discipline.

Slide 57

Slide 57 text

Lack of support? • How do you build understanding from business stakeholders • Hard to explain the need for refactoring. [don't] • Easy to explain the need for quality. [do] • You will fall when you run too fast. You may make the finish, but you won't be able to run the next race.

Slide 58

Slide 58 text

# developers doubles every 5 years At any point in time, 
 50% of developers 
 have less than 5 years experience

Slide 59

Slide 59 text

The path to Clean Code

Slide 60

Slide 60 text

Convince your organization

Slide 61

Slide 61 text

Convince your business # of Tests

Slide 62

Slide 62 text

Convince your business 16 Accelerate: State of DevOps 2019 | How Do We C SOFTWARE DEVELOPMENT SOFTWARE DEPLOYMENT SERVICE OPERAT Lead Time Change Fail Availability Deployment Frequency Time to Restore FOUR KEY METRICS The first four metrics that capture the effectiveness of the development and delivery process can be summarized in terms of throughput and stability. We measure the throughput of the so ware delivery process using lead time of code changes from check-in to release along with deployment fr Stability is measured using time to r the time it takes from detecting a us impacting incident to having it rem and change fail rate, a measure of t of the release process. PERFORMANCE METRICS # of Tests

Slide 63

Slide 63 text

Master the fundamentals Learn Framework / Library Use Learn Cloud landscape Use Learn Clean code Use Learn Use Switch programming language Switch programming language

Slide 64

Slide 64 text

It's a journey

Slide 65

Slide 65 text

Learn & Practice = Experience • TDD / BDD • Refactoring • Absolutely gorgeous • Version control • DDD

Slide 66

Slide 66 text

Helpful tooling • IDE / IntelliJ (hints + safe refactoring) • SonarLint / SonarQube • ArchUnit • Checkstyle • PMD, FindBugs, SpotBugs

Slide 67

Slide 67 text

Helpful tooling • IDE / IntelliJ (hints + safe refactoring) • SonarLint / SonarQube • ArchUnit • Checkstyle • PMD, FindBugs, SpotBugs 😀 🙅

Slide 68

Slide 68 text

Expand your vocabulary • Design patterns • Heuristics • Algorithms • Pair programming

Slide 69

Slide 69 text

Learning angles • Read books, watch talks • Follow training • Form group, do katas • Practice on side projects https://tinyurl.com/luminis-clean-code

Slide 70

Slide 70 text

Clean Code is written over time • First focus on making it work • Spend time to make it communicate • Your understanding of a codebase will grow • Any time you see or touch code, make it fit your latest insights

Slide 71

Slide 71 text

Clean as-you-code • Refactor in your task cycle • Boy/girl scout rule 
 "Always leave the campground cleaner than you found it." • Code rub

Slide 72

Slide 72 text

Take away

Slide 73

Slide 73 text

Take away Any codebase can become clean

Slide 74

Slide 74 text

Take away Any codebase can become clean Yours too!

Slide 75

Slide 75 text

Take away Any codebase can become clean Yours too! https://tinyurl.com/luminis-clean-code @nicokrijnen nkrijnen nicokrijnen