Saturday, 27 December 2014

Pair Programming vs Pairing

I'm a fan of pair programming. I owe a lot of this practice to my improvement early on in my career. I define pair programming as two developers working on a task using one or more machines at the same time.

I have had some excellent pair programming sessions. I can even remember some of them in great detail. Here I went away learning something new, solved a difficult problem, or just generally had a fun time.

On the other hand I've also had some awful experiences, which unfortunately I can still remember. Here my partner wouldn't play the role of the driver or navigator correctly, wouldn't be engaged, or just generally didn't get into the flow of pair programming.

Team's mandating 100% pair programming is bad. Some tasks don't need two developers to be working on them concurrently. Here pairing should be used.

Pairing is two developers working together to solve a task, but doing so separately. During pairing regularly communication, design sessions and feedback should be used. You can even join up to pair program on complex areas. The difference is that unlike pair programming you don't need to have two developers working on the same part of a task at all times. Pair programming and pairing are two very distinct concepts.

The key takeaway here is to know when to use pairing over pair programming and vice versa. Both have their merits and should be applied in the correct context.

Tuesday, 23 December 2014

A Unit is Not Always a Method or Class

Part three of my Three Steps to Code Quality via TDD series. The most important concept when coupled with the previous two points - not every unit will relate to a method or class.


Most introductions into TDD use simple examples. Even the excellent TDD by Example uses a value object in terms of Domain Driven Design. Most introductory articles on the Internet suffer the same fate. While these are great for demonstrations, they don't relate to what most developers need to code on a day to day basis. It's around this point where people proclaim that the benefit of automated testing (even after the fact) is a waste of time.

One of my biggest revelations with TDD was that each unit does not need to equate to a single method or class. For a long time I followed what others did. Each collaborator would be injected and replaced with a test double. Each class would have a corresponding test file. However as I have stated in the introduction, this leads to problems.

We should test units of behaviour, not units of implementation. Given we know we should be using as few dependencies as possible, and we know we should limit visibility, each test should be simple to write. As part of the refactor step if we choose to introduce a new class that is fine. There is no need in most cases to extract this and introduce a test double. Every time this is done we tie the test closer and closer to the implementation details. Every class having a corresponding test file is wrong.

By testing a unit of behaviour we can chop and change the internals of the system under test without breaking anything. This allows the merciless refactoring automated testing advertises as a benefit.

Aren't you describing integration testing?

No. Tests should be isolated as I've documented before, but there is nothing stating they should be isolated from the components they work with. If we isolate at the method or class level we make testing and refactoring much harder. Due to the term "unit" being so closely linked with a class or method, I like the naming convention Google use for their tests - small, medium and large.

Additionally an excellent article from Martin Fowler on the subject of unit testing introduces two new terms, solitary and sociable tests. Neither one style alone works so the type of test you write should be based on context. Unfortunately the industry seems to be fixated on solitary testing.

Sociable Tests

Work great at the aggregate root level. Does the object do what we expect it to? It can use zero or many collaborators behind the scenes but these are implementation details. Here we would limit the use of test doubles as much as possible but still have fast, isolated tests. As generalization - most automated testing should fall into this category as the core domain of your application is likely to have the most amount of logic present.

Solitary Tests

Useful at the adapter or system edge. For example, does the controller invoke the correct application service? We don't care how it works behind the scenes. Anything beyond this service would be a test double. These sort of tests are more closely coupled to implementation details so should be used sparingly.

Doesn't this lead to huge tests?

No, not really. As you will limit implementation details leaking into the public API the use of test doubles will reduce. This will shrink test setup and in most cases improve readability. Worrying about large tests shouldn't be a problem with this style of testing. You will not reduce the amount of tests required, however you will find them to be much more stable and resilient than before.

Sunday, 21 December 2014

Limit the Amount of Dependencies you Use

Part two of my Three Steps to Code Quality via TDD series and ties very closely into step one, limiting the visibility of your classes.


The more dependencies you use the more your tests are coupled to implementation.

Consider the constructor below.

Code like this is common and difficult to work with. Each dependency you inject requires a mock, stub or fake when writing tests. This couples the implementation to the test despite the use of interfaces or abstract base classes.

Every public dependency here increases the resistance for change. If I was to remove the builder and replace with some equivalent code to construct a Bar instance, the test would fail despite being functionally equivalent. This is wrong.

A constructor is part of the public API of an object even though this is not detailed as part of interfaces in languages such as C#/Java. Every collaborator that is provided by a constructor should have a reason for being exposed as part of the the public API.

What Are Good Dependencies?

