6. How is it even possible
for code to be this bad?
7. 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
23. 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
30. Why?
Who's the main recipient of the code we write – a computer, an
end user or a programmer?
The primary user of source
code is a programmer, perhaps
● your Customer
● your Colleague
● You
35. 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
36. Dog's Legs
public class CareTaker {
private Dog dog;
...
public void walkTheDog(){
dog.walk();
dog.stop(); // tree or grass etc
dog.doYourThing();
...
}
}
37. Dog's Legs
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();
...
}
}
38. Don't talk to strangers
http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/
39. Law of Demeter
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?
}
}
40. Law of Demeter
public class PaperBoy {
private Wallet myWallet;
...
public void chargeCustomer(Customer customer) {
Collection<Bill> payment = customer.getPayment(5);
myWallet.add(payment);
}
}
47. Summary
Source code vs machine code
Write with the audience in minde
Smart vs Professional
Code quality and retrospectives
Code Review (wtf!) vs pair programming
49. 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”
50. 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!
51. 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