Tuesday, 18 November 2014

Ratcheting

Some tasks in software development are mundane such as formatting and code conventions. Where possible tooling should take away some of this pain, however sometimes you need a developer to take on a task that requires a great deal of time and/or effort to complete. Tooling will only get you so far.

An example of this would be declaring that all projects build and compile with zero warnings. I've tried this in the past after a team retrospective. We had hundreds of errors per project, covering about fifteen projects at the time. Spending several weeks of development time resolving these would not have be fun nor financially viable. However we really wanted to implement this change

Solution

  • I wrote a single test which would execute as part of the build process that asserted the count of the errors per project.
  • Every now and then whenever I had some slack time (10 mins before a meeting, 30 mins at the end of the day etc...) I would open up a project and fix some errors. Then run the test and try and lower the number of errors it was asserting against until I hit the lower limit.
  • Rinse repeat this process and after a while a project would assert that there are no errors.
  • From here on it was impossible for a developer to commit in a change that would raise a warning.
  • The limit would ensure that during this period no new errors were added, increasing the work load.

After a month or so all the projects reported zero warnings. Going forward the test was modified so that new projects added to source control would be checked and have the same tests run against them, meaning no new projects can have a warning count greater than zero.

It turns out this has been documented before - its called Ratcheting. While I didn't know it at the time its nice to have a name to use when describing this technique.

Thursday, 6 November 2014

Dependency Injection (DI) Containers

Strengths

One place for configuration
Rather than scattered through out the system. Most DI containers have some sort of "module" system where you group associated components together.
Scoping
Different types of lifestyle can be achieved. Per request, per thread, singleton and others. Usually other frameworks have the ability to plug into these containers, meaning such features integrate nicely.
Feature rich
Included along with the basic DI components is usually a large amount of additional features which may or may not be needed.

Weaknesses

Heavyweight
Usually in the form of frameworks or libraries. DI is a simple concept, but such containers can make getting to grips with it tremendously difficult.
Config
Configuration can be difficult. Rather than just applying DI you need to learn the tooling. XML configuration has widely fell out of favour, but even code based configurations can be costly to setup.
Runtime errors
Any errors that might have occurred at compile time (in a static language) now become runtime errors. Circular references are easily introduced if you are not careful. Made a mistake during configuration? The system will be out of action. If you're lucky the stacktrace can point you in the right direction, but usually these are vague and/or confusing.
Magic
With the container in charge you lose control of what should be an easy part of your development process. The more convention based configuration you apply, the more chance things can go wrong. Simple changes such as multiple implementations of an interface can prove difficult to configure without breaking previous conventions. Much of the time adding a new class to the system feels risky - you won't know until runtime if you've got it working.

Alternatives

KISS
Keep your dependency wiring at your application root - most likely main. This is my preferred, default approach to begin with.
KISS - Modules
If this configuration starts to get out of hand - use modules. Need to modify how the kitchen is built? Just open up KitchenModule.cs. With direct access to the references of these dependencies you can control scoping. For example you can re-use the same kitchen instance between house instances.
Refacator
As always you can refactor towards an DI container if you feel the need to use one.