Clean Code 2

51

description

This is a 45 minute talk I've given a couple of times.Heavily based on the book Clean Code.

Transcript of Clean Code 2

Page 1: Clean Code 2
Page 2: Clean Code 2

Fredrik Wendt

Page 3: Clean Code 2

Clean Codewhat, why, how

Page 4: Clean Code 2

http://blog.xebia.com/2009/01/clean-code-vs-implementation-patterns/

Page 5: Clean Code 2

Disclaimer

Correlation betweenClean code and success?

Page 6: Clean Code 2

How is it even possiblefor code to be this bad?

Page 7: Clean Code 2

Exhibits• Hudson class is 3900+

LOC

• Hudson class is untestable Singleton

• Constructor calls a method that gets the singleton instance and calls a method on the instance.

• Hudson extends Node, but Node calls methods defined in Hudson

Page 8: Clean Code 2

WTF!So why care – Jenkins is great?

Page 9: Clean Code 2
Page 10: Clean Code 2

”Smart” vs Professional• Smart

● Great coding skills● Writes advanced code● String r; // lowercase url

• Professional● Readable code● Maintainable code● 'Clarity is king'● String lowerCaseUrl

OfCurrentPage;

http://www.slideshare.net/JandV/clean-code-summary

Page 11: Clean Code 2

What is Clean Code?

Page 12: Clean Code 2

int d; // elapsed time in days

Page 13: Clean Code 2

int d; // elapsed time in days

int elapsedTimeInDays;

Page 14: Clean Code 2

int d; // elapsed time in days

int elapsedTimeInDays;

int daysSinceCreation;

Page 15: Clean Code 2

int d; // elapsed time in days

int elapsedTimeInDays;

int daysSinceCreation;

int fileAgeInDays;

Page 16: Clean Code 2

public List<int[]> getThem() {

List<int[]> list1 = new ArrayList<int[]>();

for (int[] x : theList)

if (x[0] == 4)

list1.add(x);

return list1;

}

Page 17: Clean Code 2

public List<int[]> getThem() {

List<int[]> list1 = new ArrayList<int[]>();

for (int[] cell : gameBoard)

if (cell[STATUS_VALUE] == FLAGGED)

list1.add(cell);

return list1;

}

Page 18: Clean Code 2

public List<int[]> getFlaggedCells() {

List<int[]> flaggedCells = new ArrayList<int[]>();

for (int[] cell : gameBoard)

if (cell[STATUS_VALUE] == FLAGGED)

flaggedCells.add(cell);

return flaggedCells;

}

Page 19: Clean Code 2

public List<Cell> getFlaggedCells() {

List<Cell> flaggedCells = new ArrayList<Cell>();

for (Cell cell : gameBoard)

if (cell.isFlagged())

flaggedCells.add(cell);

return flaggedCells;

}

Page 20: Clean Code 2

Clean code always

looks like it was written by

someone who cares

Page 21: Clean Code 2

Clean code always

looks like it was wrtten by

somone who craes

Page 22: Clean Code 2

Readable

Maintainable

Changeable

Page 23: Clean Code 2

Agile Manifesto

Individuals and interactions over processes and tools

Working software over comprehensive documentation

Customer collaboration over contract negotiation

Responding to change over following a plan

Page 24: Clean Code 2
Page 25: Clean Code 2

Readable?

Page 26: Clean Code 2

~ 90% of your time is reading

What if you'dddouble write-time

and therebycut read-time

by half?

Page 27: Clean Code 2

public class Part {

private String m_dsc; // The textual description

void setName(String name) {

m_dsc = name;

}

}

public class Part {

private String description;

void setDescription(String description) {

this.description = description;

}

}

Page 28: Clean Code 2

Still, why bother?

Page 29: Clean Code 2

Code is written for humans!

Page 30: Clean Code 2

Why?Who's the main recipient of the code we write – a computer, an end user or a programmer?

The primary user of sourcecode is a programmer, perhaps

● your Customer● your Colleague● You

Page 31: Clean Code 2

How should I write code?

Page 32: Clean Code 2

DRY - Don't Repeat Yourself

YAGNI - You Ain't Gonna Need It

Page 33: Clean Code 2

small

Page 34: Clean Code 2

Meaningful Names• Intention-Revealing

• Pronounceable

• Avoid Encoding

• Classes

• Methods

• JobQueue

• Words from the domain

• Comments

• Formatting!

Page 35: Clean Code 2

Law of Demeter

More formally, the Law of Demeter for functions requires that a method M of an object O

may only invoke the methods of the following kinds of objects:

1. O itself 2. M's parameters 3. any objects created/instantiated within M 4. O's direct component objects 5. a variable, accessible by O, in the scope of M

Page 36: Clean Code 2

public class CareTaker {

private Dog dog;

...

public void walkTheDog(){

dog.walk();

dog.stop(); // tree or grass etc

dog.doYourThing();

...

}

}

Dog's Legs

Page 37: Clean Code 2

public class CareTaker {

private Dog dog;

...

public void walkTheDog(){

for (Leg leg : dog.getLegs())

leg.move();

for (Leg leg : dog.getLegs())

leg.stopMoving();

dog.brain.urinationCenter.releaseUrge();

...

}

}

Dog's Legs

Page 38: Clean Code 2

Don't talk to strangers

http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/

Page 39: Clean Code 2

public class PaperBoy {

private Wallet myWallet;

...

public void chargeCustomer(Customer customer) {

Wallet wallet = customer.getWallet();

Collection<Bill> bills = wallet.getBills();

Collection<Bill> payment = extract(5, bills);

myWallet.add(payment); // or bills?

}

}

Law of Demeter

Page 40: Clean Code 2

public class PaperBoy {

private Wallet myWallet;

...

public void chargeCustomer(Customer customer) {

Collection<Bill> payment = customer.getPayment(5);

myWallet.add(payment);

}

}

Law of Demeter

Page 41: Clean Code 2

1small

Single Responsibility

Page 42: Clean Code 2
Page 43: Clean Code 2

Boy Scout Rule

Leave the campground cleaner than you found it.

Page 44: Clean Code 2

Hur mäter man Clean Code?Eller kodkvalitet?

Page 45: Clean Code 2

Can you measure Clean Code?Or code quality?

Page 46: Clean Code 2

The only valid measurement of

code quality

WTFs / minute

Page 47: Clean Code 2

Summary

Source code vs machine codeWrite with the audience in minde

Smart vs ProfessionalCode quality and retrospectives

Code Review (wtf!) vs pair programming

Page 48: Clean Code 2

Code with no tests

Legacy Code

Page 49: Clean Code 2

What about the code that's already in my backpack?

• Get the old man on track – make the code testable (and tested)

• Fix bugs brought out in daylight by the tests

• ”Make it right”

Page 50: Clean Code 2

What about the code that's already in my backpack?

• Get the old man on track – make the code testable (and tested)

• Fix bugs brought out in daylight by the tests

• ”Make it right” – refactor!

Page 51: Clean Code 2

Code without tests ...

• is legacy code

• is not refactored

• unlikely to be changed by someone other than the person who wrote it (the past few days)

• is heavy load/back pack