How to Troubleshoot Apps for the Modern Connected Worker
ITCamp 2013 - Florin Coros - Driving Your Team Towards Code Quality
1. itcampro@ itcamp13# Premium conference on Microsoft technologies
Driving Your Team Towards
Code Quality
Florin Coros
www.rabs.ro
florin.coros@rabs.ro
@florincoros
www.florincoros.wordpress.com
2. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesHuge thanks to our sponsors!
3. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesAbout me
RABS, Co-Founder
ISDC, Software Architect
@florincoros
5. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesWhy Code Quality?
6. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesChange Predictability
- How much time to add this new small feature?
- Hm… let’s see… so it is very similar with what we have, right? Hm… about 2 to 3 days I think...
7. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesChange Predictability
crap…. I have to change much more than I thought … No way I can do this by tomorrow
… I think I’m going to work on this 2 to 3 weeks maybe
8. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesIt works. Don’t touch it!
9. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesGood Design vs Bad Design
http://martinfowler.com/bliki/DesignStaminaHypothesis.html
10. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesTechnical Debt
11. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesFunctions, Classes, Modules
12. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesDesigns
1 2
3 4
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2010/ARC201
13. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesDesigns
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2010/ARC201
14. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesDesign Principles & Design Patterns
15. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesKnowledge vs Experience
You learn the rules of chess in a few hours
You become a good chess player after years
of practicing
You need to practice with strong opponents
You need to go to tournaments to meet
strong opponents
You learn the rules of GO in a few minutes
You become a good GO player after many
years of practicing
Same as at chess, but much more exercise
is needed because it is more complex
16. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesAlternative: GOOD Unit Tests
It is very small
It can be written easily and runs
quickly
It’s readable, maintainable,
trustworthy and fully automated
It should run in isolation
It should test ONE thing
If it fails you should know exactly
where the bug is
17. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesHow does GOOD UT get us there?
OOD Principles
SOLID
DRY
IoC & DI
LowCoupling
High Coherence
…..…
Design Patterns
Composite
Chain Of Responsibility
Decorator
…….
Can you TEST it?
Good Enough
GOOD Unit Tests
Small
In Isolation
Test ONE Thing
Easy to implement
18. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesGood Unit Tests - easy to write
less than 5 minutes to implement
few lines of code. Less than 10 -15
see from a glimpse what the test checks
19. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesGood Unit Tests - Run in Isolation
Isolate the code under test
from the rest of your system,
by creating seams, to be able
to plug fakes (stubs & mocks)
which you can control in
your test
“There is no object-oriented
problem that cannot be
solved by adding a layer of
indirection, except, of
course, too many layers of
indirection.”
20. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesGood Unit Test - test ONE thing
Check only ONE thing in
your tests. If the test fails
you know exactly where the
problem is. You do not need
to do step-by-step
debugging
21. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesGood Unit Tests - not integration tests
B
C
f()
g()
h()
h’()
Depend on abstractions not on implementation
details (DIP)
Use IoC for instantiating objects, or Factory
design patterns
Visible dependencies for our classes
Low coupling and high coherence
Extensibility, Reusability (OCP)
Design Patterns
Integration tests:
HIGH COSTS & LOW BENEFITS
22. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesGood Unit Tests - not integration tests
Screen
Keyboard
translate()
write()
read()
Depend on abstractions not on implementation
details (DIP)
Use IoC for instantiating objects, or Factory
design patterns
Visible dependencies for our classes
Low coupling and high coherence
Extensibility, Reusability (OCP)
Design Patterns
Integration tests:
HIGH COSTS & LOW BENEFITS
23. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesGood Unit Tests - not integration tests
IText
Output
IText
Input
translate()
write()
read()
Depend on abstractions not on implementation
details (DIP)
Use IoC for instantiating objects, or Factory
design patterns
Visible dependencies for our classes
Low coupling and high coherence
Extensibility, Reusability (OCP)
Design Patterns
Integration tests:
HIGH COSTS & LOW BENEFITS
24. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesEliminate circular dependencies
A simple designed system
cannot be large
It has low coupling and high
coherence
25. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesEliminate circular dependencies
A system is large and too rigid to
change if it has circular
dependencies
It has high coupling and low
coherence
26. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesContinuous & Small Cycles
CODE
UNIT
TEST
REFACTOR
Short cycles between unit test and
production code
Refactor in early stage, at lower costs
Same principle for test first or for
code first
28. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesIs it easier?
OOD Principles
Design Patterns Can you TEST it?
Reusability
Extensibility
Maintainability
GOOD Unit Tests
Bad Design Smells with Good Tests
Code at wrong level of abstraction
Class with more responsibilities
Base classes depending on their derivates
Artificial coupling
Feature envy
Polymorphism over if/else or switch/case
Poor coherance
Violates SOLID
Should have used certain design pattern
Test is difficult to understand. It is very large
(more than 20 lines)
Checks on more than ONE thing
Does not run in isolation
Are not following the naming conventions
29. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesHow to do it?
Architecture
App Software
Infrastructure
Vertical Slice
Review
Good UT
Refactor
WHOLE team is doing Good Unit Testing!
Deliver
Functionality
Deliver
Functionality
Deliver
Functionality
Define code coverage
targets