Click here to Skip to main content
15,896,912 members
Articles / All Topics

What is Wrong With Me?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Oct 2009CPOL2 min read 19.5K   2   8
Right after unit testing and committing the code, I felt like there is something wrong with me. Here are the details.

Why?

Some time ago, while I was working on a project, that was a top secret project so cannot 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 committing the code, I felt like there is something wrong with 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.

C#-Code: Interval Struct

C#
[code=cs;ln=on;]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

The 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 in 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:

C#-Code: IntersectsWith Implementation

C#
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 a result of too much analytical thinking?
  • Is this weird implementation 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 weird IntersectsWith implementation?
  • Shall I be ashamed of myself?

This article was originally posted at http://blog.pragmasql.com/post/What-is-wrong-with-me.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader PragmaTouch
Turkey Turkey
- Software developer
- Has BS degree in Computer Engineering
- Has MBA degree
- Programmed with C, C++, Delphi, T-SQL and recently C#
- Little educational experience with Prolog
- Feel enthusiasm about NHibernate and LINQ
- Love to develop on Cuyahoga Web Framework
- Developer of PragmaSQL Editor
(Code Project Members Choice Winner for 2009 and 2010)
- Developed JiraTouch and MoodleTouch for iPhone
- PragmaTouch Lead (www.pragmatouch.com)

Comments and Discussions

 
GeneralTo answer your core question... Pin
Middle Manager21-Oct-09 7:49
Middle Manager21-Oct-09 7:49 
GeneralInterval intersection vs. rectangle intersection Pin
Tefik Becirovic12-Oct-09 1:47
Tefik Becirovic12-Oct-09 1:47 
GeneralRe: Interval intersection vs. rectangle intersection Pin
Ali Ozgur12-Oct-09 3:15
Ali Ozgur12-Oct-09 3:15 
Your suggestion does not cover case 3 and case 4

For case 3
Givent that this = Point A and interval = Point B
this.StartValue >= interval.StopValue Evaluates to False
this.StopValue <= interval.StartValue Evaluates to True

Result is False. It should be True though

For case 4
this.StartValue >= interval.StopValue Evaluates to True
this.StopValue <= interval.StartValue Evaluates to False

Result is False. It should be True though


AnswerRe: Interval intersection vs. rectangle intersection [modified] Pin
Tefik Becirovic12-Oct-09 9:14
Tefik Becirovic12-Oct-09 9:14 
GeneralRe: Interval intersection vs. rectangle intersection Pin
Ali Ozgur12-Oct-09 20:21
Ali Ozgur12-Oct-09 20:21 
GeneralRe: Interval intersection vs. rectangle intersection Pin
Tefik Becirovic12-Oct-09 23:44
Tefik Becirovic12-Oct-09 23:44 
GeneralRe: Interval intersection vs. rectangle intersection [modified] Pin
supercat913-Oct-09 6:06
supercat913-Oct-09 6:06 
GeneralRe: Interval intersection vs. rectangle intersection Pin
JesperMadsen12319-Oct-09 19:54
JesperMadsen12319-Oct-09 19:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.