Hacker News new | ask | show | jobs
by Stratoscope 3283 days ago
Personally I find the first version a bit hard to follow, and the second one makes my head spin.

But I can understand the objection to the third version too. I don't think the problem is that the expression is too simple - simplicity is generally a good thing! - but that it's easier to reason about non-conflicting times instead of conflicting times:

If my other meeting ends by the time this one starts, we're good.

Or if this meeting ends by the time the other one starts, we're good.

Otherwise we have a conflict.

So now if you like the step-by-step approach, we can write a function that seems pretty straightforward and easy to understand:

  boolean AreTimesCompatible( TimeRange thisRange, TimeRange thatRange ) {
      if( thatRange.end <= thisRange.start ) {
          return true;  // that one ends by the time this one starts
      }
      if( thisRange.end <= thatRange.start ) {
          return true;  // this one ends by the time that one starts
      }
      return false;  // they overlap
  }
I think it reads fine as a single expression too, given an appropriate comment explaining the idea (which you'd want anyway):

  // Time ranges are compatible if that one ends by the time this one
  // starts, or if this one ends by the time that one starts.
  // Otherwise they overlap.
  boolean AreTimesCompatible( TimeRange thisRange, TimeRange thatRange ) {
      return
          thatRange.end <= thisRange.start  ||
          thisRange.end <= thatRange.start;
  }
And here is the matching function to go with either of those:

  boolean AreTimesConflicting( TimeRange thisRange, TimeRange thatRange ) {
      return ! AreTimesCompatible( thisRange, thatRange );
  }