Over my career, I’ve spent a lot of time bringing new engineers up to speed on how to break down problems, how to write better code, and how to write automated tests. What I’ve come to find out is that there are a ton of resources on how to do each of these things individually, but not very many that brings all of these concepts together in one place. The purpose of this series is to bring all of these ideas together as we solve the Mars Rover kata.
For new engineers, this kata has about the right amount of complexity to explore these various concepts and help identify things to improve on. As a team lead at SentryOne, I’ve onboard interns and our associate engineers using this kata as a launch point to teach them the tooling and processes we use. In addition, this kata serves as a method to evaluate their current skills so we can help round out a solid foundation for their career.
By the end of this series, you will have a better understanding of how to break down a problem into small, deliverable pieces while being able to write tests around new functionality. Even though there will be a lot of code to look at, none of these concepts are technology-specific so I challenge you to follow along with your tech stack of choice and see how you would implement some of these concepts in that stack.
On the topic of technologies, I’ll be using the following for my implementation of this kata. As long as you find the relevant tool for your technology stack, you should be able to follow along!
In the last post, we took a look at the problem description for Mars Rover and developed a set of concepts for the problem. From these concepts, we were able to develop common terminology and determine the relationships between the concepts. In this post, I’m going to show how I think about software design in general and how to apply them when modeling in code.
As a note, I’ll be showing code in both C# (for Object-Oriented approaches) and F# (for Functional Programming approaches). Once again, these concepts are fundamentals, but depending on your technology stack, the implementations will vary.
When I’m designing my models, my end goal is to produce software that captures the problem at hand using the same terms that the business uses. By striving for this goal, I can have richer conversations with my stakeholders when I run into interesting interactions of various business rules and can speak to them using the right terminology. In addition to capturing the problem, I will focus on designing my models in such a way that a developer can’t violate a business rule because the code won’t compile. At this point, I would have made illegal states unrepresentable in my code.
I first came across this term while reading Scott Wlashcin‘s work on the terrific F# for Fun and Profit website and it immediately resonated with me. I’ve definitely been bitten before working in a codebase where I wrote some code that compiled but blew up in my face during runtime because the parameter I passed in wasn’t valid for the method I was calling. Wouldn’t it be nice if the compiler told me while I was writing the code that what I was doing wouldn’t work? By thinking a bit more about the models being used and what some of their properties are, we can make this goal achievable.
If the type has a finite number of valid values, then we can remove error conditions by defining the type to only be one of those possible options. For those from an Object-Oriented background, enums are a great example of modeling these types as you can explicitly set a label for the different values. For those from a Functional background, sum types are a great way to model these choices.
Some examples of such a type include the states in the U.S., the suits for a deck of playing cards, or the months in a year.
For other types, however, there are so many possible valid values that it’s impossible to list all of them. For example, if we were looking at valid house numbers for an address, any positive integer would be valid so good luck on defining every positive number as an enum or sum type.
In these cases, I will leverage built-in primitives to model the concept at first. So in the case of HouseNumber, an integer might be a good enough spot to start. However, if I then find myself writing code that can work on integers, but shouldn’t work on HouseNumbers, then I might wrap a stronger type around the integer (see below).
// If the value isn't a major component of the design, we can use a primitive typeinthouseNumber;// However, if the type is a major concept to the domain at hand, // it makes sense to lift it to its own typepublicclassHouseNumber{publicintValue{get;}publicStreetNumber(intinput){// validation logicValue=input;}}// The difference between the two approaches is that in the first case, this would workinthouseNumber=400;Math.Sqrt(houseNumber);// But this wouldn'tvarhouseNumber=newHouseNumber(400);Math.Sqrt(houseNumber);// fails to compile with "cannot convert from HouseNumber to double"
// If the value isn't a major component of the design, we can use a primitive typelethouseNumber:int;// However, if the type is a major concept to the domain at hand, // it makes sense to lift it to its own type (single case sum type)typeHouseNumber=HouseNumberofint// The difference between the two approaches is that in the first case, this would worklethouseNumber=400;Math.Sqrt(houseNumber);// But this wouldn'tlethouseNumber=HouseNumber400Math.Sqrt(houseNumber);// fails to compile with // "This expression was expected to have type 'float' but here has type 'HouseNumber'"
As the saying goes, large programs are built by composing a bunch of smaller programs, and types are no different. As we begin to model more complicated types, it’s natural to start thinking about types being composed of other types. For these types, we’ll leverage either objects (if following OO) or records (if following FP).
One way you can determine if you’re needing a composite type like this is if you find yourself using the word and or has when describing the type, then it’s a composition. For example:
An Address has a HouseNumber, it has a StreetName, it has a State.
An Address consists of a HouseNumber and a StreetName and a State
Now that we’ve talked about some different modeling techniques, let’s see how we can apply those rules as we start to model Mars Rover. From the previous post, we were able to derive the following concepts and relationships:
A Rover has a Location and an Orientation
Orientation is the Direction that a Rover is facing
Location is the coordinates that the Rover is located at
A Command is something that a Rover receives from the User
A Direction can be North, East, South, or West
A Command can be Move Forward, Move Backward, Turn Left, Turn Right, or Quit
Yielding the following graph
Domain model relationships where Rover has a Location and an Orientation. Orientation is a Direction and Command is not related to anything.
Given the above rules, we can start taking a look at how to model these in code! We’ll first start with the models that don’t have a dependency, and then build up from there
From the above requirements, Direction can only be one of four possible values (North, East, South, West). So based on that, it looks like we can leverage the first rule and model Direction like so:
From the above requirements, Command can only be one of five possible values (MoveForward, MoveBackward, TurnLeft, TurnRight, and Quit). Based on that, we can once again leverage the first rule and model Command like so:
After talking more with our Subject Matter Expert, a Location is the Coordinate where the Rover is located.
Aha! A new concept!
When we ask additional questions, we find out that a Coordinate refers to the Cartesian Coordinate System and for the problem we’re solving, we can assume that a Coordinate represents two numbers where the first number represents the location from the x-axis and the second number represents the location from the y-axis.
With this new information, our mental model has changed to be the following
Domain model relationships where Rover has a Location and an Orientation. Location is a Coordinate where Coordinate has an X and Y value. Orientation is a Direction and Command is not related to anything.
Going into further discussion, we find out that both X and Y will be whole numbers for our emulation and that they can be negative. Based on these properties, it sounds like X and Y can be modeled as integers and therefore fall under the second rule.
Given that a Coordinate has to have both an X and Y value, it sounds like Coordinate falls under the third rule and that this concept is a composition of X and Y.
From the above requirements, it seems like Orientation is what we call the Direction that the Rover is facing. Based on that, this sounds like a property that Rover would have.
Now that we have both the Direction and Coordinate concepts designed, we can start designing Rover. From the requirements, it looks like Rover is a combination of Direction (known as Orientation) and a Coordinate (known as a Location). Based on that, Rover falls under the third rule and looks like the following.
In this post, we implemented the basic types needed to solve the Mars Rover kata! We first started by taking a look at the concepts identified earlier and thought about the characteristics of the type which helped guide us to build software that both uses the terms of the problem domain and also prevents us from creating errors by making illegal states unrepresentable. In the next post, we’ll start adding functionality to our application.
I’ve found that interviewing for a new job can be super stressful and it reminds me of speed dating, “Let’s get to know each other over the next few hours to see if this relationship can work”. With such a short time window, there’s not much time to ask “fluff” questions like “if you were a tree, what kind of tree would you be”. Instead, I’m more likely to ask some of the following questions to get a better understanding of what I’m about to step into. If the company can’t answer some of these questions, it’s not a deal breaker, but can be a red flag about this place.
With that being said, I hope these questions help you during your interviewing process!
What was a stumbling block for over the past year?
What does your ideal customer look like?
What is your business model (i.e. how does make its money)?
Any thoughts of expanding to other markets (such as, if the company sells a particular type of medical device, are there any thoughts of making other devices or add-ons for the main device)? If not, why?
Who would you say are your biggest competitors? What differentiates you from them?
How many vacation days? How many sick days? Are they from the same bucket (i.e PTO) or different buckets?
Is there a 401(K)? If so, what’s the vesting period (i.e. how long until the employer contributions become yours)? What’s the employer contribution?
Is there a training budget? If so, is it per person, per team, per department? What do you typical training expenses look like (books, videos, conferences, or in person training)?
When does open enrollment begin for insurance? Am I covered on day one or is there a waiting period?
So the code is pretty straight forward, I have a PermissionChecker whose job is to use the IModuleDisabler to turn off certain modules depending upon the user permissions. Pretty straightforward implementation.
Now that the solution is fleshed out, it’s time to write some tests around this. When it comes to testing classes that have dependencies on non-trivial classes, I use NSubstitute, a mocking tool, to create mock versions of those dependencies. In this case, NSubstitute allows me to test how the IModuleDisabler is being used by the PermissionsChecker.
For example, let’s say that I wanted to test how the PermissionChecker interacts with the IModuleDisabler when the user has a partial access, I’d write a test that looks like the following:
In the above test, our assertion step is to check if the mockDisabler received a single call to the DisableReportModule. If it didn’t receive a call, then the test fails. We can write similar tests for the different modules that should be disabled for the partial rights permission and follow a similar pattern for the full rights permission.
However, things get a bit more interesting when we’re testing what happens if the user is an admin. If we follow the same pattern, we’d end up with a test that looks like this:
The issue arises when we add a new module to be disabled which forces the IModuleDisabler to implement a new method. In that case, you need to remember to update this test to also check that the new method wasn’t being called. If you forget, this test would still pass, but it’d pass for the wrong reason.
To help illustrate, let’s say that another method, DisableImportModule, has been added to the IModuleDisabler interface. In addition, we also need to make sure that this is called when users have partial access, but should not be called for users who are admins or users who have full access.
To fulfill those requirements, we modify the PermissionChecker as so:
At this point, we’d write another test for when the a user has partial access, the import module should be disabled. However, it’s very unlikely that we’d remember to update the test for the admin. Remember, for the admin, we’re checking that it received no calls to any disable methods and the way we’re doing that is by checking each method individually.
[Test]publicvoidAnd_the_user_has_admin_permissions_then_the_disabler_is_not_used(){// ArrangevarpermissionChecker=newPermissionChecker();varmockDisabler=Substitute.For();varuser=newUser{IsAdmin=true};// ActpermissionChecker.CheckPermissions(mockDisabler,user);// AssertmockDisabler.DidNotReceive().DisableSystemAdminModule();mockDisabler.DidNotReceive().DisableReportModule();mockDisabler.DidNotReceive().DisableUserManagementModule();// Need to add check for DidNotReceive().DisableImportModule();}
There’s got to be a better way. After some digging around, I found that any NSubstitute mock, has a ReceivedCalls method that returns all calls that the mock received. With this new knowledge, we can refactor the previous test with the following:
This solution is much better because if we add more modules, this test is still checking to make sure that admin users do not have any modules disabled.
When using a NSubstitute mock and you need to make sure that it received no calls to any methods or properties, you can using NSubstitute’s ReceivedCalls in conjunction with CollectionAssert.IsEmpty to ensure that the substitute was not called.
In this post, I’m going to solve a logic puzzle using C# and F#. First, I’ll define the problem being solved and what our restrictions are. Next, I’ll show how I’d break down the problem and write an easy-to-read, extendable solution using idiomatic C#. Afterwards, I’ll solve the same problem and write an easy-to-read, extendable solution writing in idiomatic F#. Finally, we’ll compare the two solutions and see why the F# solution is the better solution.
For this problem, I’m going to write a constraint solver (thanks to Geoff Mazeroff for the inspiration).
If you’re not familiar with the concept, a constraint is simply some rule that must be followed (such as all numbers must start with a 4). So a constraint solver is something that takes all the constraints and a source of inputs and returns all values that fit all the constraints.
With that being said, our source will be a list of positive integers and our constraints are the following:
4 digits long (so 1000 – 9999)
Must be even (so 1000, 1002, 1004, etc…)
The first digit must match the last digit (2002, 2012, 2022, etc…)
To further restrict solutions, all code will be production ready. This includes handling error conditions (like input being null), being maintainable (easily adding more constraints) and easy to read.
To quickly summarize, we need to find a robust, maintainable, and readable solution to help us find all 4 digit number that are even and that the first and last digit are equal.
For the C# solution, I’m going to need a class for every constraint, a class to execute all constraints against a source (positive integers) and a runner that ties all the pieces together.
Starting with the smaller blocks and building up, I’m going to start with the constraint classes. Each constraint is going to take a single number and will return true if the number follows the constraint, false otherwise.
With that being said, I’d first implement the constraint that all numbers must be 4 digits long
At this point, I have the constraints written, but I need them to follow a general contract so that the Constraint Solver (about to be defined) can take a list of these constraints. I’ll introduce an interface, IConstraint and update my classes to implement that interface.
So now I have the constraints defined and they’re now implementing a uniform interface, I can now create the constraint solver. This class is responsible for taking the list of numbers and the list of constraints and then returning a list of numbers that follow all constraints.
This solution is easily extendable because if we need to add another constraint, we just add another class that implements the IConstraint interface and change the Main method to add an instance of the new constraint to the list of constraints.
Now that we have the C# solution, let’s take a look at how I would solve the problem using F#.
Similar to the C# solution, I’m going to create a function for every constraint, a function to execute all constraints against a source (positive integers) and a runner that ties all the pieces together.
Also similar to the C# solution, I’m going to start with creating the constraints and then work on the constraint solver function.
First, I’d implement that the number must be four digits long constraint.
At this stage in the C# solution, I had to create an interface, IConstraint, so that the constraint solver could take a list of constraints. What’s cool with F# is that I don’t have to define the interface. The F# type inference is saying that each of these functions are taking the same input (some generic `a) and returning a bool, so I can add all of them to the list. This is pretty convenient since I don’t have to worry about this piece of plumbing.
Now that the different constraints are defined, I’d go ahead and write the last function that takes a list of constraints and a list of numbers and returns the numbers that the constraints fit. (Confused by this function? Take a look at Implementing your own version of # List.Filter)
Now that we have both solutions written up, let’s compare and see which solution is better.
First, the same design was used for both solutions. I decided to use this design for both because it’s flexible enough that we could add new constraints if needed (such as, the 2nd digit must be odd). As an example, for the C# solution, I’d create a new class that implemented IConstraint and then update the runner to use the new class. For the F# solution, I’d create a new function and update the runner. So, I’d think it’s safe to say that both solutions score about the same from a maintainability and extendability point of view.
From an implementation perspective, both solutions are production ready since the code handles possible error conditions (C# with null checks in the ConstraintSolver class, F# with none because it doesn’t support null). In addition to being robust, both solutions are readable by using ample whitespace and having all variables, classes, and interfaces clearly described.
With that being said, this is where the similarities end. When we look at how much code was written to solve the problem, we have a stark difference. For the C# solution, I ended up with 48 lines of code (omitting blank lines), however, for the F# solution, it only took 19. Now you could argue that I could have written the C# solution in fewer lines of code by removing curly braces around one line statements or ignoring null inputs. However, this would lead the code to be less robust.
Another difference between the F# solution and C# is that I was able to focus on the solution without having to wire up an interface. You’ll often hear developers talk about the how little plumbing you need for F# to “just work” and this small example demonstrates that point.
Another difference (albeit subtle) is that the F# solution is that I can use the findValidNumbers function with any generic list of values and any generic list of functions that take the generic type and return true/false.
In other words, if I had another constraint problem using strings, I’d still implement the different constraints, but I could use the same findValidNumbers function. At that point, however, I’d probably rename it to findValidValues to signify the generic solution.
What’s awesome about this is that I didn’t have to do any more work to have a generic solution, F# did that for me. To be fair, the C# solution can easily be made generic, but that would have to be a conscious design decision and I think that’s a downside.
In this post, we took a look at solving a number constraint problem by using idiomatic C# and F#. Even though both solutions are easy to read and easy to extend, the F# version was less than 1/2 the size of the C# solution. In addition, I didn’t have to do any plumbing for the F# version, but had to do some for the C# solution, and to top it off, the F# solution is generically solved, whereas the C# solution is not.
As I’ve been thinking more about F#, I began to wonder how certain methods in the F# stack work, so I decided to implement F#’s List.filter method.
For those of you who aren’t familiar, List.Filter takes a function that returns true or false and a list of values. The result of the call is all values that fulfill the fuction.
For example, if we wanted to keep just the even numbers in our list, then the following would accomplish that goal.
Now that we have the signature, let’s add some logic to match on the list of values. When working with lists, there are two possibilities, an empty list and a non-empty list. Let’s first explore the empty list option.
In the case of an empty list of values, then it doesn’t matter what the func parameter does, there are no possible results, so we should return an empty list for the result.
Now that we’ve handled the empty list, let’s explore the non-empty list scenario. In this branch, the list must have a head and a tail, so we can deconstruct the list to follow that pattern.
letfilter(func:int->bool)(values:intList)=matchvalueswith|[]->[]|head::tail->// what goes here?
Now that we’ve deconstructed the list, we can now use the func parameter with the head element. If the value satisfies the func parameter, then we want to add the head element to the list of results and continue processing the rest of the list. To do that, we can use recursion to call back into filter with the same func parameter and the rest of the list:
At this point, we need to handle the case where the head element does not satisfy the func parameter. In this case, we should not add the element to the list of results and we should let filter continue the work
By handling the base case first (an empty list), filter can focus on the current element in the list (head) and then recurse to process the rest of the list. This solution works, but we can make this better by removing the type annotations. Interestingly enough, we don’t care if we’re working with integers, strings, or whatever. Just as long as the function takes some type and returns bool and the list of values matches the same type as the func parameter, it works. So then we end up with the following:
In general, when working with lists, I tend to start by matching the list with either an empty list or non-empty. From there, I’ve got my base case, so I can focus on the implementation for the first element. After performing the work for the first element, I can then recurse to the next element.
There is nothing new in the world except the history you do not know.
– Harry S. Truman
The more experience I gain problem solving, the more this holds true. For this post, I’m going to first discuss the problem that I was trying to solve. Next, I’ll show what my first solution was, followed by the shortcomings of this solution. Thirdly, we’ll iterate over a better solution to the problem. This in turn, will provide the motivation for what the Chain of Responsibility is and how to implement. Finally, I’ll wrap up with what the benefits were of using this design. .
As part of the process of installing our software, there are scripts that will update the database from it’s current version to the latest version. As it stands, it needs to be able to upgrade a database from any version to the current version.
The first thing that comes to me is that I need to apply database scripts in a sequential way. For example, if the database’s current version is 1.0 and the latest version is 3.0, it would need to apply the script to upgrade the database from 1.0 to 2.0 and then apply the script to upgrade the database from 2.0 to 3.0.
For the first implementation, there were only two versions, 1.0 and 2.0. Since I didn’t want to build in a lot of functionality if it wasn’t needed yet, I created a helper method that returns the correct updater for a given version. In the below code, if the version does not exist, I assume the database does not exist and return the class that will create the database. Otherwise if the version is 1.0, I return a class that is responsible for the upgrading a database from 1.0 to 2.0. If the version is 2.0, I return a class that doesn’t do anything (i.e. there’s no upgrades to be done).
publicIDatabaseUpdaterGetDatabaseUpdater(stringversion){if(string.IsNullOrWhiteSpace(version))returnnewDatabaseCreator();if(version=="1.0")returnnewDatabase100To200Updater();if(version=="2.0")returnnewCurrentVersionUpdater();thrownewArgumentException("The version "+version+" is not supported for database upgrades.");}
This solution worked well when there only three possible actions (create a new database, apply the single script, or do nothing). However, we are now going to be shipping version 3.0 and there will need to be a new class that is responsible for upgrading the 2.0 to 3.0. In order to add this functionality, I’d have to do the following:
Create the Database200To300Updater class that contained the logic for updating the database from 2.0 to 3.0.
Modify the Database100To200Updater class to also use the Database200To300Updater in order to perform the next part of the upgrade.
Add additional logic to the above method so that if the database is 2.0 to return the Database200To300Updater class.
After making the modifications, the method now looks like:
publicIDatabaseUpdaterGetDatabaseUpdater(stringversion){if(string.IsNullOrWhiteSpace(version))returnnewDatabaseCreator();if(version=="1.0")returnnewDatabase100To200Updater(newDatabase200To300Updater());if(version=="2.0")returnnewDatabase200To300Updater();if(version=="3.0")returnnewCurrentVersionUpdater();thrownewArgumentException("The version "+version+" is not supported for database upgrades.");}
So far, so good, we now have the logic to be able to apply scripts in order, however, now that we’ve added version 3.0, I start to wonder what I would do if we added more versions? After some thought, it would look identical to the previous steps (see below for what would happen if we added version 4.0).
publicIDatabaseUpdaterGetDatabaseUpdater(stringversion){if(string.IsNullOrWhiteSpace(version))returnnewDatabaseCreator();if(version=="1.0")returnnewDatabase100To200Updater(newDatabase200To300Updater(newDatabase300To400Updater()));if(version=="2.0")returnnewDatabase200To300Updater(newDatabase300To400Updater());if(version=="3.0")returnnewDatabase300To400Updater();if(version=="4.0")returnnewCurrentVersionUpdater();thrownewArgumentException("The version "+version+" is not supported for database upgrades.");}
If we create some variables to hold onto these classes, and reorder the if statements, we can write this helper method as:
publicIDatabaseUpdaterGetDatabaseUpdater(stringversion){if(string.IsNullOrWhiteSpace(version))returnnewDatabaseCreator();if(version=="4.0")returnnewCurrentVersionUpdater();vardatabase300Updater=newDatabase300To400Updater();vardatabase200Updater=newDatabase200To300Updater(database300To400Updater);vardatabase100Updater=newDatabase100To200Updater(database200To300Updater);if(version=="1.0")returndatabase100Updater;if(version=="2.0")returnnewdatabase200Updater;if(version=="3.0")returnnewdatabase300Updater;thrownewArgumentException("The version "+version+" is not supported for database upgrades.");}
What I find interesting in this design is that I’ve now chained these updater classes together so that if the version 1.0 is returned, it will also use the 2.0 updater, which in turn calls the 3.0 updater. It was at this point, that I remembered a design pattern that followed this structure.
In this design pattern, you essentially have Handlers (in my case updaters) that check to see if they can handle the request. If so, they do and that stops the chain. However, if they can’t handle the request, they pass it to their Successor (which was also a Handler) to handle the request. The design pattern I was thinking about is the Chain of Responsibility pattern.
In order to implement this pattern, you need to have an IHandler interface that exposes a Handle method and either a method or property to set the Successor. The method is the action to take (in our case Update) and the Successor represents the next Handler in the chain if the request could not be handled. The second component is referred to as ConcreteHandlers and they are just the implementors of the interface. One way to implement this is like the following:
publicinterfaceIHandler{IHandlerSuccessor{get;set;}voidUpdate(intversion);}publicclassConcreteHandlerA:IHandler{publicIHandlerSuccessor{get;set;}publicvoidUpdate(intversion){if(CanTheRequestBeHandled){// handle the request}else{Successor.Update(version);}}}
The main difference between the pattern and what I need is that instead of doing if (canHandle)/else call Successor, what I’m really looking for is to run the upgrade script if the version we’re upgrading to is higher than our current version and then always call the successor. Given this change, here’s what that new implementation looks like:
publicclassConcreteHandlerA:IHandler{publicSuccessor{get;set;}publicvoidUpdate(intversion){if(CanTheRequestBeHandled){// handle the request}Successor.Update(version);}}
Now that I know the pattern to use and how it works, I need to update the IDatabaseUpdater interface to follow the IHandler interface. Next, I will need to modify the concrete handlers to use the new interface correctly.
Second, we will need to update our concrete handlers to implement the interface correctly and to update their UpdateMethod to follow the design. In my case, the concrete handlers perform similar logic, so one of the classes is used for an example.
publicclassDatabase100To200Updater:IDatabaseUpdater{privateDatabase200To300Updater_successor;publicDatabase100To200Updater(Database200To300Updatersuccessor){if(successor==null)thrownewArgumentNullException("successor");_successor=successor;}publicvoidUpdate(){Console.WriteLine("Updating the database to version 2.0");_successor.Update();}}
publicclassDatabase100To200Updater:IDatabaseUpdateHandler{publicvoidUpdate(intversion){if(version>=2)Console.WriteLine("Updating the database to version 2.0");if(Successor!=null)Successor.Update(version);}publicIDatabaseUpdateHandlerSuccessor{get;set;}}
What I really like about the chain of responsibility pattern is that I was able to connect my upgrade classes together in a consistent fashion. Another reason why I like this pattern is that it forces me to have the logic to determine whether I should run the update or not inside the individual classes instead of the helper method. This produces more readable code which then lends itself to easier maintainability.
During this past week, I was working with our intern and showing him some cool debugging tricks when I came across a massive method. I gave him 30 seconds to look at it and tell me what he thought the method was doing. After a while, he was able to figure it out, but the fact that it wasn’t easy discernible was enough to give pause.
The lesson here is that if you can’t determine what the method is doing easily, then it’s probably doing way too much (violating the Single Responsibility Principle) and needs to be broken into more easily readable pieces.
To demonstrate what I mean, I wrote a program that inserts Messages into a database. A Message contains a description, the number (for identification when troubleshooting) and the module. We would have issues where different messages would have the same number which would cause confusion when troubleshooting errors.
In the program I wrote, the user provides the message and what module the message belongs to and the program automatically generates the message number and inserts the message into the database.
For brevity’s sake, shown below is the logic for determining what the next message number should be.
publicintGetNextAlertAndErrorModuleNumber(stringmodule){if(String.IsNullOrEmpty(module))thrownewArgumentException("module cannot be null or empty");if(_connection==null)_connection=CreateConnection();varresults=newList<int>();_connection.Open();varcmd=newSqlCommand("dbo.GetAlertData",_connection);cmd.CommandType=CommandType.StoredProcedure;varreader=cmd.ExecuteReader();while(reader.Read()){if(!reader["ALERT_ID_NUMBER"].ToString().Contains(module))continue;varpieces=reader["ALERT_ID_NUMBER"].ToString().Split(‘‘);results.Add(Int32.Parse(pieces[1]));}if(reader!=null)reader.Close();cmd=newSqlCommand("dbo.GetErrorData";,_connection);cmd.CommandType=CommandType.StoredProcedure;reader=cmd.ExecuteReader();while(reader.Read()){if(!reader["ERROR_ID_NUMBER"].ToString().Contains(module))continue;varpieces=reader["ERROR_ID_NUMBER"].ToString().Split(‘‘);results.Add(Int32.Parse(pieces[1]));}if(reader!=null)reader.Close();if(_connection!=null)_connection.Close();returnresults.Max()+1;}
The method itself isn’t complex, just calling some stored procedures, parsing the output and adding the number to a list. However, it’s not abundantly clear what the purpose of the calling the stored procedures.
First, it looks like we’re reading the alerts error numbers from a stored procedure call, why don’t we extract that logic out to a helper method and have the public method call the helper?
By doing this, we fix two issues at once. First, we’ve given a name to the process of reading the alerts which in turns allows us to quickly understand what the public method should be doing (i.e. improved readability).
Second, it allows us for easier debugging because we now have smaller components. For example, let’s say that we were getting the wrong value. In the first implementation, we would have to put breakpoints in different areas trying to determine which part was broken. However, in the new form, we can check to see if ReadAlerts is behaving correctly. If it isn’t, we now know the bug has to be in that method, otherwise, it’s in the rest.
For the next step, you may have noticed that we can repeat the same refactoring trick again, except this time, we can extract the logic for reading the errors into a helper method.
publicintGetNextAlertAndErrorModuleNumber(stringmodule){if(String.IsNullOrEmpty(module))thrownewArgumentException("module cannot be null or empty");if(_connection==null)_connection=CreateConnection();_connection.Open();varresults=newList<int>();results.AddRange(ReadAlerts(module.ToUpper()));results.AddRange(ReadErrors(module.ToUpper()));if(_connection!=null)_connection.Close();returnresults.Max()+1;}privateList<int>ReadAlerts(stringmodule){varresults=newList<int>();varcmd=newSqlCommand("dbo.GetAlertData",_connection);cmd.CommandType=CommandType.StoredProcedure;varreader=cmd.ExecuteReader();while(reader.Read()){if(!reader["ALERT_ID_NUMBER"].ToString().Contains(module))continue;varpieces=reader["ALERT_ID_NUMBER"].ToString().Split(‘‘);results.Add(Int32.Parse(pieces[1]));}if(reader!=null)reader.Close();returnresults;}privateList<int>ReadErrors(stringmodule){varresults=newList<int>();varcmd=newSqlCommand("dbo.GetErrorData",_connection);cmd.CommandType=CommandType.StoredProcedure;varreader=cmd.ExecuteReader();while(reader.Read()){if(!reader["ERROR_ID_NUMBER"].ToString().Contains(module))continue;varpieces=reader["ERROR_ID_NUMBER"].ToString().Split(‘‘);results.Add(Int32.Parse(pieces[1]));}if(reader!=null)reader.Close();returnresults;}
After the changes, anyone who looks at the public API can easily see that it’s reading from both Alerts and Errors. This is really powerful because now you can communicate with non-technical people about requirements and what the code is doing.
Let’s say that in the future, the requirements change and this conversation plays out:
Alice (QA, finding an error) – Hey Bob, I was running one of our test plans and it looks like that we’re getting the wrong message number if we’re trying to add a new message and there are warning messages in the database. We are including the warning table when figuring that out, right?
Bob (Engineer, finding the root cause) – Hey you’re right, it looks like we’re only using the alerts and error tables when calculating the next number. Why don’t we write up a ticket for that and get a fix in?
The key point is that no matter how large a method is, there always have to be steps being performed in some order (by definition of an algorithm) and this is the key to refactoring larger methods into more maintainable pieces of code. The trick is determining what those steps are and making decisions on whether to make helper methods or helper classes.
If those steps become complicated, then they should be broken out into helper methods. As time progresses and those helper methods start to become more complicated, then those helper methods should in turn become classes of their own.
publicIStrategyGetStrategy(Projectproject,boolisAffected){vartype=project.Type;if(type==ProjectType.A&&isAffected)returnnewProjectAIsAffectedStrategy();if(type==ProjectType.B)returnnewProjectBStrategy();// Similar if statements}
At first glance, it looks pretty good. Logic was sound and it seemed to be returning a class implementing an interface similar to what we would expect for the Factory pattern. However, there’s a slight problem, can you spot it?
The issue is with the first parameter, Project. The method takes a Project, however, we’re only really depending on the Project’s Type property.
publicIStrategyGetStrategy(Projectproject,boolisAffected){vartype=project.Type;if(type==ProjectType.A&&isAffected)returnnewProjectAIsAffectedStrategy();if(type==ProjectType.B)returnnewProjectBStrategy();// Similar if statements}
So why don’t we get rid of the dependency on the Project and instead replace it with the dependency on the ProjectType instead?
publicIStrategyGetStrategy(ProjectTypetype,boolisAffected){if(type==ProjectType.A&&isAffected)returnnewProjectAIsAffectedStrategy();if(type==ProjectType.B)returnnewProjectBStrategy();// Similar if statements}
Instinctual, I knew this was the right call, but I couldn’t remember why I knew it was a good choice. After some digging, I remembered that this is a Law of Demeter violation, or better known as the Principle of Least Knowledge violation.
In general, this principle states that a method should have the least amount of information it needs to do it’s job. Other classic violations of this principle is when you use a class’s internals internals. For example,
One of the reasons that I really like the Law of Demeter is that if you follow it, you create easier to test methods. Don’t believe me? Which is easier to create, the Project class (which may have many dependencies that would need to be stubbed) or the ProjectType enum (which by definition has zero dependencies)?
Another reason that following the Law of Demeter is good practice is that it forces your code to be explicit about what dependencies are required. For example, in the first implementation, the caller knew that the method needed a Project, but had no clue on how much of the Project it needed (does it need all of the properties set? Does it need further setup besides basic instantiation?). However, with the refactored version, now it’s much clearer that the method has a looser dependency on not Project, but just the ProjectType.
Welcome to the final installment of Establishing a SOLID Foundation series. In this post, we’ll be exploring the fifth part of SOLID, the Dependency Inversion Principle.
When working with object-oriented languages, we take large problems and break them down into smaller pieces. These smaller pieces in turn are broken down into even smaller, more manageable pieces to work on. As part of the breaking down process, we inherently have to introduce dependencies between the larger pieces and the smaller pieces.
How we weave these dependencies together is the difference between easily changing behavior and spending the next week pulling your hair out.
When working with classes, dependencies are usually introduced by constructing them in the class that they’re used in. For example, let’s say that we’ve been asked to write an utility that emulates an calculator but it also keeps a transaction log for record keeping purposes.
So far so good, we have two classes (Logger and Calculator) that is responsible for logging and the calculations. Even though this is a small amount of code (~50 lines for both classes), there are three dependencies in the code. The first dependency that I notice is that Calculator depends on Logger. We can see this by looking at the initialize method for Calculator (as hinted above):
The third dependency is probably the hardest to find, but in the the Logger class’ log method, we are also depending on the file system for the machine by using Ruby’s File class. But wait a minute, I hear you say, why is the File class considered a dependency, that’s a part of the Ruby language? I agree with you, but it’s still a dependency in the code and something that we should keep in mind.
From these three dependencies, we’re going to focus on resolving the first two. We could make resolve the dependency on the file system, but it would take so much effort for so little gain.
One thing that I keep in mind when identifying which dependencies to invert is to focus on inverting dependencies outside of the framework. In this case, we’re going to ignore the file system dependency because I, the programmer, depend on Ruby to behave correctly. If Ruby stops working, then a broken file system is the least of my worries. Therefore, it’s not worth the effort to resolve.
In order to resolve these DIP violations, we need to expose ways to drop in these dependencies. There are two ways of doing this. We can either:
Expose the dependency via the constructor
Expose the dependency via a public property
Using the Calculator example, we can expose the logger dependency via the constructor and we can expose the filePath dependency by exposing it as a property.
For the Calculator class, I’m going to first change the initialize method so that it takes a logger instead of constructing it’s own.
A quick run of our program tells us that everything is still working correctly.
Now that we’ve finished up exposing the logger dependency, it’s time to expose the file path. First, I’m going to add a public property on the Logger class call filePath and use that property in the log method
When using the constructor method, it’s very clear to see what dependencies the class has, just look at the constructor. However, adding a new dependency to the constructor may cause other code to fail because the signature of the constructor has changed. This in turn can lead to cascading changes where multiple places of code need to be updated to pass in the dependency.
On the other hand, using the property method, the change is less invasive because the property can be set independently of the when the object was constructed. However, it’s harder to see the dependencies for the class because now the properties are containing the dependencies. Also, it’s very easy to forget to set a property before using the object.
Both of these methods are valid, but when I’m working with DIP, I prefer to expose the dependencies via the constructor because if my class starts to gain more and more dependencies, then it’s a sign that my class is doing too much and violating the Single Responsibility Principle (SRP). You can say that DIP is the canary in the coal mine for SRP.
In summary, the Dependency Inversion Principle (DIP) tells us that we should we should have the outside world pass in our dependencies. If we don’t follow this rule, then we will not. In order to resolve violations, we need to determine what dependencies we have and modify our constructor to accept those dependencies. If our constructor becomes too large, then our class might be violating the Single Responsibility Principle. By following the DIP, we expose the dependencies our classes require and allows for greater decoupling. As always, don’t forget to refactor as you go along.