Four small exercises about TDD

823 views
Skip to first unread message

Luca Minudel

unread,
May 21, 2012, 7:27:41 AM5/21/12
to Growing Object-Oriented Software
Hi,

here you can find four small and very focused exercises about unit
tests in TDD together with one of the possible valid solutions . They
are in C# and JavaScript for now (want to port them i.e. to Java and
to Ruby?), each exercise require about 15 minutes.

The main focus here is on common refactoring required in order to make
code testable, and so while doing that improving also the design of
the code.

- https://github.com/lucaminudel/TDDwithMockObjectsAndDesignPrinciples/tree/master/TDDMicroExercises



Luca Minudel
http://www.luca.minudel.it/
http://www.linkedin.com/in/lucaminudel

Reuven Yagel

unread,
May 25, 2012, 2:29:15 AM5/25/12
to growing-object-o...@googlegroups.com
Hello,
Thanks for sharing the examples.
Reading your attached paper, I find that the most interesting finding is
that developers exposed to TDD write more SOLID code than ones exposed to SOLID.
The problem is that we don't have an exact test for SOLIDity.
I think that even the very first code snippet in the paper, disussing OCP & DIP, has a SOLID violation: the default constructor is initalized with a specific implementation.
Reuven

Luca Minudel

unread,
May 25, 2012, 6:21:22 AM5/25/12
to growing-object-o...@googlegroups.com
Hello Reuven


> I think that even the very first code snippet in the paper, disussing OCP & DIP,
> has a SOLID violation: the default constructor is initalized with a specific implementation.

Thanks for your feedback, this is the kind of practical discussion that I was hoping to encourage.
Note that the snippet you mention is from the class Alarm of the TirePressureMonitoringSystem exercise in github.

Can you make an example, mention a change request, that surface the violation of OCP or DIP by the default constructor?
Can you suggest an alternative to the default constructor that avoid that violation?

Reuven Yagel

unread,
May 28, 2012, 12:05:31 PM5/28/12
to growing-object-o...@googlegroups.com
Can you make an example, mention a change request, that surface the violation of OCP or DIP by the default constructor?
Can you suggest an alternative to the default constructor that avoid that violation?
I could not locate the code from the paper in the repository, but I see that at the file Alarm.cs the constructor does get it's dependency as an interface ISensor which seems better.
As a first refactoring: in case a default constructor is needed, it should get it's dependencies through some factory ("new" is a smell)

Luca Minudel

unread,
May 30, 2012, 11:38:19 AM5/30/12
to growing-object-o...@googlegroups.com

The Alarm class has a parametric constructor that accept any working implementation of ISensor so the OCP is satisfied by that parametric constructor (together with all the references in the class that are ISensor and not Sensor).

The violation you mentioned is of the DIP. An example that surface the violation  is when the Sensor class is changed to remove the implementation of the ISensor. This requires also a change to the the Alarm default constructor.
Replacing the new Sensor() with a factory remove the DIP violation, still the change mentioned before requires the same kind changes in the code to be done to the factory now.

In the Alarm class implementation [2] there is also one violation of the OCP that can have a minor impact like the violation you pointed out.

It is interesting to note here that in order to make the initial code [1] testable, the refactoring made to the code [2] removed major violations and this improved the design. At the same time some other minor violation remained there so there are still other  opportunity for improving the design. This show how the practice of TDD with mocks guide devs to remove many common and serious violations but not all the possible violations, still the visible effect on the code-base and on the maintainability and evolvibility of the code support developers in better learning and understanding design in practice.[1] TDDMicroExercises/CSharp/TirePressureMonitoringSystem/Alarm.cs
[2] TDDMicroExercises.ProposedSolution/CSharp/TirePressureMonitoringSystem/Alarm.cs

Luca Minudel

unread,
Oct 18, 2012, 4:53:45 PM10/18/12
to growing-object-o...@googlegroups.com

Now the 4 micro-exercise about TDD and related refactoring are available also in Java, Python and Ruby.
Thanks to whom helped porting them.
Here they are:
- https://github.com/lucaminudel/TDDwithMockObjectsAndDesignPrinciples#readme

Luca
Reply all
Reply to author
Forward
0 new messages