Monday, March 31, 2014

Finally some better naming but still with fragile tests

After sleeping on it I've found better names for the examples I've recently posted about (see preious post).

The obstacle was seeing it as an action on a form. Thinking instead on information that needs to be introduced and sent made the names flow much better.

This is the resulting class:
and these are its tests:


Finally, this is how it's used now:



I'm much happier with the naming now.

The tests are still fragile, but by using helpers in the tests the amount of places where the mocks are used gets reduced.

Refactored my clumsy tests

I've reworked the class and the tests I showed in my previous post.

This is the refactored class:

The responsibility to compose an email from the form fields was delegated to the FormEmailComposer collaborator whereas the responsibility of sending the email was delegated to the EmailSender collaborator.

The Redirection collaborator is still in charge of the redirecting responsibility.

Now the Form and the Email class are just value objects.

These are the new tests:

And this is how I'm using the SendingAndRedirectionAction class:

I'm a bit happier with the naming and I also reduced the number of classes.
However I'm still concerned about the tests fragility.

Saturday, March 29, 2014

Five years ago in Garajeando...

This is a post from five years ago that I read today:
Primeros pasos en Java
Hice mi primer programa orientado a objetos!
Es un programa muy tonto para probar, modificando uno que sale en el libro Head first Java.
Tengo tres clases: la que inicia el juego, el juego en sí y el jugador.
En la clase que inicia el juego se crea un objeto Juego y se le pide que inicie la partida. Durante la partida el objeto Juego piensa un número, crea un número de jugadores y les pide que adivinen el número. El diálogo entre los jugadores sale por pantalla.
Es una chorrada pero me lo pasé muy bien. Lo guapo es que si modificas algo en el programa. Sólo tienes que volver a compilar el código de la clase modificada. Todo lo demás sigue funcionando igual.
I have tender feelings reading this again.
I'm also happy that I'm still as passionate about programming as I was then.

First clumsy tests using Phake test doubles framework

Two weeks ago I started learning PHP for my new job at Runroom.

It's my first job as a web developer so I'm still a bit "lost".

Today I tried to write unit test for this class in order to learn how to use Phake test doubles framework:
These were the resulting tests:
Although I like the readability of the tests, I'm not very happy with them because I think they will be too fragile. At the same time I'm not sure how else I could test the behavior of SendingFormByEmailAndRedirecting class...

To give you a bit more of context this is how I'm using it from my code:

Interesting Talk: "The Care and Feeding of C++'s Dragons"

I've just watched this interesting talk by Chandler Carruth about the amazing LLVM clang tools for C++:

Interesting Talk: "Testing and Refactoring Legacy Code"

I've just watched this great live-coding talk by Sandro Mancuso:

Tuesday, March 25, 2014

Using C++11 to split strings without using the boost library

Recently I did the StringCalculator kata in C++.

In order to be able to split a string using several alternative delimiters of one or more characters I used the following function: which is using several functions from the boost library: boost::algorithm::split_regex, boost::regex and boost::join.

Yesterday I managed to use some C++11 features to get the same functionality without using the boost library. This is the resulting code which uses C++11 regex and lambdas: These are the StringUtils tests where you can see how the split and join functions work: You can find all the code in this repository.

Saturday, March 22, 2014

Friday, March 21, 2014

Some resources about scientific software quality

Some time ago I collected these links about scientific software quality and testing for a Barcelona Supercomputing Center winter retreat.

I hope they could be useful for people interested in introducing better practices in scientific software.


Beware, it might be very hard or even useless to directly apply some of industry's good practices in this field.

I think it would be necessary to adapt and refine many of them to cater for the special needs of the scientific software community.

Friday, March 7, 2014

Some resources about Object-Oriented Design, Testability and Legacy Code

These are some resources about Object Oriented Design, testability and legacy code that I collected for the developers at Tecnatom.

I hope that you'll find them useful.

OOP
Testing and Testability
Ports and Adapters
Legacy Code

Some resources about the physical structure of C++ projects

These are several links about the physical structure of C++ projects that I collected for the C++ developers at Tecnatom.

I hope that you'll find them useful.

Some resources about continuous integration for C++ projects on Windows

These are several links about continuous integration for C++ projects on Windows that I collected for the C++ developers at Tecnatom.

I hope that you'll find them useful.

C++ Continuous Integration (CI)

C++ Tools that might be interesting to use from Jenkins or on their own

Thursday, March 6, 2014

Kata: Small step by step TDD kata using C++ and GoogleMock

I've just made a small TDD kata in C++ using GoogleMock.

I committed the existing code after each green and after each refactoring step.
Check the step by step process in GitHub.

Wednesday, March 5, 2014

Extracting reusable C++ templated functions from StringCalculator code

I've recently coded a solution to the StringCalculator kata in C++ using GoogleMock, (you can check its code in GitHub).

In one of the refactoring steps, I extracted the code that filters out not numeric tokens to a separated helper method: filterOutNotNumericTokens:

Once all the kata requirements were satisfied, I refactored this method a little bit more using C++11 copy_if algorithm:

We can use the same technique to refactor the method that ignores numbers greater than 1000, ignoreTooBig, from this:

to this:
 
Notice that we had to make notTooBig a free function in order to pass it to copy_if.

To keep it as a member function we'd need to write this less readable version:

which uses the bind1st and mem_fun functions from functional.

In this case, I think it's probably better to use a lambda:

In the two previous examples we eliminated duplication using the copy_if algorithm.

We can improve the readability of this code introducing a helper templated functions, filter:

Notice that filter works only for vector, copy_if is more general. But this leads to a bit more readable code for filterOutNotNumericTokens and ignoreTooBig:
 
We can also use filter to refactor the getNegatives method:
 
The same procedure can be applied to have a map templated function that uses the transform algorithm which can then be used to refactor the convertToInts method:

This map and filter functions that we'd refactored out from the StringCalculator code are pure functions that are highly reusable and will help us to avoid duplication in another projects.

Monday, March 3, 2014

Kata: Small step by step refactoring kata in C++

I've just made a small refactoring kata in C++.

Some of the tests in the initial code* were very slow because their code had to sleep some time in order for them to pass. Some other tests relied on having certain environment variable set to a given value.

The behavior that checked the environment variable and the one related to time were embedded in the Course class.

Through refactoring this behavior was isolated and extracted so that it could be injected into Course and then two fake classes were created to isolate Course in tests

Check the step by step refactoring in GitHub*.

Thanks to Leo Antoli for passing me the initial code in C#.

---------------------------

* This C++ code example works only for Windows.

Sunday, March 2, 2014

Kata: StringCalculator in C++ step by step using GoogleMock and Boost.Regex

I've just coded the StringCalculator kata in C++ using GoogleMock and Boost.Regex.

I commited it step by step. Check the resulting code in Github.

--------------------------

Update:
I did some refactorings to my solution in order to a) reduce some duplication and b) avoid having to use Boost.Regex.
They're explained, respectively, in the two following posts: