Ali Özgür - The Pragmatic Developer

My Amazon.com Wish List

RecentComments

Comment RSS
PragmaSQL was named as Best General Database Tool by The Code Project’s members.
Members were asked to rate their favorites on CodeProject. The Code Project staff then compiled the highest rated listings, awarding the highest ranked in each product category. The results reflect actual perceptions of what developers value. Each codeproject.com member decides which companies or products deserve recognition, based on in-the-field experience.  

Posted in: CodeProject  Tags:
aliozgur posted on May 18, 2009 16:46

Why?

Some time ago while I was working on a project, that was a top secret project so can not give more details Smile,  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.

public struct Interval
{
private int _startValue;
public int StartValue
{
get { return _startValue; }
private set { _startValue = value; }
}

private int _stopValue;
public int StopValue
{
get { return _stopValue; }
private set { _stopValue = value; }
}

public Interval(int startValue, int stopValue)
{
if (startValue > stopValue)
throw new TypeInitializationException("Interval",
new Exception("Provided start value is greater than the provided stop value."));

_startValue = startValue;
_stopValue = stopValue;
}

public bool IntersectsWith(Interval interval)
{
//TODO: Check if this intersects with the provided interval
}
}

 

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

 

public bool IntersectsWith(Interval interval)
{
  Rectangle r1 = new Rectangle(StartValue, 0, StopValue - StartValue, 1);
  Rectangle r2 = new Rectangle(interval.StartValue, 0, interval.StopValue - interval.StartValue, 1);

  return r1.IntersectsWith(r2) || (r1.X + r1.Width == r2.X) || (r2.X + r2.Width == r1.X);
}

 

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)


Posted in: .NET Development , General Programming   Tags: