The evil powers of mutants
Tags: webdev, programming, mutation, oop
Published: 2020-12-19 20:18:08

Since the very beginning of stored program concept, we learned that software is Programs + Data. It is clear that without data there is no software.

In object-oriented programming we build models that evolve over time, emulating the knowledge we learn by observing the reality we are representing.

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

However, we manipulate and abuse those changes uncontrollably, violating the only important design principle by generating incomplete (and therefore invalid) representations and propagating the ripple effect with our changes.

{% post https://dev.to/mcsee/the-one-and-only-software-design-principle-3086 %}

In the functional paradigm, this is elegantly achieved by directly forbidding mutations. We can be (a little) less drastic.

# Returning to the essential

The great [Fred Brooks](https://en.wikipedia.org/wiki/Frederick_Brooks) gave us a few thoughts. Among others, he told us about [The myth of pregnant women,](https://en.wikipedia.org/wiki/The_Mythical_Man-Month) he taught us to be suspicious of silver bullets and educated us in the separation of accidental vs. essential complexity.

> The essence of an entity of reality is that which makes it be itself and not another.

{% post https://dev.to/mcsee/no-silver-bullet-320 %}

The entity's accident happens due to temporary situations that despite changing object behavior do not prevent us from noticing we are watching the same entity even though it evolves as we do not [bathe twice in the same river.](https://www.goodreads.com/quotes/117526-no-man-ever-steps-in-the-same-river-twice-for)


![River](https://dev-to-uploads.s3.amazonaws.com/i/1uf1txlkty10l9kcq317.jpeg)

Photo by [Alexander McFeron](https://unsplash.com/@alexmcferon) on [Unsplash](https://unsplash.com/s/photos/bath-on-river)

Being true to our bijection, in our computable model we should be able to distinguish when an object changes in terms of the accidental and prohibit all essential changes (because they would violate that bijection which is our only principle).

> Objects should know how to defend themselves against invalid representations. They are the powers against mutants.

## Is data ok ?

In most countries, an invoice is a written and unchangeable document. Altering it has criminal consequences similar to what happens with a transaction in a blockchain chain or a ledger.

In this problem domain, immutability is a rule, therefore our simulations must respect it.

However, those of us who have worked in economic, financial or banking domains have built systems that violate these rules systematically with some excuse related to *laziness* or *performance* (our favorites).

As a personal anecdote, in one of my first jobs for a large international bank, we modeled financial transactions with an isDataOK attribute that we had as a Boolean flag until we made sure that the transaction was indeed a valid and actionable transaction. This brought us multiple [coupling problems](https://mcsee.hashnode.dev/coupling-the-one-and-only-software-design-problem-ckejcs8u700w02ss14hey2ffu) on multiple occasions.

Besides, many of those fields remained with null values ​​(instead of modeling the incompleteness or indefiniteness of the data), so we had to spread the code with multiple controls by *ifs* to validate that some data against nulls. 

{% post https://dev.to/mcsee/null-the-billion-dollar-mistake-1h1c %}

Thinking about how to build a solution to the problem we were solving at the time, I found the answer on our only axiom: Bijection one by one with reality.

Let's revisit our 90s code:

{% gist https://gist.github.com/mcsee/2ee0069b851c3500a6fae00c8ee14458 %}

A hollow class with a lot of attributes and no encapsulation but with a flag (*isDataOK*) should when it could be safely used.
Let's start by hiding the decision to know when it is an actionable movement.

{% gist https://gist.github.com/mcsee/cddb71ed240ece973d686766b653bd96 %}

Then let's go on encapsulating the movement's attributes:

{% gist https://gist.github.com/mcsee/e85dba7a09f16494f7b956667419ae6c %}

This movement is mutable (despite not being so in the real world). We must ensure that it behaves as our observed entity.

{% gist https://gist.github.com/mcsee/9247c16a3af3a743b2c60ca479762a05 %}

Simple, elegant, immutable, without dataOk, always valid, without setters or getters.

Movement is valid from inception, just as it happens in the real world.

Now let's assume that a business rule prevents us from making movements between the same party and counterparty (this happens in the real world).

In our first version this control would be impossible. In the immutable version we only represent real situations, it will be enough to prevent the construction of these objects.

{% gist https://gist.github.com/mcsee/08bc4e593f42ce14460f95de78be8db9 %}

## Times are changing

We are going to continue the previous example focusing on the date on which said the transaction was made.
In the real world, a date represents a day on an arbitrary calendar.

![Time](https://dev-to-uploads.s3.amazonaws.com/i/dj3j1lul7x2fot1bk32w.jpeg)

If we create a movement in bitcoins for [May 12, 2020's halving event](https://www.cnbc.com/2020/05/08/bitcoin-btc-cryptocurrency-prices-rise-as-halving-approaches.html) and we recreate it in our computable model we will have something like this.

{% gist https://gist.github.com/mcsee/821d9499b2518d47f192faa6c0f0294c %}

But this violates our unique design principle of maintaining a bijection with the real world. Let's be true to our one rule.

{% gist https://gist.github.com/mcsee/339a71cdb67c3e398ada290327c1210a %}

We model reality's entities such as a day of a month, a calendar year, and a date, forgetting about arbitrary implementations with integers because bijection and declarative are more important than performance and laziness.

Let us dwell for a minute on the mutability of a date. One hopes that a date will never mutate because it does not do so in the real world. No non-computer person would ever think of changing a date.

Let us analyze by the method of reduction to the absurd what would happen if we allow a date to change.

Our accredited transaction on the day of halving knows its imputation date. If it changes internally all consecutive blockchains should be recalculated and this is expressly prohibited by the financial domain. It is clear that the date should never mutate.

# Is it crystal clear for everybody that a date should not mutate?

Let's review the date class in the most widely used languages ​​in today's industry.

[Go](https://godoc.org/google.golang.org/genproto/googleapis/type/date): Date is a struct.

[Java](https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html): Mutable (deprecated).

[PHP](https://www.php.net/manual/es/class.datetime.php): Mutable with setters abuse.

[Python](https://docs.python.org/2/library/datetime.html#date-objects): Mutable (All attributes are public on Python).

[Swift](https://developer.apple.com/documentation/foundation/date): Mutable.

Date problem's domain is probably one of the oldest and best known to humanity. The excuse that these *getters* are being deprecated speaks about poor initial design in most modern languages.

# Possible solutions

A possible attack is to reverse the burden of proof. Objects are completely immutable unless otherwise stated.
Should they evolve they must always do so in their accidental aspects, never in their essence. This change should not be coupled to all the other objects that use it.

# Conclusions

> If an object is complete since its creation, it will always be able to answer to messages.
> An object must correctly represent the entity since its inception.
> If we work in a concurrent environment it is essential that the objects are always valid.
> An object must be immutable if the entity it represents is immutable
> Most entities are immutable.

These rules keep the model consistently consistent with representation.
As a corollary of the demonstration by the absurd we can derive a series of rules:

Corollary 1

> Objects must be complete since their creation.

Corollary 2

> [Setters](https://mcsee.hashnode.dev/nude-models-part-i-setters-ckec0inec005za7s1di7o9w88) and [getters](https://mcsee.hashnode.dev/nude-models-part-ii-getters-ckedliv3e01vutds11ikg3ctt) must not exist.

Corollary 3

> Getters should not exist (unless they exist in the real world and then the bijection is valid). It is not the responsibility of any reality entity to reply to a getXXX() message.

* * *

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

We look forward to comments and suggestions on this article.

This article was published at the same time in Spanish [here](https://medium.com/@mcsee/los-poderes-mal%C3%A9ficos-de-los-mutantes-664163409f41).