How I escaped the trap of the table-object binding

I really can’t recall the moment when I started thinking that an object was to be persisted as a table (hence we have a table for every object). I think it was on my first job, using an ORM, but I’m not sure. So what? mine is not a unique story: almost every software developer I know has been through this. I blame schools for this.

So where was I? oh! the table per object idea. That’s absurd. But I digress, let’s move forward. Once upon a time, I worked for a lending company. I was trying to model a loan application process. Something like:

So here it is. Imagine you have to code this. In case you’re not familiar with state machines diagrams, the lines are actions and the circles are states. Before moving forward, get a piece of paper and design a system to automate this process. Just outline the objects and their interactions. Go on, I’ll wait here.

Now, there are 2 different approaches here that will yield very different designs. Let’s review them.

Data centric

The data-centric approach aka database first is probably the most common approach. Basically, you use the relational paradigm to define objects… this has just one caveat: the result is not objects but tables. You create tables and represent them on code. This was my first approach and I ended with something like:

Good. Does your design look like this? If so, you’re in for a fun ride. Let me show you what I mean. Suppose you want to implement this design. It’ll probably look like:

class LoanApplication
{
   ...
   public string Status {get; private set;}
   public void StartReview()
   {
     if(Status !="Awaiting Review") throw new NotImplementedException();
     //do something here
    Status = "Reviewing";
   }

   public void SubmitDocs()
   {
     if(Status !="Docs Required") throw new NotImplementedException();
     //do something here
    Status = "Reviewing";
   }

   public void Withdraw()
   {
     if(Status !="Awaiting Review" && Status !="Reviewing" && Status !="Approved") 
           throw new NotImplementedException();
     //do something here
    Status = "Cancelled";
   }
 ...
}

You can see where this is going. The actions depend on the current state of the process. So with every new state, you will have to modify the guard clauses for the actions that you want to enable. But if you follow a data-centric approach, you probably won’t be bothered by this. You will assume that’s the only way. Luckily for me, the fact that I had to change different methods every time I introduced a new state, led me to further experimentation.

The behavior centric approach

As the name implies this approach focuses on object behavior. That means that the organizing principle is behavior rather than data. The result is rather radical:

So now you have objects that represent the states in the loan application process. The main benefit is that the code is simplified as only the actions allowed in the current state are advertised. See the difference? The behavior associated with each state is what drives the design. I loved this design. It was simpler. I no longer had to check the state before trying to send a message to the object. The only problem left was how to persist this?

Interaction between paradigms

So now, you’re probably wondering if you have to create many tables to store the data for each object. I have talked about having different models for an application before. This basically means that we can use a data-centric approach for the database design, which is basically the Relational paradigm. At the same time, we can use a behavior-centric approach to design the objects. This behavior-centric approach is at the heart of the Object-Oriented paradigm. Having different models interact with each other is just part of dealing with the impedance mismatch problem. The way I dealt with it was by using the repository pattern. Something like:

This way I had several objects being stored and loaded from a single table. The result was nice relational and object-oriented designs that we’re able to evolve independently.

Closing toughts

Probably the most important idea I learned from this experience is that a concept in the problem space can have several representations on the solution space.

Open your mind to this idea and try experimenting with it on your own project. As always, let me know your thoughts.

Liskov’s substitution explained like I’m five

Liskov’s substitution is one of those topics that are self-evident once you get it. However, explaining it can be tricky since the concept itself relies on other ideas. Besides, there are different aspects that you can deepen on, so you can always find something new to learn about it. Anyway, let’s talk about the main ideas and let the minutiae for another day.

The piano metaphor

I’ll assume that everyone knows what is a piano. If you have seen a piano, you will recognize it even if it is grand piano a digital piano, or even a virtual piano! Likewise, if you know how to play the piano, you can play any kind of piano as long as it behaves as such. Let’s break this down.

The shape

How is it that once you have seen a piano, you have seen them all? I mean, a digital piano is made in a very different way than a grand piano, how come you can identify both? Well, turns out all the pianos in the world share 2 kinds of keys (naturals & flats/sharps) that are set up in a very specific order. No matter what, if you see these keys arranged in that way, you know is a piano. Even if you can’t see under the hood!

The behavior

The reason a piano player can play any piano, is because he knows that a key in a given position will produce a note in a given tone. Since this is the same in every piano, he can confidently play a piece in any kind of piano. There may be some differences in the strength needed to hit the key, but that is irrelevant. The fact that the same key on any piano will give the same tone is a warranty.

The contract metaphor

Bertrand Mayer lay the ideas upon which the Liskov’s substitution was built. The main one is the idea of a contract. It basically says that 2 pieces of code can collaborate safely by doing it through a contract. The contract specifies what services are available, what is required to make use of them (pre-conditions), and what can you expect from them (post-conditions). As you will soon see, this idea of contract has 2 different instances in OOP: at the class level and at the method level.

Class contracts

To better understand Liskov’s substitution principle, you have to understand the idea of contracts, applied to classes.

Simply put is a contract around the “shape”: it specifies the services provided by objects of a certain class. Back to the piano analogy, it states that all pianos have keys in a given order, no matter the size, color, or shape of the body. In code, this kind of contract is expressed as interfaces, classes, or abstract classes.

Another aspect of a class contract it’s around its state: Invariants. Invariants are the rules that must be followed by all instances of the class to be valid, i.e. an hour object cannot have more than 60 minutes and not less than 0.

Method contracts

