Hi there Elixir friends
In a recent task I was on, I needed to check in two dates which of them is the maximum and which of them was the minimum. I’ve found out that using max
and min
functions from Kernel would work. Per example:
max(~D[2020-12-18], ~D[2020-12-10])
iex> ~D[2020-12-18]
min(~D[2020-12-18], ~D[2020-12-10])
iex> ~D[2020-12-10]
Using this solved the problem in a few seconds. When running the tests in my app, I’ve noticed that in some edge cases one or maybe both of the Dates could be nil
values, but even with that, no error was being given in max
and min
. Started to dig out both functions, and I’ve found this.
With the max function everything was being returned as I was expecting when a nil
value was encountered, it would return the other value and it would only return nil
if both values were nil
. This behaviour was correct.
iex> max(~D[2020-12-18], nil)
~D[2020-12-18]
iex> max(~D[2020-12-18], ~D[2020-12-10])
~D[2020-12-18]
iex> max(~D[2020-12-18], nil)
~D[2020-12-18]
iex> max(nil, ~D[2020-12-10])
~D[2020-12-10]
iex> max(nil, nil)
nil
But, when testing the min
function, the behaviour was not equal to what max
was having.
iex> min(~D[2020-12-18], ~D[2020-12-10])
~D[2020-12-10]
iex> min(~D[2020-12-18], nil)
nil
iex> min(nil, ~D[2020-12-10])
nil
iex> min(nil, nil)
nil
As you can see, when the function receives any parameter that it is nil, simply returns the nil, expecting it to be the minimum between both values, even if one of them is a Date type. This, in my opinion, is not correct behaviour. With this doubt, I talked with a co-worker and he pointed out it could be because of nil being read as Epoch 0. Is theory was probably correct, but today I tried this.
iex> min(~D[1970-01-01], nil)
nil
iex> min(~D[1969-01-01], nil)
nil
With this, I just simply removed the possibility of nil
being considered as epoch 0, and now I’m wondering if it makes sense to contribute to Elixir Repo with a possible solution to this. Does anyone already think about this and what was their conclusions and does it make sense the behaviour of min
? (or even max
just straight return the same as what min
returns).
Best regards!