Upload
marco-beelen
View
38
Download
1
Embed Size (px)
Citation preview
Test Driven Development Part 2Designing Testable (Clean) Code
by @mcbeelenHaarlem Software Developer Meetup
Confidential Page 2
KvK 53479335
Lichtfabriekplein 1, 2031 TE Haarlem
P.O. Box 2031 , 2002 CA Haarlem
+31 (0)88 248 1632, [email protected]
www.ximedes.com
Confidential Page 2
Meetup Sponsor:
01-10-2015 Confidential Page 3
01-10-2015 Confidential Page 4
01-10-2015 Confidential Page 5
01-10-2015 Confidential Page 6
01-10-2015 Confidential Page 7
01-10-2015 Confidential Page 8
01-10-2015 Confidential Page 9
“Clean Code that works” by Ron Jeffries / Kent Beck:
1. meets the requirements passes the tests
2. reveals intention the code is easy to understand
3. contains no duplication DRY: Once and only Once
4. Build with fewest number of elements Remove everything else:
YAGNI!
01-10-2015 Confidential Page 10
01-10-2015 Confidential Page 11
Loose Coupling – High Cohesion
Methods in classesClasses in filesClasses in packagesPackages in a module
01-10-2015 Confidential Page 12
com.klm......modelcom.klm.....databasecom.klm.....servicecom.klm.....rest
Package by Feature com.klm.....airportcom.klm.....citycom.klm.....museumscom.klm.....restaurants
Package by Layer
01-10-2015 Confidential Page 13
Change is local
01-10-2015 Confidential Page 14
Mind sized componentsMaximum of number lines of code in a method?
01-10-2015 Confidential Page 15
hasGoodNames() isSolid()
adheresToLawOfDemeter()
01-10-2015 Confidential Page 16
Use words from the Ubiquitous Language.
A common, rigorous language to be usedbetween developers and users, without any ambiguity.
01-10-2015 Confidential Page 17
Just say the words!
01-10-2015 Confidential Page 18http://arlobelshee.com/good-naming-is-a-process-not-a-single-step/
01-10-2015 Confidential Page 19
01-10-2015 Confidential Page 20
Each component has just 1 responsibility.All parts of such a component should be related to that single responsibility.
Any logic is implemented in just one place.
Just one programming language per file
01-10-2015 Confidential Page 21
Law of Demeter
01-10-2015 Confidential Page 22
Law of Demeter
In a method of an object you are ONLY allowed to talk to:1. The object itself.
to call other methods of this object
2. The parameters of the method.to get access to information (or to invoke a callback)
3. Object created within that method.in order to delegate responsibility
01-10-2015 Confidential Page 23
Law of Demeter
payAmountDue(customer, amount) { wallet = customer.getWallet(); return wallet.getMoney(amount);}
payAmountDue(customer, amount) { return customer.askForMoney(20);}
Customer.askForMoney(amount) { return myWallet.getMoney(amount)}
01-10-2015 Confidential Page 24
http://www.planetgeek.ch/2014/11/18/clean-code-cheat-sheet-v-2-4/
01-10-2015 Confidential Page 25
01-10-2015 Confidential Page 26
1. You are here to LEARN. (and have fun)
2. Try to do your best.
3. Work in pairs.Find a partner in a similar programming language and operating system
01-10-2015 Confidential Page 27
01-10-2015 Confidential Page 28
Game of Life Kata
See you next time: March 2ndTest Driven Development
Driving Development by Tests Haarlem Software Developer Meetup