Beruflich Dokumente
Kultur Dokumente
Introduction
Introduction
Statement: The ability to change, is linked to a systems stability in the face of change.
In an Agile World
In and agile world we already manage change. We have many practices:
Unit tests Refactoring Continuous Integration Iterations On-sight customer
Rigidity: Hard to change Fragility: Changes break other parts Immobility: Reuse is not possible
What we need is a set of principals to evaluate a designs ability to change and remain stable.
The Open Closed Principal Single Responsibility Principal Dependency Inversion Principal Liskov Substitution Principal
The Open-Closed Principal "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"
What a blessing it would be if we could open and shut our ears as easily as we open and shut our eyes!
Georg Christoph Lichtenberg (1742 1799)
The Open-Closed Principal "To understand the Open-Closed Principal, we need to understand stability in the face of change."
What is an object?
Objects encapsulate state with behaviour. Classes are more stable because state is encapsulated. Behaviour
State
Less Stable
Stable
The single responsibility principal "A responsibility is a reason to change, a class or module should have one, and only one, reason to change."
None but Buddha himself must take the responsibility of giving out occult secrets...
E. Cobham Brewer 18101897. Dictionary of Phrase and Fable. 1898.
Responsibility is a Reason for change Each responsibility is an axis of change There should never be more than one reason for a class to change Dijkstras SoC: Separation of Concerns This helps us evaluate a classes exposure to change
Counterparty Validator
Counterparty Validator
Trade DB
Counterparty Validator
internal class CounterpartyValidator { public void AssertValid(Trade t) { var sql = "SELECT COUNT(*) FROM counterparty WHERE name=@Name"; using (var conn = CreateOpenConnection()) { var cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@Name", SqlDbType.VarChar); cmd.Parameters["@name"].Value = t.CounterpartyName; var count = (Int32) cmd.ExecuteScalar(); if (count != 1) throw new InvalidCounterpartyException(t.CounterpartyName); } } ...
Single responsibility
Counterparty Validator
Counterparty Source
Trade DB
Whats it job?
Classes should have a single responsibility or job Developers should have that job in mind when they work on a class A developer should easily be able to write a block comment at the top of a class identifying its job. That comment should not have the word AND in it.
Dependency Inversion Principal High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions."
It may be that the old astrologers had the truth exactly reversed, when they believed that the stars controlled the destinies of men. The time may come when men control the destinies of stars.
Arthur C. Clarke (1917 - ), First on the Moon, 1970
Counterparty Validator
High Level (Less Stable)
Counterparty Validator
Counterparty Source
DB
Trade
DBCounterparty Source
DB
Cloud
Inversion of Control
Inversion of Control
IoC is key part of Frameworks Hollywood Principal (Dont call us, We will call you)
Dependency Injection
Specific means of IoC Closely tied to Plug-ins Lookup (service locator) specific plug-in like implementation Containers vs. Factories Suits TDD (Mocks) Number of injections indicate stability
The Liskov substitution principal Functions that reference a base class must be able to use objects of derived classes without knowing it."
There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable. There is another theory which states that this has already happened. Douglas Adams (1952 - 2001)
Rate Calculator
RateCalculator
Trade
Fee
Option
Issues: Exposed to change. We cannot add an adjustment fee type. A convention not a design, developers may not follow.
Trade
Fee
Option
First Attempt
Counterparty Validator <Interface> ITrade Quote
Trade
Fee
Option
Quote
Trade
OCP software entities should be open for extension, but closed for modification
Fee
Option
Stable Dependencies Principal The dependencies between packages in a design should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is."
Module Dependencies
Validation Module
Products Module
Stability Metrics
Ca : Afferent Couplings : The number of classes outside this package that depend upon classes within this package. Ce : Efferent Couplings : The number of classes inside this package that depend upon classes outside this package. I : Instability : (Ce/(Ca+Ce)) : This metric has the range [0,1]. I=0 indicates a maximally stable package. I=1 indicates a maximally instable package.
Module Dependencies
Ca: 0 Ce: 1 I:1 Validation Module
Stable Abstractions Principle Packages that are maximally stable should be maximally abstract. Instable packages should be concrete. The abstraction of a package should be in proportion to its stability."
Measuring Abstraction
A = abstractClasses / totalClasses
Only as approximation as abstract classes have varying numbers of abstract methods, but it is a good guide.
Module Dependencies
Validation Module
Products Module
Strategic Closure
No significant program can be 100% closed Closures cant be complete Closures must be Strategic Stability metrics can indicate hotpots Designer must choose the changes against which her design should be closed
Summary
Judge your design for exposure to change The Open Closed Principal Single Responsibility Principal Dependency Inversion Principal Liskov Substitution Principal Understand stability in the face of change Stable Dependencies Principal Stable Abstractions Principal
References
Open Closed Principal: http://www.objectmentor.com/resources/articles/ocp.pdf Single Responsibility Principal: http://msdn.microsoft.com/en-us/magazine/cc546578.aspx Contract First Development: http://msdn.microsoft.com/en-us/magazine/cc163800.aspx Robert C. Martin http://www.objectmentor.com/omTeam/martin_r.html Martin Fowlers Blog: http://martinfowler.com/bliki/