On the other hand, a contract applied to a method is a contract around “behavior”. It states that not only shape is important for a piano to be a piano: whenever you hit the C key, it has to make a sound on the C tone. The tricky part is that in most languages there are no artifacts (like interfaces) that enforce this kind of contract. Even worse, we ourselves are very lousy at defining this kind of contract.

Imagine the following code:

int Add2(int plus){...}

What would you expect of the following expression?

int result = Add2(plus: 3);

What would you think if the result was anything other than 5 or if it threw an exception? It makes no sense right? The function says that is going to add 2 (name) to an integer (parameter) and return the resulting integer (return type). So given we provide it with a valid value (pre-condition), we would expect a valid value in return (post-condition). There is more to say about this, but I’ll stop here.

The Substitution principle vs the Liskov’s substitution principle

A common confusion when trying to understand Liskov’s substitution principle is with regards to the substitution principle. Let’s fix that.

The substitution principle basically states that a class contract must be respected by it’s subclasses.

The Liskov’s substitution principle states that the invariants and methods’ contracts in a class must be respected by it’s subclasses.

Let’s look at the following example:

  public class Piano
    {
        protected Dictionary<string, Action> Keys;

        public Piano()
        {
            Keys = new Dictionary<string, Action>
            {
                {"C", makeSound(tone:"C")},
                {"C#", makeSound(tone:"C#")},
                {"D", makeSound(tone:"D")},
                {"D#", makeSound(tone:"D#")},
                {"E", makeSound(tone:"E")},
                {"F", makeSound(tone:"F")},
                {"F#", makeSound(tone:"F#")},
                {"G", makeSound(tone:"G")},
                {"G#", makeSound(tone:"G#")},
                {"A", makeSound(tone:"A")},
                {"A#", makeSound(tone:"A#")},
                {"B", makeSound(tone:"B")}
            };
        }
        
        public void Play(string key) => Keys[key].Invoke();

        protected Action makeSound(string tone)
        {          
            //do some magic
        }
        
    }

So the class contract on this Piano class says that there is a Play method. The pre-conditions are to receive a valid key, and the post-condition is that some sound is made. Now check this class:

 public class BadPiano : Piano
    {
        public BadPiano()
        {
            Keys["C#"] = () => throw new NotImplementedException();
            Keys["A"] = makeSound("E");
        }
       
    }

This class violates the Play method contract. If we try to play the tone C# is going to throw an exception and if we try to play the tone “A” it’s going to play “E” instead, whereas its parent won’t. Get it? is not just that the behavior is different than its parent, it’s actually out of the expectations of the client. Using this would at least produce some weird interpretation and at most, it will blow the whole program. Again there’s more to say here but I’ll refrain.

Closing thoughts

I hope this helps you grasp the ideas behind Liskov’s substitution principle. While there’s more to be said about the topic, I wanted this to be an introduction. If you’re interested in learning more, let me know in the comments and I will try to follow up later. Enjoy!

Using metaphors to make the code easy to understand

I mentioned this before, but to me, high-quality code has 3 attributes: it’s easy to understand, easy to change, and correct. I always start with trying to make any piece of code the easiest to understand. If you make it easy to understand, even if it’s not easy to change or correct yet, you are in a much better position than otherwise. Whenever I’m mentoring I always explain it like this: if you can understand the “what” you can change the “how”.

So, making the “what” explicit, that’s the challenge.

The socio-technical space

There’s this idea in the DDD circles called socio-technical space. The way I like to think of it is like a continuum that has technical issues/solutions on one side and social issues/solutions on the other.

When you start looking at social issues, the concepts and their interactions provide you with a nice framework where you can reason about the problem. Often, your design will take after these concepts as the building pieces for your solution. That means that if you are working on a system for a banking domain, you likely will have objects like accounts, money, and credit.

But what when you are solving a highly technical problem where the concepts are too vague, abstract, or low level? Well, you can try defining your own concepts to reason the problem and to explain your solution. Or you could use a metaphor.

A technical challenge for you

Exercism.io is a platform to practice solving problems in a programming language. I recommend it to any developer who takes pride in his/her craft. So I was solving the Spiral Matrix problem (login/sign up to access the problem). Before you continue reading I challenge you to solve it. Go on, I’ll wait for you.

So the problem states that given a size you have to create a Matrix[size, size] and you have to fill it with numbers starting from 1 up to the last element. Suppose you have a Matrix[5, 5] then you would have to fill all the slots with numbers 1 to 25. The tricky part is that you have to follow an inward spiral pattern. Are you interested now? try solving it!

Metaphors to the rescue!

The first time I heard about metaphors in the software development realm, was in relation to XP. The idea is simple: use a metaphor to drive the system design. Kent Beck used this on an overall system design level (architecture). But this time I’ll apply it on a smaller scale: the Spiral Matrix solution.

Each XP software project is guided by a single overarching metaphor… The metaphor
just helps everyone on the project understand the basic elements and their relationships.

-Kent Beck, Extreme Programming Explained

Patterns, patterns everywhere!

There are many ways to solve the Spiral Matrix problem. The most obvious solution is to sense the surrounding cells as you move. However, as I was looking at the numbers, I found a pattern in them. Turns out that you can calculate the turning points.

Here I marked all the turning points for a 3×3 matrix. If you lay out the numbers the pattern makes itself visible.

So starting from the right to left, you’ll notice that the distance between the 2 turning points is 1 (where distance is how many spots you’ll have to traverse before finding the next turning point). After the 2 turning points the distance increases by 1. And the sequence goes on. Every 2 turning points the distance increases by 1 until it reaches size-1. I’ll leave it to you to come up with an algorithm to take advantage of this. By the way, the number of turning points is equal to (size * 2) – 2.

