Skip to main content

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.

Comments

Popular posts from this blog

Constant Object Anti Pattern

Most constants are used to remove magic numbers or variables that lack context. A classic example would be code littered with the number 7. What does this refer to exactly? If this was replaced with DaysInWeek or similar, much clarity is provided. You can determine that code performing offsets would be adding days, rather than a mysterious number seven.Sadly a common pattern which uses constants is the use of a single constant file or object. The beauty of constants is clarity, and the obvious fact such variables are fixed. When a constant container is used, constants are simply lumped together. These can grow in size and often become a dumping ground for all values within the application.A disadvantage of this pattern is the actual value is hidden. While a friendly variable name is great, there will come a time where you will want to know the actual value. This forces you to navigate, if only to peek at the value within the constant object. A solution is to simple perform a refactor …

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 breakingMinor changes require hours of changes to test codeTest setup is huge, slow to write and difficult to understandThe 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.StepsStop Making Everything PublicLimit the Amount of Dependencies you Use A Unit is Not Always a Method or ClassCode 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.

DRY vs DAMP in Tests

In the previous post I mentioned that duplication in tests is not always bad. Sometimes duplication becomes a problem. Tests can become large or virtually identically excluding a few lines. Changes to these tests can take a while and increase the maintenance overhead. At this point, DRY violations need to be resolved.SolutionsTest HelpersA common solution is to extract common functionality into setup methods or other helper utilities. While this will remove and reduce duplication this can make tests a bit harder to read as the test is now split amongst unrelated components. There is a limit to how useful such extractions can help as each test may need to do something slightly differently.DAMP - Descriptive and Meaningful PhrasesDescriptive and Meaningful Phrases is the alter ego of DRY. DAMP tests often use the builder pattern to construct the System Under Test. This allows calls to be chained in a fluent API style, similar to the Page Object Pattern. Internally the implementation wil…