Wednesday, 31 August 2016

Past Mistakes - ORMs and Bounded Contexts

Sticking with the theme of documenting past mistakes, it's worth expanding a real life scenario where I was unaware of the use of bounded contexts and fully understanding the tools you use.

Ignoring a Bounded Context

A fellow developer set upon a quest to rid numerous projects of duplicated records, which was effectively the active record pattern. This was a huge under taking split across hundreds of thousands of lines of code, over numerous separate projects. Close to completing the task I assisted and finally the shared library containing a single record for each type was complete. Each project now referenced the shared copy. This was versioned as each build was completed.

For a while this worked with no problems. It certainly felt nice to see the reduction in duplicated code. Sadly sometime later myself and another developer made a seemingly innocent change. In terms of relation, the change was as far removed from the production error that we had just been alerted to was. There was no link. It was a different project, in a different path, on a different model. The only commonality was the fact the issue only occurred after the previous deploy.

ORMs and Changes

Several minutes of panic later, the problem was spotted. While the model we had changed had no direct relation, indirectly there was. As each record was loaded by the ORM in question, links and dependencies where also loaded or checked. So were the children's links and dependencies. Finally this would hit the newly changed record. Due to the database changing ahead of the library, numerous other projects now had a runtime error. As we naively believed we were only working within a single project, we deployed the changes within the one project. As the library was shared, all other projects were now vulnerable.

This lack of bounded context, and focusing on removal of duplication was not the only lesson here. This issue painfully highlighted the need and importance to know exactly what your tools are doing, especially when they are hidden behind the scenes. In fact, my use of ORMs other than micro-ORMs is next to non existent at present.

Lessons

  • Use bounded contexts.
  • Favour loose coupling, over reduced duplication.
  • Anything shared must be deployed and tested as a single unit, otherwise remove the shared component.
  • ORMs (or other tools) should be understand and respected.

Wednesday, 24 August 2016

Test Your Live System using Live Service Tests

Traditionally there are three categories of functional tests.

  • Acceptance
  • Integration
  • Unit

This is often refereed to as the testing pyramid. Unit tests form the bulk of your suite, followed by a smaller subset of integration tests. Acceptance tests that cover features should be the tip of your testing strategy, few in number. These are great but there is a missing suite of tests - live service tests.

  • Live Service Tests
  • Acceptance
  • Integration
  • Unit

Live Service Tests.

The role of live service tests (LST) is to test the live system against the production environment and configuration. LST would be fewer in number than acceptance tests. Unlike acceptance tests, these should run constantly. Once a run has completed, kick of a new test run. This will require a dedicated machine or piece of infrastructure, but the value provided is well worth it.

LST should focus on journeys instead of functionality or features. In contrast to acceptance tests a user journey would be the core purpose of the system. For example, a LST suite to cover this blog would ensure the home page loads, an individual post can be loaded, and the archive is accessible. The rest of the site such as comments or social media interactions could be broken, but the core purpose of the system is working. Readers can read and browse the blog. If at any time the tests detect a failure in the core journey there is a big problem.

Why

LST offer the fastest feedback possible due to the fact they are constantly running. It is far more desirable to detect a problem before your users do. Naturally LST offer great protection after deploys. Deployment of new code is one of the times you are more likely to encounter issues, so a suite of tests triggered after a deployment is a natural fit. LST also protect against unplanned events. In my experience, exceeding disk space, DNS failure, third party issues and more have all be detected.

How To

Adding another suite of tests may sound like increased effort but the cost associated with LST is rather low. Sometimes acceptance tests can be run as LST, meaning no extra effort. Care must be taken here if the tests perform anything destructive or anything that interacts with third parties.

Alternatively writing LST is simpler than standard acceptance tests. The same tooling can be used such as Selenium, NUnit and so forth. As the tests themselves focus on journeys rather than functionality, the tests are often less complex to write.

The only difficulty LST introduce is the fact they are executing against the live system. Consider interactions with a third party. Using a real account on the real system may be problematic. One way to get around this issue is to embed test functionality within the system itself. For example you could set up a test account which the tests use. Instead of executing against the third party system, the dummy account is used. Likewise most third parties have test accounts which can be setup and used instead.

LST are a nice compliant to a diagnostic dashboard. If your dash is reporting no issues, and your tests are green, you can be confident the system is operating in a good enough state.

Lessons

  • Functional tests are not enough.
  • Use live service tests to test the real production system.
  • Run live service tests constantly for the earliest feedback possible.
  • Alter production code to introduce test functionality.
  • Make use of test accounts and anything else that third parties may offer.

Thursday, 4 August 2016

Why You Should Do Code Katas

Code katas are simple exercises that are meant to be repeated. They are great for learning a new language or tool. The goal is to learn something, not to complete them. In fact, if you don't finish a kata that is perfectly normal as long as you take something away. The beauty of code katas is they are small enough to be repeated and possibly completed within small time slots.

Takeaways

Beginner or expert there will be something you can learn or take away. Beginners should take away language skills, tooling tricks and more. This is furthered when pair programming.

Experts should be inclined to try new solutions rather than rinse repeat. Instead of solving the problem in the manner you do 9-5, try something completely new. Solving a kata ten times the same way is a waste. Instead solve it once, ten different ways. In short, experiment in code katas, not your production code.

Going Further

Sadly a small minority seem such simple exercises as below them. No one is above doing a kata. Constant improvement or training is essential regardless of level. Katas alone are not enough to support personal development. Experience, reading and practical development are all still required. However when code katas are combined they are an excellent aid.