Skip to main content

From crappy to happy - refactoring untestable code - an introduction

I started testing my code automatically a couple of years in after starting my career. Working in a small company, there weren't really incentives for us to automate testing and we were not following any kind of best practices. Our way of working was to write the code, test it manually and then just Release It ™, preferably on a Friday of course.

I'm sure everyone can relate to this at some point in their career, because this is a lot more common than the Almighty Programming Gods of the Internet make us believe. I find that the amount of companies that actually bother writing tests for their production code is a fraction of the whole universe. I know some friends who work in pretty big companies with big names in the industry and even there the same mindset exists.

Of course, at some point in time our code turned into a big pile of shit. Nobody really knew what was going on and where. We had quantum-level switcheroo that nobody really wanted to touch, and I suspect it is still being maintained today. We were a C# shop, but finding where logic happened was as difficult as untangling the worst kinds of Javascript crap. I'm sure I'm not alone in this one too :)

Young me had never heard of such thing as automating your tests. It was truly a whole new world that I was about to discover, and oh boy what a ride. Before long I was neck deep into the concept of unit testing, integration testing, end-to-end testing, fakes, mocks, stubs and anything in between. It was a really eye-opening time for me and eventually I fell in love with the concept. The simple idea that you can prove your code works and prevent it from breaking in the future is p o w e r f u l. 

And so I went deep into the concepts of TDD and started practicing it, read a lot of books on the subject (the classics), bought an edition of the Gang of Four (Design Patterns: Elements of Reusable Object-Oriented Software), discovered Martin Fowler and Uncle Bob and other great architects and coders... I learned so much in that rush. But then came reality:


It is so much more complicated than having these concepts in a book and reading about them, or trying them in your Useless App #69. Here are some problems I struggled with the first time I tried to apply the concepts:
  1. Lack of understanding. It is one thing to read it in the book and see some minimalist examples in a well isolated harness. It is a different thing when you try to pull apart a 3500 LOC code-behind in a C# Windows Form application. That is an uphill battle.
  2. Testing can become impossible and counter productive if your project is not designed with it in mind. Ours wasn't. 
  3. You can't look at "testing" in isolation. It brings so many concepts attached that before long you will be overwhelmed with so much stuff.
  4. You can't just pick up code that possibly works and start changing it. There is a big deal of risk involved. This requires buy-in from management and understanding that things can go wrong.
  5. Testing is an investment. I can't stress this enough. It is not something you will get rewarded from applying right away, although there are some immediate benefits. It is something that might take months, even years, to give you good returns. It is more noticeable when someone new enters the team, or someone knowledgeable leaves. Tests are good artifacts that someone leaves behind and that can help document their work. They are the good legacy code.

About point 3, here are some examples of things you need to understand if you are starting from 0 as I was:
  • The concept of testing and the multiple kinds of tests you can do
  • What to test? What is a testable behavior?
  • Know a framework that allows for unit testing (NUnit, xUnit, MsTest)
  • What are fakes, mocks and stubs?
  • Learn a framework that allows you to create those fakes, or apply techniques that get you the same behavior.
  • How do you structure your tests? 
  • Study hard about design patterns, know when to apply and which situations. Know the SOLID principles closely.
Did you notice that I did not include anything about the code itself? If you already have code, then you also need to:
  • Introduce meaningful abstractions and layers that create a better separation of concerns. This might mean having to learn MVP, MVC or MVVM, for example.
  • Start identifying and pulling apart dependencies in your code. 
  • Apply dependency injection and inversion of control everywhere.
  • Preferably introduce a dependency injection framework and know how the hell it works. Then learn how you should structure your test code to play well with that DI framework, or ignore it altogether in the tests and do the wiring manually.
  • Check any dependencies that you have (eg: EF) and find out how you can test those or whether you need to abstract them away into their own layers.
These are just some of the most obvious ones. Notice that this list has it all: behavior changes, architectural changes, a myriad of new things to learn about... It truly becomes a mess.

Having said that, learning automated tests is also one of the most wonderful things you can do for your career:
  • After learning all this (and still learning many years later) I can say that I truly boosted my knowledge of how you build software. In wanting to learn "testing" I ended up having to learn a lot more than that. There is no other way around it, but it truly rewards you.
  • In general, you will be more valued as a software developer if you know your way with testing because in the wild it is a rarer skill.
  • When you have practice in writing testable systems, it is natural that you also have practice detecting anti-patterns that break testing. Sometimes the smallest things matter and will break a good test suite - looking at you, DateTime.Now.
  • You will naturally get acquainted with the concepts of CI/CD once you introduce automated tests. This can be a good skill to have.
Putting myself back in the position I was initially, one of the problems I had was that many examples assumed that I was working with new code that I was writing (yes, I know the Refactoring book).

So, I will start a series of posts that try to emulate situations from the real world, introduce some example refactoring and finally explain the theory behind it in simple terms. Sometimes there are simple things we can do while refactoring, but people will shy away from them because they have fancy names, so I think we will start with Dependency Injection!

Comments

Popular posts from this blog

The repository's repository

Ever since I started delving into architecture,  and specifically service oriented architecture, there has been one matter where opinions get divided. Let me state the problem first, and then take a look at both sides of the barricade. Given that your service layer needs to access persistent storage, how do you model that layer? It is almost common knowledge what to do here: use the Repository design pattern. So we look at the pattern and decide that it seems simple enough! Let's implement the shit out of it! Now, let's say that you will use an ORM - here comes trouble. Specifically we're using EF, but we could be talking about NHibernate or really any other. The real divisive theme is this question: should you be using the repository pattern at all when you use an ORM? I'll flat out say it: I don't think you should... except with good reason. So, sharpen your swords, pray to your gods and come with me to fight this war... or maybe stay in the couch? ...

Follow up: improving the Result type from feedback

This post is a follow up on the previous post. It presents an approach on how to return values from a method. I got some great feedback both good and bad from other people, and with that I will present now the updated code taking that feedback into account. Here is the original: And the modified version: Following is some of the most important feedback which led to this. Make it an immutable struct This was a useful one. I can't say that I have ever found a problem with having the Result type as a class, but that is just a matter of scale. The point of this is that now we avoid allocating memory in high usage scenarios. This was a problem of scale, easily solvable. Return a tuple instead of using a dedicated Result type The initial implementation comes from a long time ago, when C# did not have (good) support for tuples and deconstruction wasn't heard of. You would have to deal with the Tuple type, which was a bit of a hassle. I feel it would complicate the ...

C# 2.0 - Partial Types

For those of you interested, i found a very interesting list of features that were introduced in C# in  here . This is a very complete list that contains all the features, and i'm explaining them one by one in this post series. We've talked about  Generics  and  Iterators . Now it's time for some partial types . A partial type  is a type which definition is spread across one or more files. It doesn't have to be in multiple separated files, but can be. This is a very simple concept that can give us many benefits, let's see: If a type is partial, multiple developers can work on every part of it. This allows a more organized way of working and can lead to production improvement.  Winforms , for example, generates a partial class for the form so that the client can separately edit other parts it. This way, a part contains information about the design and the other contains the logic of the form. In fact, this is a very spread pattern across .Net. Ent...