Good dependencies are things that are out of your control or process such as:

  • Databases (repositories, queries)
  • Web Services
  • Third Parties
  • Strategies (anything that needs to change dynamically)

As part three of the series will detail - isolate your tests from these sorts of dependencies, don't isolate your code from itself.

Doesn't this mean you end up with God classes?

No. As step one detailed - small, well focused classes are a good thing. They just should remain as implementation details.

Tuesday, 16 December 2014

Stop Making Everything Public

Part one of my Three Steps to Code Quality via TDD series.


We always default to public class when creating a new class. Why? The concept of visibility in OO languages appears very early on in programming books, yet more often than not most of the classes we create default to public visibility.

@simonbrown stated that each time you make something public you need to make a donation to charity. In other words we should think more about why the class we are making should be visible to everyone. I really like this idea that the use of the public keyword should be a well thought out decision.

Server side development has a part to play in the lack of concern given to visibility issues. Library or framework developers on the other hand must carefully consider what is part of the public API. Any changes made after are considered breaking and require careful consideration. Yet in the land of server side development this is see as a non issue. This is wrong. If we treat our tests as consumers of our public API, constantly updating them or modifying them should be a warning symbol.

Use internal or private classes for details and public classes for your API. The beauty of this is that TDD drives your public API, which should be fairly stable. Internally however you want the ability to refactor without a suite of tests breaking, otherwise what is the point of writing automated tests?

Implementation details are introduced as part of the refactor step. Ideally they should never be introduced without a passing test in place, as previously the simplest possible thing should have been done.

What Should be Public Then?

  • Application services (use cases) that adapters talk to.
  • Adapters - controllers, console application, presentation layer.
  • Domain concepts - money or customer for example
  • Strategies - things you need to inject, repositories, third parties
Doesn't this lead to god classes?

No. As part of the TDD cycle, when refactoring you can extract implementation details. There is no reason why the public types should suffer.

Doesn't this lead to large tests on the public types?

No. You'll use less test doubles (stubs, mocks, fakes) and in turn reduce setup. For any logic that appears to be painful or common you can introduce domain concepts which can and should be public. The tests can be wrote at this level then. Just find the right test seam.

What is the benefit?

The ability to switch implementation details without breaking public functionality. A whole world of refactoring options are available, inline method, extract method, extract class, inline class, replace with library and so forth. As long as the tests pass, you can be confident.

Sunday, 14 December 2014

Three Steps to Code Quality via TDD

Common complaints and problems that I've both encountered and hear other developers raise when it comes to the practice of Test Driven Development are:
  • Impossible to refactor without all the tests breaking
  • Minor changes require hours of changes to test code
  • Test setup is huge, slow to write and difficult to understand
  • The use of test doubles (mocks, stubs and fakes is confusing)

Over the next three posts I will demonstrate three easy steps that can resolve the problems above. In turn this will allow developers to gain one of the benefits that TDD promises - the ability to refactor your code mercifully in order to improve code quality.

Steps

  1. Stop Making Everything Public
  2. Limit the Amount of Dependencies you Use
  3. A Unit is Not Always a Method or Class

Code quality is a tricky subject and highly subjective, however if you follow the three guidelines above you should have the ability to radically change implementation details and therefore improve code quality when needed.

Saturday, 13 December 2014

Factory Obsession

I have noticed a pattern over the years with developers of which I will refer to as factory obsession. Everything is a factory or builder object. To some, the use of new is banned.


Consider a object that is responsible for some business logic and finally saves the result to a persistent store.

Message here is a value object, however the new can cause an odd fear within developers. Therefore a factory is required. Or is it? How can we test the repository is updated with the new message without reference equality?

An example test in C#, using the Mock framework with this newly introduced factory would look like:

This fear of new is wrong.

  • Instantiating value types is a good thing.
  • Instantiating entities is a good thing.
  • Instantiating services can depend - if the service is expensive we don't want to create lots of instances on a whim.

Here the factory offers nothing but a more strongly coupled solution.

If we ignore the factory the test becomes easier to write. To do this equality should be correctly implemented upon the message value type. I have questioned this in the past but for correct Domain Driven Design (DDD) semantics this is a good thing to follow.

We can take this further though. If we ditch the factory idea all together and replace the repository with a fake implementation we can have an even cleaner test fixture. You would still need equality but the design retains its flexibility.

Factories have their place, like all design patterns, however they should be introduced as part of the refactor step in most cases. Hiding the new keyword is not a goal. The fact that mocking frameworks default to reference equality shouldn't force you to make a more complicated or coupled solution to a problem.