Enough talk, show me the code!

So I wanted to make this pattern as obvious as I could, but after the first implementation, it was everything but obvious. After looking closely I noticed there were several things happening at the same time: keeping track of the corresponding number, moving on the grid, and knowing when to turn. So I decided to create objects to handle those responsibilities… but how should I call them?

Sure, you can call your objects however you want, but I wanted to make everything as clear as possible. Easy to understand, remember? So one of the responsibilities was to “navigate” the matrix. This led me to decide on a map. A map helps you navigate right? who could use a map? An Explorer right? after some iterations I ended with something like:

public static int[,] GetMatrix(int size)
    {
        var terrain = new int[size,size];
 
        var compass = new Compass(size);

        new Explorer().ExploreTerrain(terrain, compass);
        
        return terrain;
    }

So imagine you were tasked with starting the numeration at 3 instead of 1. You come and find this code. You’ll probably be puzzled, but the objects make sense to you. Because you understand the relationship between an explorer and the compass. You understand how the compass is used by the explorer. And knowing that, it makes sense to you that the explorer would use a compass to explore a terrain. Actually, it would be weird if he didn’t. But all of this happens in the back of your mind in a fraction of a second, without you really noticing it. So you go and check the ExploreTerrain method.

  public void ExploreTerrain(int[,] terrain, Compass compass)
        {
            while (_stepsTaken <= terrain.Length)
            {
                mapCurrentPosition(terrain);
                adjustDirection(compass);
                advance();
            }
        }

Again, this code is taking advantage of you existing knowledge on the matter of exploration. Wait what is this mapCurrentPosition doing? I think I know, but let’s confirm it.

 void mapCurrentPosition(int[,] terrain) => 
    terrain[_currentPosition.Y, _currentPosition.X] = _stepsTaken;

oh! so it’s putting a number in there… given what we know, this should be the corresponding number… so that is referenced as _stepsTaken! ok, let’s go back. Wait how is adjustDirection accomplished?

   void adjustDirection(Compass compass)
        {
            if(compass.IsTurningPoint(_stepsTaken)) 
                _currentPosition.TurnRight();
        }

So if the compass says that I need to turn at the current step, I turn right (notice how this didn’t puzzle you. Because using a compass to figure out if you need to turn around is something you understand, maybe even experienced before). Maybe we should rename that _stepsTaken variable to _currentStep? let’s go back and figure out what the advance method does.

 void advance() 
        { 
            _currentPosition.Forward();
            _stepsTaken++;
        }

Well, yeah, as expected. Wonder, how does the _currentPosition move forward? (notice we are questioning the “how” not the “what”. We understand what “moving forward” means when exploring). But hold on! where is that _stepsTaken initialized?

class Explorer
    {
        int _stepsTaken  = 1;
        ...
    }

Bingo! let’s initialize this variable to 3 instead of 1 and presto!

class Explorer
    {
        int _stepsTaken  = 3;
        ...
    }

I think you got the idea. If you want to check the details you can find the whole code here.

Closing thoughts

Hopefully at this point the advantages of using a metaphor have become evident (especially in an object oriented system).

Another benefit of using a metaphor is communication. Good metaphors are based on everyday experiences that a lot of people can relate to. This will allow you to convey ideas about the system design/architecture to non-technical people, which becomes increasingly important in agile settings, where the customer is part of the team.

I hope this picks your curiosity about using metaphors in the code. We already do it to explain our ideas in other settings, so why not use it in our code too? I challenge you to do it!

How to improve the signal to noise ratio of your code

As I have previously shared, code quality can be summarized along 3 axes: It is easy to understand, easy to change, and correct.
Today I want to talk about a trait that indicates how easy to understand is a codebase: Signal to noise ratio.

What is signal to noise ratio?

Signal-to-noise ratio (SNR or S/N) is a measure used in science and engineering that compares the level of a desired signal to the level of background noise

https://en.wikipedia.org/wiki/Signal-to-noise_ratio

In software development, this means how much of you code explains your intention/ideas/knowledge vs how much doesn’t.

Why is signal to noise ratio important?

Well, as mentioned before, this is an indicator of how easy to understand is your code. That means, how much time and mental effort is required to understand what the code does and more importantly, why it does it that way. Understanding these 2 facts is a requirement before changing how the code works. There’s no workaround for that.

What is the most influential factor on the signal to noise ratio?

If I were to pick a single attribute on a codebase to change its signal to noise ratio, that would be the abstraction level. You see in my experience poor signal to noise ratio comes from either under abstraction (too much detail) or over abstraction (too many layers of artifacts, too much indirection).

Under abstraction and its effect on the signal to noise ratio

How many times have you been tasked to make a little, tiny change in behavior, only to find yourself with a 200 lines function (… I just had a PTSD episode). The problem with a 200 lines function is that there’s too much detail to easily figure out the what and why.

This detail overload doesn’t happen just at the level of huge functions, but also at the level of language constructs. Take a look:

decimal orderTotal;
foreach(var line in orderLines)
{
    orderTotal+= line.Total;
}

So as you can see, the idea here is that the order total is the sum of the order lines total. So what code here isn’t relevant to that idea? Think about it for a moment. Done?

decimal orderTotal;
foreach(var line in orderLines)
{
    orderTotal+= line.Total;
}

