Monday, 13 October 2014

Characterization Tests

Having worked with some truly awful codebases a common problem tends to arise every now and then. You need to make a change within some legacy component that most likely has limited or no automated tests around. This can be a scary process.

There are a few techniques you can use to limit the fear of breaking some legacy code such as sprout methods or classes, however these aren't always optimal in all scenarios.

Another option is characterization tests or "what is this bit of code actually doing?".

  1. Start with a simple test such as "ItWorks".
  2. Run the test - watch it fail.
  3. Using the stacktrace or error reported, write some additional setup.
  4. Run the test - watch it get past the previous error.
  5. Rinse and repeat step 3 - 4 until green.

As part of the first step you should keep the initial test as simple as possible. For example if an input to the system under test (SUT) takes a Foo object, just instantiate Foo. Don't start setting values or fields on Foo. Let the failing test indicate what needs to be set such as a BarException informing you that "bar must be greater than zero" as part of step three.

By now you should have exercised a good chunk of the system under test. However you may need to add additional tests. For example if the code contained an "if" statement, you would need at least two characterization tests. A good way to detect how many tests you need is a code coverage tool, or manually inserting assertions into the SUT to show any missing coverage. Likewise a good manual review is required to fully detect any other tests you may have missed such as boundary cases.

Now the fun can begin. You can refactor like crazy.

Afterwards you should have a nicely refactored component that you can easily extend or modify to add your new feature. You also have a solid suite of tests to prove you've not broken anything. These tests will also document the current behaviour of the system - bugs included.

Examples

Saturday, 4 October 2014

Reinvent the Wheel, Often

We are often never told to reinvent the wheel. In other words, if your job is solve problems within Domain X you shouldn't spend your time recreating or solving problems that fall outside of this domain.

For production code, this I agree with this statement fully. Software development is hard enough. The last thing we want is to waste resources such as time or money on anything we can get away with not implementing. For example, creating your own web framework is a project within itself. All you'll end up with is a slow, buggy, badly implemented version of a web framework that happens to power your domain. Sadly I have been on the receiving end of such decisions.

There are two times however, when reinventing the wheel is a good thing.

  • You can't get the product off the shelf
  • Learning or personal benefit

Chances there is no web framework, database client, caching layer or so forth that you can use is very slim. Some systems become so bespoke or scale to such volumes that recreating such components makes sense. These are the Netflix/Facebook/Google of the world. Most enterprise software will never reach a slither of this sort of scale.

The biggest benefit of recreating well known, solved solutions is the vast amount of learning and knowledge you will obtain. In the past I have re-invented numerous wheels, but each time taken away something of value.

Systems that seem simple at first such as static website generator, turn out to be incredibly complex once you understand the full set of scenarios and edge cases you must handle. The key point here is these wheels, never make it into production for the reasons detailed previously.

In turn you will come to appreciate library and framework developers if you can fight the urge to resist Not Invented Here Syndrome. Their full time project is the delivery of that solution. They have the time to solve all the edge cases you don't. Not to mention the vast amount of other users that will have debugged and improved the solution going forwards. By not reinventing wheels you get as much time as possible to focus on delivering your solution to the domain problem in question, which after all is your job.

Tuesday, 23 September 2014

DDD Validation

Validation within an application (specifically in terms of Domain Driven Design - DDD) can be solved in a variety of ways.

  • A validate method on the entity/value type in question
  • An IsValid property/accessor on the entity/value type in question
  • A separate service could be used

Validate Method

Adding a validate method would work, but the flaw with this approach is that you lack any context of what is happening to the object in question.

Validate Flag

Some sort of flag on the object that denotes whether or not the object is in a valid state is undesirable. Firstly it forces the developer to ensure they check this at the correct time. If the object is invalid, exactly what do you do at this point? This approach is often used with a combination of a validate method that returns the exact error messages.

Validator Services

A separate service seems less than ideal at first when you consider developing a richer domain model, but this solution has numerous benefits. Firstly unlike the two solutions above you always have the context in which validation is being performed. For example, if you are saving a customer you will most likely want to perform different validation to what you would perform when loading up an aggregate.

An additional point to consider is that most validation is not business logic. In other words, checking for null references is not a business concern. Therefore separating this from your domain objects makes a lot of sense. The only logic the domain objects should contain is business logic.

As each service is a separate object, you gain the benefits of the single responsibility principle (SRP). Meaning testing, development and future changes are easier.

Example

The beauty here is that each validator (a simple function in this case) can be used in the correct context. E.g. when the PersonController POST handler is invoked, we use the person saving validator.

Tuesday, 16 September 2014

Developer Diaries

A few weeks back I stumbled across a tweet (which I unfortunately cannot find to give credit to) that talked about the benefit of keeping a dev diary.

At the same time I was reading Getting Things Done (GTD) so I felt inspired to take note of everything related to development that I do during my day to day time - this would satisfy the criteria I had for my GTD system, along with trying to emulate the success the original tweet was referring to.

I don't have a fancy system as such, rather I have a text file that is distributed between the numerous desktops and laptops I have access to. Here the file is synced, so I should always be up to date. Dropbox handles this for me.

Each day I simply make a note of anything I think "I must remember that" or anything that happens to be useful, interesting or new. There is no complex system to this in order to keep in aligned with GTD, new points are simply appended at the bottom of the file. At the end of each week I simply group up related notes. For example, if I've got a few bullet points about databases, I move these to fit under a "Database" heading. This system works for now, though I might have to re-asses this in the future. An example of the file is below.

Example

Databases
    - Points
    - about
    - databases

SOA
   - More
   - points

...

####
Everything below here is a rough note
sorted at the end of a week to fit under
the headings above. If no heading exists
one is created.

The most surprising thing about this, is that even on a dull day I absorb a lot of "stuff" related to development. Equally surprising is how awful my memory is regarding it. If I skim across the document now, I'm alarmed at the stuff that I would have forgotten had I not taken a note. It's quite possible that I would remember some of this information in the long term, but regular skim readings of the diary is proving very helpful.

Thursday, 21 August 2014

Program for Change

We should program for change AKA the Open/Closed Principle. In my opinion, the OCP is one of the lesser respected SOLID principles. One of my biggest, and earliest failures fresh out of university was ignoring this concept.

At the time I was applying YAGNI to some code myself and a couple of other developers were working on. After all agile methodologies promote this concept heavily. This made sense to me. My solution was to solve the problem with the minimal amount of fuss, however in doing so I strongly coupled the code we produced with the direct business requirements.

The requirements stated that we would have three different types expenses. So I promoted that we model these three types of expenses directly. The UI knew about these expenses. The database knew about these expenses. The domain logic knew about these expenses.

Everything worked well for a while. We finished early. We wrote just the code we needed. I was happy. Until the business requirements changed. The three types of expenses became four, then three again, then one was replaced completely. Bugger.

The code was unusable. Everything knew just enough to get by, so when the change came in, everything needed to change. My team was confident this would be OK. After a few hours of analysis, we concluded the code was a train wreck. We'd need to restart from the beginning in order to make the proper changes we wanted. I was pretty gutted, however I learned a very important lesson.

YAGNI is about features, not code.

If I was to complete this feature again, I would still start with the simplest thing that could possibly work. Most likely the code would explicitly know about each type of expense, yet my tests would be wrote in an agnostic manner. I would still apply YAGNI, but at a feature level. In other words, I wouldn't write an expense logger, if all we need to do is validate and calculate expense totals.

During each refactor stage of the TDD cycle I would remove any specific expense knowledge. After a while I would end up with the various parts of the application working with a generic expense algorithm. The tests would drive us towards how the algorithm would work.

The beauty here is that if a new expense was to be introduced, this change would be data driven. We would be able to give this the business for "free".

I still regret this mistake, but this lesson has lived with for some time and has proved to be a valuable experience.