Tuesday, 26 April 2016

X% of Configuration is Never Used

Code configuration is essentially for the likes of URLs, credentials or other per deployable settings. Sadly configuration seems to fall into examples where there is simply too much configuration, or the system has so many configuration points the actual code becomes far too complex for its own good.

Too Much Config

I once worked on a system with in excess of six hundred different configuration points. In reality all but a handful of these would ever actually need changing. Most configuration is added to enable anyone to make the change. Ironically if these configuration points do need changing, developers need to do it. The business or non technical individuals will never change settings. In this scenario you would need to actually test all six hundred different combinations of configuration. 1 on, 599 off, 2 on, 598 off and so on - this is not ideal nor realistic.

Configurable Systems are Complex

One of the earliest project mistakes I can remember involved creating a system that could be configured by anyone. A simple task became a several day exploration in failure. Each quarter a minor change to a static ASP page was required. This involved a date and some minor alterations to some financial wording for legal requirements. Instead of simply making the change I started building a custom CMS. A form overlayed the content allowing anyone to make the change and generate the page. It worked a treat technically, except it never saw the light of day. The business would not use it. Numerous individuals required approval before the change could be put live; security, legal, branding and several more. Also using the form still required some implicit knowledge of HTML. At the end of this we threw the prototype away and I made the change in a matter of minutes. My mistake here was building a solution that was not required.

Implementation

When it comes to implementing configuration a common mistake is to rely upon the method of obtaining the value, rather than the value itself. Additionally the use of some form of abstraction is often mistakenly used such as IConfiguration.

The solution is to instead provide the configuration value, not the means of obtaining it. This can be done either via a constructor or directly to the method. This allows the configuration to be provided in different manners such as from a DB or file, with no code changes apart from the composition root. Such solutions are easily testable and open to modification.

Lessons

  • Only add configuration for values that will certainly change between deployable units such as credentials or URLs.
  • Leave everything else where it belongs, either in the source file next to a class, in a method or whatever is easiest. If it needs to change, just make the change when the time comes. Chances are it will never come.
  • If a configuration value is changed, run your automated tests (or a subset) against the deployable unit.
  • A configuration change should be treated as a code change.
  • The business will never change your configuration - that's a technical task.
  • Provide configurations values, not the means of obtaining them.
  • Rely upon convention over configuration as much as possible.

Tuesday, 19 April 2016

Legacy Code is Just Code

Try and define legacy code. Working Effectively With Legacy Code states it is simply code with no tests. This is an almost perfect definition, however it is quite easy to have code that is covered by automated tests, yet is still considered to be legacy. Poor quality, or missing test cases can provide a false sense of security.

Legacy in the Real World

Legacy code is scary to change or work with. Typically it is stuck using an old language or framework which is too expensive to upgrade. Most notable legacy code is often considered old. Developers or teams that no longer exist wrote it and have long since moved on. Hence legacy code is often ignored or over looked by the wise. To be blunt, most developers consider legacy code to be crap.

Just Code

In the end legacy code is just code. It should be treated and given the same amount of respect as your new and shiny solution. In fact legacy code is more than that, it's proven. Unlike clean code you have stagnating in your repository, legacy code has lived and breathed in production. It may be far from ideal, but it works and it does the job. Learn from it and refactor where possible. In cases where refactoring is not possible, use specific techniques for dealing with legacy code (exapanded in future posts).

In reality software developers leave features in their place, not code. Much of the code I have written can/should/will be replaced but the features live on.

The lesson here is to simply treat all code as equal regardless of its status - legacy or not.

Wednesday, 13 April 2016

Dependency Injection for Common Global Dependencies

The use of singletons can often be replaced by simply adjusting scoping of objects. The vast majority of dependencies fit this pattern, with a few exceptions such as DateTime instances, or logging.

Sometimes you just need these dependencies everywhere. You can find yourself passing these dependencies down into the deep depths of your code base. Such changes are often dangerous, time consuming and undesirable.

DateTime

For a while the use of some date/time abstraction was my default approach to handling dates and times. This fake clock or calendar instance when combined with DI at the lowest level does actually work. However if we stop and think about the abstraction it is clearly unnecessary in many cases. Unless your domain is dealing with date and times explicitly, you don't really need an abstraction. In other words, other than the system where the code is running when or why would you provide a different implementation?

The approach taken as part of the example within the Dependency Elimination Principle is my current solution to date/times and DI. This is still dependency injection, except the value is provided, not the method of obtaining the value. This is essentially one of the benefits of functional programming.

Logging

All systems need some form of logging. Commonly either the standard library or a highly rated logging framework is used. The general advice has been to use the logging component directly, rather than providing your own abstraction. Most frameworks already provide interfaces or base classes that make this easy to achieve.

Even so logging suffers the same issue as date/times when it comes to DI. You often need the logging component everywhere, whether it is simply to pass on to other services.

Logging and DI generally do not go well together. Instead simple use the logging instance directly. A good logging framework would be fast, so any automated tests will not notice the difference. Likewise whether logging is configured or not, this should not cause tests to fail. In summary, not every object has to be provided via dependency injection. Loggers being a prime example.

Due to this directly using a logging instance is the preferred approach. Do not rely on DI. However semantic or structured logging does change this suggestion as the use of a domain explicit interface can provide benefits. Semantic logging will be expanded in a future post.

Others

Date/Time and Logging are the two most common global dependencies. The majority of all other dependencies can and probably should be satisfied by traditional DI where possible. As always each dependency should be validated prior to introduction. It may be possible to either eliminate or replace the component in question.

Friday, 8 April 2016

Project Setup Tax

With microservices gaining popularity, one consideration prior to adoption is new project setup. In fact this statement holds true for any new project that you decide to create.

Each new project requires at a minimum

  • Source control - somewhere to actually store the code.
  • A project base - API, executable, library, application etc.
  • Users, accounts and permissions.
  • Build configuration - in order to compile, package and run tests.
  • Deployment and installation - to a production like environment.

Remember this is all before you write a single line of code.

Automating as much of this away does help. Templates, conventions, containers or similar can assist. Still nothing is free. This all requires maintenance regardless of how you choose to optimize the creation of a new project.

When weighing up decisions about a separate project, always factor in the project setup tax. In my experience this tends to take longer than expected. Often it is very easy to forget various project conventions, configuration options or security concerns.

The lesson here is to never underestimate the time and effort required in starting a new project. Always allocate more time. Better yet, question if the introduction of a new project is even required.

Wednesday, 30 March 2016

Pulling the Plug on Date Time Parsing

Date/time logic is hard. Throw in time zones along with daylight saving and it's even harder. Recently a suite of tests that had happily been running for months started failing. There were no code changes and all the tests were somehow related to date/time ranges.

Despite this the production code was functioning as expected. It turns out the API was explicitly setting the locale to use en-GB. However the suite of tests were not.

The fix was simple. Prior to the test fixtures executing, explicitly set the locale. In order to test this assumption and see the tests pass, a temporary change on the development machine was required.

The locale was set on the development machine to another region. In this case setting to en-US was enough to cause the tests to fail. After the code change the tests passed. Any locale can be used as long as the date format differs.

This idea is pretty easy, and is very close to my technique of pulling the plug on automated tests. The test suite can now be run on any machine, even those incorrectly configured and we can be sure the tests will still pass.

Going forward for any date/time tests I will make an active decision to temporarily change my regional settings. With more codebases utilizing the cloud, relying on implicit configuration should be avoided where possible. In fact I would bet a large sum of money that many codebases out there would fail this temporary locale change. Give it a go - pull the plug.