Surprise! I bet a lot of you didn’t see that coming! This is because sometimes we get so used to the language that we give those things for granted. I know I did. It took me a lot of effort learning Smalltalk (and banging my head against the wall every time I tried to do something new) to rewire some parts of my brain. But you can’t deny it. Iterating over the lines is just a detail to sum up the lines total. I does not help conveying the main idea. It’s noise. How would you fix that? Actually, there are several ways.

decimal sumLinesTotal(){
    decimal linesTotal;
    foreach(var line in orderLines)
    {
        linesTotal+= line.Total;
    }
    return linesTotal;
}
...
decimal orderTotal = sumLinesTotal();

How’s that? Not a big deal right? But, now there’s no doubt about the code intention. I know, some of you may think this is dumb. The code itself wasn’t that complex to start with, why should we create a new function just for this? Well, what do you think would happen to a 200 lines function if you started doing this? Not only for loops but every place where implementation details (the how) appear. I dare you to try it. Now, if you are using C# there are other ways to be explicit about this:

 decimal orderTotal = orderLines.Sum(orderLine=>orderLine.Total);

Over abstraction and its effect on the signal to noise ratio

Over abstraction happens when we add unnecessary artifacts to a codebase. This is a prime example of accidental complexity. A very common cause of this is speculative generality: the idea that someday we may need to do something and preparing the code to handle such cases, even when we don’t have the need right now. But there are more common, more subtle cases.

So let’s say we have a report API to which we make requests:

public EmployeeData GetEmployeeData(Guid id);

public EmployeeData
{
    Guid Id;
    ...
}

public ManagerData GetManagerData(Guid id);

public ManagerData
{
   Guid Id;
   ...
}

So our relational mindset tell us that we are duplicating data here (id) and that we should remove that duplication.

public class ReportData
{
    Guid Id;
}

public EmployeeData GetEmployeeData(Guid id);

public EmployeeData: ReportData
{   
    ...
}

public ManagerData GetManagerData(Guid id);

public ManagerData: ReportData
{  
   ...
}

Great! duplication removed! but wait! we can go even further! Isn’t it everything we’re returning just report data? Let’s make that explicit!

public class ReportData
{
    Guid Id;
}

public ReportData GetEmployeeData(Guid id);

public EmployeeData: ReportData
{   
    ...
}

public ReportData GetManagerData(Guid id);

public ManagerData: ReportData
{  
   ...
}

But now the client code need to cast the result to the concrete type. Maybe we can make the ReportData object accommodate different sets of data?

public class ReportData
{
    Guid Id;
    Dictionary<string, object> Data;
}

public ReportData GetEmployeeData(Guid id);

public ReportData GetManagerData(Guid id);

So now let’s say you are given a ReportData object. How can you know if you are dealing with an employee or a manager’s data? You could query the data dictionary for a particular key that represents a property available only in employee (or manager), or worse, you can introduce a key in the dictionary that says which type of data is contained in it, moving from strongly typed to stringly typed. This is all noise. The signal has been effectively diluted.

Some guidelines to improve your signal to noise ratio

By this point I hope is clear to you that to improve your signal to noise ratio, using the right abstraction level is key. So I’ll share with you some of my observations on the abstraction process.

Step 1: remove noise by encapsulating details away into functions

Encapsulation and abstraction are closely related. I’ll talk about it in another post. Suffice to say that as you are encapsulating details away, you’re also raising the abstraction level. The trick to avoid going overboard is to think about what you want to express: the signal. Is that clear enough? A good rule of thumb is trying to make your functions 5 lines or less.

Step 2: uncover the objects

You will find that some functions act upon the same set of data. Those are objects hidden in the mist. Move, both the data and the functions that act upon it to a class. Naming the class will have an impact on the clarity of your signal, but don’t worry to get it right the first time, you can rename it (and you will) as your understanding increases.

Step 3: wash, rinse and repeat

Repeat the 2 previous steps over and over. If the idea you want to convey is still not clearly expressed by the code go to step 4.

Step 4: select a metaphor

To be discussed on the next post. 🙂

A quick comment on comments

As I began writing I mentioned that you need to understand the what as well as the why of the code. The former can clearly be expressed by the code. If that’s not the case, you haven’t reached the right level of abstraction yet. As for the latter, this is the only situation in which I find comments justifiable. Explain constraints or whatever it is that lead you to chose the current solution.

Closing thoughts

Man, that was longer than I expected! I hope this can give you some hints on what to look for the next time you are on a code review (yours or someone else’s). As always if you have any comments, doubts or whatever, leave them below. Good coding!

Problem space, solution space, and complexity explained with pictures

For the last couple of years, my work can be described as nothing but refactoring. And I like it. It’s like taking away the mist surrounding the forest. As you move forward you start to gain a better sense of the code intention and start to detect places where complexity has made its nest.

Complexity is a strange beast. According to Ward Cunningham there are 2 kinds of complexity: empowering complexity (“Well that’s an interesting problem. Let me think about that for a while”) and difficulties (blockage from progress). Does this sound familiar? Where complexity and difficulties come from? To answer this, let’s take a look at the idea of problem space and solution space.

Problem and solution space

The problem space

As depicted in the picture the problem space is this conceptual space delimited by some rules and constraints. More important it includes the current state of affairs and the desired state. Is inside the boundaries of this space that solutions are born.

The solution space

As you can see the solutions are not all equal. Obviously, solution 2 is better than solution 1. This leads me to Ward’s definition of simplicity: Simplicity is the shortest path to a solution. Or in the context of our drawing, the shortest path to the desired state. By the same token, we could say complexity is any path that’s longer than necessary.

