-
Notifications
You must be signed in to change notification settings - Fork 4
Operator TimeFrame#& should not return nil #1
Comments
Can you give an example where the operator does not behave as expected? |
Yes, I mean the return of nil. That's unexpected and may result in follow-up errors. |
We discussed the return value in case of non intersecting TimeFrame instances too, and we decided to go with the What would you expect to be returned if there is no intersection? An empty TimeFrame instance? Which time value for It would be possible to define a time value like Alternative solution: We could also create an instance of TimeFrame within the gem, which is empty by default and which would be returned in every case where an empty range should be returned - let's discuss about that. In terms of code: Current solution: time_frame = TimeFrame.new(min: Time.new(2014), duration: 1.day)
other_time_frame = TimeFrame.new(min: Time.now, duration: 1.hour)
intersection = time_frame & other_time_frame
puts 'Intersecting!' if intersection The empty TimeFrame solution: time_frame = TimeFrame.new(min: Time.new(2014), duration: 1.day)
other_time_frame = TimeFrame.new(min: Time.now, duration: 1.hour)
intersection = time_frame & other_time_frame
puts 'Intersecting!' unless intersection.empty? I like the first code sample a bit more, but this is a matter of personal taste - I am happy with both solutions in the end 😉 |
First of all: Coming from a mathematical background I have to say that this operator can never be bijective since the preimage is a space of dimension 2 and the image a space of dimension 1. So it is easy to generate four completely different timeframes t1 to t4 where t1 & t2 = t3 & t4. That means this operator is not injective and so also not bijective. Lets get back to the topic. While having to timeframes t1 and t2 as imput we can have two scenarios: they cover a common timeframe or they don't. in case they cover a common timeframe, I think you agree with the result. So lets talk about the scenario they don't have a common timeframe. If they do not have a common timeframe, the result is (looking on it from a mathematical perspective with intervals) the emptyset (or nullset). So we decided to return nil which is the mathematical representative for empty set. But lets discuss this approach. You suggest to throw an error, but from our current point from view this makes it a bit more difficult to handle it in code since you have to try catch (begin/rescue) it and (and this is more important to me) it's mathematically wrong, since the result is just the empty set. I think the only alternative which makes sence is a timeframe with no data. Such a timeframe should not have a min or a max value (timeframe.min = timeframe.max = nil). But such a change might also have some other strange impacts. What is the duration of such a timeframe? nil? 0? What is its behavior regarding the other methods ( |
I also change the topic header, since this title is not correct |
I agree with @mblumtritt, as other Ruby core/stdlib classes like |
we already used the
|
@bstoecker: Not sure whether I understand what you mean with (1). I'd prefer a simple null object similar to |
So we need an empty TimeFrame. I would suggest to enhance the TimeFrame class so it can deal with empty TimeFrame instances correctly, means: handling the case of I would suggest to provide an constructor which can handle no params which would lead to an empty I don't consider |
regarding |
@jzernisch Can you clarify the meaning of "I'd prefer a simple null object similar to Array or Set." ? Do you mean Null Objects like https://github.com/avdi/naught or do you mean something else? |
in case of an empty-timeframe object we should also consider to change the behavior of |
@pderichs What I mean was that we have a Does handling all this set-theoretic stuff in a |
The result of an operator has to be bijective. If there is no way to return the expected type (TimeFrame) you should rather throw an exception.
The text was updated successfully, but these errors were encountered: