Sameness in OOP

Mauro Ghiani
3 min readJan 14, 2019

--

If you’ve got the chance to visit The Museum of Modern Art in NY you can stop by the painting of Wifredo Lam, “The Jungle”.

Wifredo Lam from Cuba paints a series of strange faces, bodies along with a sugarcane forest. What’s interesting is the mixed feeling of complete absorbed identities, each of one, with just a small thread of soul, smiling and poking out of nowhere.

Wifredo Lam The Jungle 1943 MoMA

So, let me state a paradox. In OOP and particular in C# identifiers is the name given to entities such as variables, methods, classes, interfaces, etc. They are tokens in a program or routine which uniquely identify an element. For example:

bool isWhatItIs;

So we should expect a sort of sameness, identicalness, or the quality or fact of being the same. Well, it’s not. Identifiers point to the same location in memory but not to the same value. In fact, in OOP we deal with variables, something that has a state and can change over time. Hence the paradox, if something changes it can’t be really an identity, strictly speaking.

isWhatItIs = false;
isWhatItIs = 2 > 0 ? true : false;

The problem could be not really of interest if we had to deal with state, methods, and objects but if we brought to the table a simple concept as that of a mathematical function we are in trouble.
What is a mathematical function? It’s a black box with an input and an output, let’s say both types in the common programming sense.
Now, this function is by definition deterministic, if we run it one-time o thousands of times, given the same input we ought to have the same output. This is very important for the following reasons:

  • Parallel: We can run functions in parallel. No locks, mutexes, semaphores are needed.
  • Lazy: The output of the function will be the same whether it is evaluated now or later.
  • Cache: The output can be cached (memoization) because the same input always gives the same output.
  • Purity: In presence of many functions they can be evaluated in any order it is liked. The order can’t make any difference to the final result.

In F# and functional programming, an identifier is simply a value. Once created cannot change. And that’s the beauty of it. You can’t write a function like the one below, GetValue() whose output is always different depending on an internal state, state that might be changed anywhere in the program, although we might try to protect it from any casual modification:

 private int _v = 0;
public int GetValue()
{
_v++;
return _v;
}

In FP, if you need something like a progressive counter, you can memoize the value inside the function:

let getValue =
let N = Seq.initInfinite id
let n = N.GetEnumerator()
fun () ->
n.MoveNext() |> ignore
n.Current

So, identities in order to be so they need to remain the same. A value is a particular state of a variable. But you can’t change it, ’cause if you do so you have another value!

--

--

Mauro Ghiani
Mauro Ghiani

Written by Mauro Ghiani

A complete non sequitur being.

No responses yet