Now, this may be tricky. Is possible that solution 2 in our example/drawing requires a kind of knowledge that we don’t currently possess. In that case, we can’t even think of that solution. Or we can’t understand it when presented to us. It would take us the extra effort to acquire that knowledge before we can find solution 2 in our problem space. Hence why is important that we try to have a breadth of knowledge of the (mostly thinking) tools out there. But I digress.

So the shortest path, huh? well “shortest” is not the same for everyone.

Essential complexity

In this picture, the solution in problem space 2 is complex than the one in problem space 1, not because of the solution itself but because of the problem space.

This “distance” between the initial and desired state is known as essential complexity. No matter what you do, the solutions in problem space 2 will be complex than most of the solutions in problem space 1. It’s just the problem is more complex.

Accidental complexity

But what about this?

Clearly, problem space 2 is complex than problem space 1. Still, the solution on problem space 1 is complex than the one in problem space 2!

This is known as accidental complexity. It’s the complexity that comes from the solution we chose. Accidental complexity is our fault and is ours to solve.

And what about difficulties?

Well, now we have found where the complexity comes from. But what about difficulties? let’s review the definition:

A difficulty is just a blockage from progress.

mmmm…. from progress? That implies we are already on the path to our destination. Is it the path of solution 1 or solution 2? It doesn’t matter. What matters is that a solution has been selected and we are traversing through it. Keep this in mind as Ward enlighten us once again:

The complexity that we despise is the complexity that leads to difficulty.

That is accidental complexity!
The difficulty is born out of accidental complexity!

Final thoughts

So there you have it. I’ve been thinking about this stuff for a while. I still do.

As I continue to refactor code, I find myself understanding more about the solution, and the problem space itself. I believe the main difference between a programmer and a consultant, is that consultants start in the problem space, this means that they have the autonomy to explore and select solutions, whereas programmers are tasked to work on the solution space from the get-go. That being said, most of the time, we don’t know how good a solution is until we code it.

This leads me to the tip of the day: if it’s hard, that is, if the way is full of difficulties, maybe you are taking the long path. Try stepping back and ask yourself “is there another way to accomplish my objective?”

Depending upon abstraction is not about interfaces, is about roles

I recently stumble upon this code where someone took an object and extracted an interface from its methods, something like:

class Parent: IParent{
    public void Teach(){}
    public void Work(){}
}

interface IParent{
    public void Teach();
    public void Work();
}

I’ve seen many people (including myself, tons of times) do this and think: “There. Now we are depending upon abstractions“. The truth is, we are depending on an interface, but depending on abstraction is way more than that.

An object design guideline

All objects have a raison d’etre: to serve. They serve other objects, systems, or users. Although that may seem obvious, I’ve found that’s something often overlooked.

Warning: Rant ahead.

I have mentioned this before but, I believe the main reason object-oriented programming is often criticized is that is not well understood.

The idea of an object as an abstract concept that can represent either code or data has not reached enough people to change the overall perception.

A lot of the people I have seen complaining about OOP is doing structured programming. They still tend to separate the data from the operations that are done upon it. Basically structs and modules. It’s sad because this yield software that is hard. Hard to change, hard to understand, hard to correct. Is not soft (as in soft-ware). I blame schools for this. At least in my particular experience, OOP is often delivered as an extension of structured programming, much like C++ is often seen as an extension of C.

We need to reeducate ourselves on the way we think: OOP is not about using object-oriented technology but about thinking in an object-oriented fashion.

This is the reason I started this blog.

End of Rant 😛

So thinking of objects as either data bags or function bags is the result of ignoring a fundamental design question: whom does this object serve?

To answer this question you have to start with the client (object, system, user) needs. This leads itself to a top-down analysis/design approach. But a lot of us are trained to start a system design by thinking on the structure of a relational database, which it’s a bottom-up approach. Let’s see how they differ from each other.

The Database first approach

When designing a relational database, the thinking tools available are Entities and the Relationships between them, often displayed in an ER diagram. So we start with Entities from the nouns on the domain: Parent, Teacher, Student, Child, Class, Course, and so on. I’m pretty sure you can think of a domain just by looking at these concepts.

Now that you have these Entities, you have to think about the processes that interact with them. How do we create a new student? How do we update some of its data? How do we delete it? If you look closely you will find that most everything is modeled as CRUD operations around the Entities. In this scenario, the entities are your abstractions.

The Objects first approach

In this case, you would start by thinking about the needs of the user. This often is expressed as tasks. We usually discover and document these in the form of user stories or use cases. This initial set of needs will serve as the basis for the features of the system. We can now start creating the objects to fullfill these needs. Often this objects will represent the tasks expressed by the user. This is what is known as the application layer on DDD.

From here on things start to get interesting. Pick one of these task objects. What do you need to accomplish this particular task? These are the needs of the object. Now here comes the trick: define an interface/abstract class that fulfills one specific need and name it as such. By doing this we force ourselves to define a specific concept for a specific need in a specific operation. We call this kind of concepts: Roles.

I love the naming schema that Udi Dahan uses for Roles: IDoSomething/ ICanDoSomething. In this approach roles are your abstractions.

Entity vs Role

Let us go back to the original issue: what it means to depend on abstractions?
To answer that we need to answer another question first: what is an abstraction?

Let’s consider the difference between the 2 kinds of abstraction we’ve seen so far: Entity and Role.

First, let’s clarify something: Entities as we have discussed so far don’t belong to the OOP paradigm, they belong to the Relational paradigm. We have discussed before that the needs addressed by a model in the relational paradigm are geared toward disk space optimization, whereas the needs of an object model, particularly an object domain model, are about representing business concepts and interactions in a way easy to change and understand.

Side note: There’s actually an Entity concept in DDD.
An Entity is an object with a unique id. Often, DDD Entity objects overlap with their counterparts on the relational world, because both represent business concepts, but restricting the domain entities to the relational ones greatly caps our thinking and designing ability.

And here we come to the big idea: an Entity (or any object for that matter) can take upon many roles.

This is because roles and entities are different kinds of abstraction. Entities represent a thing/idea whereas roles represent a capability.

And often, depend on abstraction means depend on a role.

A (silly) code example

Let us review our previous code:

class Parent: IParent{
    public void Teach(){}
    public void Work(){}
}

interface IParent{
    public void Teach();
    public void Work();
}

A lot of people are OK with creating this interface before figuring out which services are going to be provided to which client. This is a leaky abstraction. It’s weak and ambiguous on its intention. Can you tell what’s the purpose of an IParent on a glance?

Let’s now review the client code. Let’s say a basic math class can be taught by a teacher, but given the COVID-19 situation it can also be taught by a parent at home:

public class BasicMathClass{
        public BasicMathClass(Teacher teacher){
             teacher.Teach();
       }

        public BasicMathClass(Parent parent){
             parent.Teach();
       }
}

public Teacher{
       public void Teach();
}

class Parent: IParent{
    public void Teach(){}
    public void Work(){}
}

interface IParent{
    public void Teach();
    public void Work();
}

When we look at the client code it’s obvious why the parent teaches. But since we extracted the interface without even checking who was using it before, we are now in a dilemma. One way to solve this could be:

public class BasicMathClass{
        public BasicMathClass(IParent parent){
             parent.Teach();
       }
}

public Teacher: IParent{
       public void Teach(){}
       public void Work(){}
}

class Parent: IParent{
    public void Teach(){}
    public void Work(){}
}

interface IParent{
    public void Teach();
    public void Work();
}

Solved. I know, this is silly, but if you think about it, all teachers also work, so it’s not so crazy to have a work method in there.
But not all of them are parents. So what then? Should we revert the interface?

public class BasicMathClass{
        public BasicMathClass(ITeacher teacher){
             teacher.Teach();
       }
}

public Teacher: ITeacher{
       public void Teach(){}
       
}

class Parent: ITeacher{
    public void Teach(){}
    public void Work(){}
}

interface ITeacher{
    public void Teach();
}

Well, this reads better right? All parents teach, so they are teachers, right? Well, that’s not necessary true either. They can teach, but not because they study to do so, and they cannot teach in a school either.

The problem is in the role conceptualization: we are talking about what something is, instead of what it does.

public class BasicMathClass{
        public BasicMathClass(IEducate educator){
             teacher.Teach();
       }
}

public Teacher: IEducate{
       public void Teach(){}
      
}

class Parent: IEducate{
    public void Teach(){}
    public void Work(){}
}

interface IEducate{
    public void Teach();
}

The change is a subtle one but is important nonetheless: instead of depending on an entity (some thing/idea) we are now depending on a role (a capability). The mental model implications are not to be taken lightly. Once you start depending on roles, you’ll start to think more in terms of them.

So here’s the tip of the day: If you want to talk about what something is, use a class. If you want to convey what it does, use an interface.

Objects are meant to act, not to be acted upon

One of the most common issues I find when mentoring people on object-oriented design has to do with the mentality that many people brings when moving from other paradigms. Particularly with the ones coming from the structured programming paradigm. Let’s clear that up.

Paradigm abstraction levels

To simplify, abstraction level = level of detail. Now imagine map application, something like google map, if you zoom out you can see more terrain and, at the same time, you lost sight of some information like store and street names. This is the idea behind an abstraction level. As you go up, the detail level goes down and vice-versa. Now, how does this relate with programming paradigms?

I often explain paradigms like tinted glasses. You put on some red-tinted glasses and everything looks reddish. If you put amber-tinted glasses everything looks brighter but if you put on some dark tinted glasses everything looks darker. So it is with paradigms: like tinted glasses, they affect the way we look at the world. Programming paradigms in specific provide some constructs to represent the world. So, every time you try to explain a world phenomenon you do it using the constructs provided by the paradigm you’re currently using.

So, we can classify a programming paradigm abstraction level by it’s number of constructs: the more it has, the more details you are dealing with, and hence you’re at a lower abstraction level.

So here’s a brief table showing some paradigms ranked by this criteria:

ParadigmConstructs
FunctionalFunction + Types
OOPObject + Message
Structured ProgrammingProcedures, Data Structures, Blocks, Basic Data Types

This is by no means an exhaustive table, but you get the idea. So you can see that OOP and Functional are paradigms at a high level of abstraction, whereas Structured Programming operates at a lower level of abstraction.

So you see, OOP abstracts both data and code under one concept: an object. Just as important, it also abstracts the control flow under the concept of the message. Those are the tools available to you in this paradigm.

The root of all Evil

Well, maybe not of all evil, but surely it has brought a lot of problems. And that is: to believe that you are working on the OOP paradigm because you have an OOP compliance language while keeping a Structured Programming mindset. There, I said it. I know this will irk some people, but there’s no way around it. Let me show you.

var range = Utils.GenerateSequence(from:1, to:7);

So I think that’s pretty straightforward OO snippet, right? Except it isn’t. Let’s see how would it look like if it truly were OO.

var range = 1.To(7);

So let’s review the differences. This may be a little tricky as the differences I am referring to are not in the code itself but in the mindset that generates it. Let’s start with the code and see if we can identify the mind patterns that generate it.

Differences between the Structured Programming and Object-Oriented mindsets

The main problem I find with people I coach or work with, it’s the idea that object == data structure + procedures. The problem with this is that it becomes a limitation. So, in the statement:

var number = 1;

People tend to think of ‘number’ as data since that’s what we are assigning to it. This difference between objects and data is throwing people off in the wrong direction. Remember that there is no such thing as ‘data’ in OOP, just objects and messages. You should think of ‘number’ as an object.

On the other hand, something like:

Action action = Utils.GenerateSequence;

It’s an object that represents code. But most people use the concept of pointer as a way to explain C# delegates. Why? because to them object == data structure + procedure. Anything outside of that definition is no object to them. By the way, this is what a pointer looks like in C#:

int* ptr1 = &x;

So the main question is: are you treating a variable as a data structure that needs to be passed around to functions in order to do something with it (is acted upon)? If that’s the case you are working on a structured programming paradigm (most likely). The Math class in the .net framework is a prime example of this.

