Why?
Some time ago while I was working on a project, that was a top secret project so can not give more details
, I realized that I've produced some sort of weird code that checks if an interval (Start,Stop integer value pair) intersects with another interval. Right after unit testing and commiting the code I felt like there is something wrong wih me. Here are the details
Nothing Fancy
Here is the code of my Interval structure. There is nothing fancy about this structure it is used to hold two integer values and performs range checking in the constructor to guarantee that start value is always smaller or equal to stop value.
001public struct Interval
002 {
003 private int _startValue;
004 public int StartValue
005 {
006 get { return _startValue; }
007 private set { _startValue = value; }
008 }
009
010 private int _stopValue;
011 public int StopValue
012 {
013 get { return _stopValue; }
014 private set { _stopValue = value; }
015 }
016
017 public Interval(int startValue, int stopValue)
018 {
019 if (startValue > stopValue)
020 throw new TypeInitializationException("Interval",
021 new Exception("Provided start value is greater than the provided stop value."));
022
023 _startValue = startValue;
024 _stopValue = stopValue;
025 }
026
027 public bool IntersectsWith(Interval interval)
028 {
029 //TODO: Check if this intersects with the provided interval
030 }
031 }
Conventional Way
Conventional way of implementing that IntersectsWith method is to 1) write some if/else blocks or 2) to combine a single return statement to cover all of the cases illustrated on the following image
My Problem
But somehow I did not choose the conventional implementation and I decided, in fact by reflex, to re-model Interval objects as rectangles with 1px in height , place them on xy coordinate system and check if two rectangles intersect or are tangent to each other. Here is my weird IntersectsWith implementation
001public bool IntersectsWith(Interval interval)
002 {
003 Rectangle r1 = new Rectangle(StartValue, 0, StopValue - StartValue, 1);
004 Rectangle r2 = new Rectangle(interval.StartValue, 0, interval.StopValue - interval.StartValue, 1);
005
006 return r1.IntersectsWith(r2) || (r1.X + r1.Width == r2.X) || (r2.X + r2.Width == r1.X);
007 }
Questions to myself
- Is this weird implementation is a result of too much analytical thinking?
- Is this weird implementation is a result of too much abstract modeling I have to do to perform my job well?
- Shall I see a therapist?
- Is this weirdness a common pattern among developers?
- Shall I ask this as an interview question? And what shall I do with people implementing this method like me and not like me?
- How will my colleagues feel when they have to read my wierd IntersectsWith implementation?
- Shall I be ashamed of myself?
Code
WeirdIntersectsWith.rar (23.00 kb)
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5