The One and Only Software Design Principle
Tags: beginners, tutorial, design, oop
Published: 2020-10-13 02:29:33

If we build our entire paradigm on a single rule, we can keep it simple and make excellent models.
Being minimalist and being axiomatic means we can derive a set of rules from a single definition.

If we build our entire paradigm over one single rule we can [Keep It Simple, Stupid](https://en.wikipedia.org/wiki/KISS_principle) and make excellent models and thus excellent software.

One of the most underrated quality attributes on software is the kind of being predictable. [We are taught at the university and books that software should be fast, reliable, robust, secure, etc](https://books.google.com/books?id=mdiIu8Kk1WMC&pg=PA74&lpg=PA74&dq=clements+quality+attributes&source=bl&ots=UeR_V8ecOU&sig=ACfU3U25yW6Cbz9-Takxo2LNKrcc_yAPoA&hl=es-419&sa=X&redir_esc=y#v=onepage&q=clements%20quality%20attributes&f=false). but being predictable is almost nowhere on the top five design priorities.

We are going to make an exercise on object-oriented software design by stating just one principle:

> Each domain object must be represented by a single object in our computable model and vice versa.

![Model](https://dev-to-uploads.s3.amazonaws.com/i/z17telh3wte1qn79iyi0.png)

The relationship between objects of the model and entities of real world is 1: 1

We can see the justification of this model in this article:

{% link https://dev.to/mcsee/what-is-wrong-with-software-5pa %}

We will then try to derive design rules and heuristics from that axiom, of course without contradicting it.

# The Problem

We will see that most of the language implementations used in the industry ignore this rule and this causes enormous problems. Invoking difficulties that arose in the construction of software 3 or 4 decades ago and that they almost no longer exist or are present in a few domains

We keep on making monkey decisions beating each other without knowing[ the reason for such behavior.](https://www.youtube.com/watch?v=pgJ8-IaBSeY)

![Monkey](https://dev-to-uploads.s3.amazonaws.com/i/vuz6j3wstk1ktrxby1wu.jpg)

Monkey and banana experiment

## Models to the rescue

When building models of any kind we want to be able to simulate the conditions that occur in the observed real world so that we can follow each element of interest in our simulation and stimulate it to observe the changes in the same way that they happen in the real world.

Meteorologists make mathematical models to predict and anticipate the behavior of climate and most scientific disciplines are based on these simulations. With the rise of machine learning, we build black-box models to predict behaviors in real life.

## The importance of bijection

In the domain of software and under the paradigm of objects we will always have one and only one object representing a real-world entity.
Let's try to prove by the absurd what would happen if we did not comply with the principles of being a bijection.

### Common cases in the industry that violate bijection

Case 1) We have an object in our computable model to represent more than one real-world entity. For example, many programming languages ​​model algebraic measures using only scalar magnitude.

Then we can represent 10 meters and 10 inches (two completely different entities in the real world) by a single object (the number 10) and we could add them together obtaining that in our model the number 10 (representing 10 meters) the number 10 (representing 10 inches) is equal to the number 20 (representing who knows what).


![broken bijection](https://dev-to-uploads.s3.amazonaws.com/i/0ruf6swynjudhinbribk.png)

Bijection is broken

This generates problems not always captured on time. Because it is a semantic fault, the error usually occurs long after the failure as in the famous case of the [Mars Climate Orbiter](https://www.latimes.com/archives/la-xpm-1999-oct-01-mn-17288-story.html)

![Mars Probe](https://dev-to-uploads.s3.amazonaws.com/i/1ri38jiqk82v1zcuwgju.jpg)
 
The probe exploded by mixing different units of measurement

Case 2) Our computable model represents the same real world entity with two objects.
Suppose we have in our observable real world an athlete John Smith who competes in one discipline but who is also a judge in another athletic discipline.
A single person in real world should be a single object in our computable model. We need to model just the minimum behavior to fulfill our partial simulation.

If we have two different objects (a competitor and a judge) that represent Jane Doe, we will sooner or later have inconsistencies by wanting to assign some responsibility to one of the two and not see it reflected in the other.

![Jane Doe](https://dev-to-uploads.s3.amazonaws.com/i/xm1dpep64j4m7vr05092.png)

Jane Doe Is represented on our model by two different entities

Case 3) A bitcoin wallet can be represented as an anemic object (with some properties regarding address, balance etc) or by a functional one (with responsibility such as receiving transactions, writing to a blockchain etc) but it's clear for someone who is not in software business they are related to the same concept. So the bijection must be held.

To solve these types of problems we must stop seeing entities as *data structures with attributes*, think of them as objects and understand that they are the same object fulfilling different roles depending on the context in which they are interacting.

Case 4) In most modern object programming languages, a date can be constructed by creating it from its day, month and year.\
We all learned that a November 31th, 2020 can be created and that most of the languages ​​will gently return a valid object (probably December 1st, 2020).

But this disguised as a benefit is nothing but error hiding, generating a coupling dependency to the design decision made by the programming language and hiding a sure error in the data load.

{% medium https://codeburst.io/coupling-the-one-and-only-software-design-problem-869e293a9f04 %}

The error will raise when running a nightly batch processing these dates far from the root cause violating the [Fail Fast](https://en.wikipedia.org/wiki/Fail-fast) principle.

{% medium https://codeburst.io/fail-fast-3f3f036032b0 %}

# Conclusions

In this article, we define the only axiomatic design rule that we will respect no matter what by justifying the problems that come with not respecting it and laying the foundations for future definitions derived from it.

* * * * *

Part of the objective of this series of articles is to generate debate and discussion spaces on the problem of software design.

We look forward to the comments on this article.

# Acknowledgements

Part of the ideas in this article were developed together with Hernan Wilkinson and all the members of Software Engineering Staff on [*Universidad de Buenos Aires.*](https://www.isw2.com.ar/)

This article is published at the same time in Spanish [here](https://medium.com/@mcsee/el-%C3%BAnico-principio-de-dise%C3%B1o-de-software-importante-36c3c4f64c1b).