On the other hand, do you send messages (‘invoke a method’ in C#/Java lingo… don’t really like the term) to the variable to do something that requires little to no external help (acts itself)? Congratulations, that’s exactly what OOP is about.

Conclusion

It’s not my intention to trash any paradigm out there. Every paradigm is useful in the right context. It’s just that there is so much confusion about them that I often find myself explaining this stuff over and over. So I hope this makes it clearer for you. If you ever find yourself struggling with OOP, try taking a step back and see if you are really operating on the OOP paradigm. Who knows, you may be surprised at your discoveries (as some of my mentees had been). See you on the next post!

Quality code pillars: a guide to better code reviews

As a code mentor and member of a software development team, I’m subject to and carry on code reviews. However, I have noticed that many times people doing the review don’t have a clear idea of what to look for. This leads to discussions on stuff like style and micro-optimizations. Having been there myself, I would like to offer some ideas on things that you could look for when doing a code review. I want to share my personal quality standard. To me, a high quality code is easy to understand, easy to change, and correct. In that order.

Easy to understand

I have found that communication is the main idea here: does the code communicate the ideas succinctly?

When reviewing, I look for code that is poorly encapsulated or named. Also pay attention to the semantic distance between the concept and the symbol in the code that represents it, it may reveal leaking abstractions. For example, you want to represent a money amount, so you code something like:

var debtInUSD = 200.15;

So if you familiar with USD you know that the decimal part refer to cents. How do you know we are dealing with USD? because the variable says so. Imagine if it were only something like:

var debt = 200.15

Could you tell what the currency is? is it USD or Euro? You probably would have to hunt down the code to figure that out. So you see, naming is very important when you try to make your code easy to understand. Don’t be lazy. Use meaningful names. Now consider the following example:

var debt = Money.USD(units:200, cents:15);

In this case, you know you are dealing with USD. At least at this point. If you find this later down the road you probably will have to hunt for the definition to see what are we dealing with. However, if you don’t care about the type, this should be enough (in the example being used here, you can think of USD and Euro as some kind of logical type, even if they’re only instances of the money type). Imagine the following:

var debt = Money.USD(units:200, cents:15);
debt = debt.AsEuro();

In this scenario, including USD in the variable name would be misleading.

Easy to change

A code should be easy to change. You can think of this as a platform to add new features. The code should make it evident where to introduce the new feature. This requires constant refactoring to reflect our new knowledge in the codebase. The codebase itself should be a reflection of our current knowledge. There are many things that make a code hard to change, uncle bob classifies them in the following categories: rigidity, fragility, immobility and viscosity. At the heart of them lies the idea of coupling.

I often look for references that couple objects, modules and projects unnecesarily.

Correct

What I mean by this is as free of errors as possible.

Typically, we deal with 3 types of errors: syntactical, semantical and runtime.
Since the compiler usually handles syntatical errors, let’s focus semantical and runtime errors.

Semantical errors are related to the business logic. This is a moving target since the rules the software try to model tend to change over time (at least this is true for a line of business application). We usually detect them using unit testing and acceptance/functional testing.

Runtime errors are usually related to resources used by the application. You can detect these using integration, load and any other kind of tests that exercise the application resources.

If the tests related to the code piece I’m reviewing are not present, I ask the developer for any.

Closing thoughts

So, there you have it. I would like to say that the order in which these appear is the priority order for me i.e I’ve found that if I start trying to create an easy to change codebase, I tend to end up with some code that is hard to understand. The reason for this, in my experience, is that we often introduce new levels of indirection in order to decouple the code, which in turn makes it harder to understand. So, by focusing on making the code easy to understand first, I can begin introducing indirection levels as this becomes necessary and still have a codebase that a new developer can pick up rather quickly. And if you have a code that is easy to understand and easy to change, you can easily correct it.

By the way, TDD promotes all of these, but that’s a post for another time 😉

So, what do you look for when doing a code review? leave your comments below

Models and business logic patterns

In the last post, we talked about the different models that co-exist in an application. However, some of these models may or may not exist in a given application and time. To get a better understanding of this effect, let’s review 3 different configurations and the way they affect the application models mentioned before.

Transaction script

A transaction script as described by Martin Fowler, it’s a procedural (often a structured programming) approach to organize an application logic. It contains all the steps to fulfill a request/use case/scenario/story. In my experience, this kind of applications often uses the persistence model directly. This means that this model doesn’t have a domain model but the business logic is embedded in the transaction script itself. Often the transaction script returns a different model on the result, effectively using a presentation model.

Table module

A table module is a different way to organize an application. It’s based on the idea of having a structure that represents a table, just one row at a time. The object itself it’s just an access mechanism to the data of the underlying table. It allows to move to any given record and provides access to all of its fields. You can put the business logic onto this object making it instantly available to all of the records. Often this object is directly bound to the controls of the UI. This means that the persistence, domain and the presentation model are exactly the same.

Domain model

A domain model centric configuration works by separating the domain layer from anything else. The resultant objects are focused on one thing and one thing only: hosting the business logic and rules. By doing this (keeping the domain model ignorant to persistence and presentation needs) we are forced to create a persistence and presentation model. This may initially closely resemble the domain model but can change independently to accommodate the needs of the layer it belongs to (presentation, persistence). Usually, the domain model is developed using an OOP paradigm, whereas the persistence model is often developed under a RELATIONAL paradigm (if using a relational DB) and the presentation model is done following an ACTION or OBJECT representation approach.

Closing thoughts

So there you have it. Go and check your codebase again. Which pattern it follows? Can you pinpoint the different models being used? leave your comments below…

Not all models are the same

Some time ago I used to work for a company that made gelatin capsules used for drug administration. The process was a tricky one: there were several machines that would mix the gelatin and then you have a to wait a cool down time before you could start using it. There were different kinds of gelatin with different mix and cooldown times. My job was to create a simulator that would calculate the optimistic use of the machines given the requirements for different kinds of gelatin. I was given access to a database that had everything I needed. So my initial domain model was based on the DB structure. As time passed by, however, it was clear that the current model was lacking a lot for my purposes. So I just dropped the whole thing and start anew. Using a repository I then mapped the domain model to the DB model. Then something funny happened: my manager could not understand my domain model since he could not reason outside of the DB one. It took me some time to figure what was confusing for him…

Not too long ago I heard something similar. I was helping a friend at work to distill a domain model that was based on a DB. I didn’t do much, just giving some pointers on how to apply OOD principles and helping him to find out how to allocate responsibilities to the right objects. As the model became more clear he came to the conclusion that some of the DB design decisions were getting in the way, so he decided to create his own domain model. Once he did that, the code became so much clear.

The fallacy of one model to rule them all

Unfortunately, a lot of people tends to start modeling the DB and then create a domain model that mimics that structure. That works for a relatively simple system but it won’t stand against a more complex one. The reason it’s simple: the DB model, serves a different purpose than solving the problem. Actually, we deal with several models on a system, each one serving a different purpose. Let’s go over them.

The persistence model

I’ll start with this because often this is the starting point when designing a system. The persistence model actually has the purpose of storing data efficiently. That’s it. We often use E/R diagrams as a tool to understand the concepts of the domain and the relationship between them. One problem with this is that often these concepts are related only on a given context and these relationships are not valid out of that scope. A very experienced developer can avoid that but I argue there are better tools for analysis than E/R diagrams. Usually, the persistence model is very granular.

The domain model

The domain model it’s the one responsible for solving a very specific problem. Hence a domain model should be very specific. Designing a domain model requires you to have an understanding of the problem. This is part of the solution and not the problem space. I believe this model should be created before any other one. If you are using OOP, this model comprises your objects and the interactions between them. Is often more coarse than the persistence model.

The presentation model

The purpose of the presentation model is to allow the user to interact with the system.

When dealing with an object-oriented system, there are 2 schools of thought regarding the user interface: task-based and object-based.

Task-based user interfaces are geared towards a task that involves several objects interacting together. It’s like a script that a set of objects has to follow to accomplish something on behalf of the user. This often results in a more coarse model that aggregate several domain objects. Objects on this model are often called view-model objects.

Object-based user interfaces are predicated on the idea that the user should be able to manipulate the objects together as he/she sees fits to accomplish anything he wants. This means exposing the underlying domain model directly to the user. Patterns and frameworks such as naked objects are examples of this idea.

Traps and tricks

One of the problems I often encounter comes from the use of ORM’s. I’m not saying that using them is bad, but you should use them carefully. They often introduce constraints from the underlying persistence mechanism, forcing us to concern with stuff other than solving the problem at hand. They also somehow promote coupling so it’s not easy to switch the underlying persistence technology, ie from a relational to a document DB.

Another problem arises when you try to expose your domain objects on a task-based UI. In my experience they become intermingled with UI logic, making them a mess that is hard to maintain. Often you end up with additional data that has nothing to do with the object original purpose.

Eric Evans figure this out long ago. That’s why on DDD he provides us with patterns to isolate the domain model from any external influence. Repositories allow the domain model to be completely independent of the persistence model whereas the application API isolates it from the UI. Unfortunately, the intent behind these patterns has been forgotten and thus misuse and abuse of these patterns arise.

Closing thoughts

Next time you start a new project, put mechanisms in place to keep your models separated.

Whenever you find yourself struggling to accomplish something because your model (be it persistence, domain or presentation) cannot accommodate that change without creating a ripple effect to the other models, take a look at the patterns mentioned here and look for ways to isolate the model. This may take some time in the present, but it will pay handsomely in the future.

Good luck out there!