Suppose we have the following class:
public class FooClass
{
private DateTime _timeStamp = DateTime.Now;
public DateTime CreateTimeStamp
{
get
{
return _timeStamp;
}
}
public void PerformBarCalculations(BarClass bar)
{
bar.DoSuff(this);
}
}The FooClass has a method called PerformBarCalculations that takes a reference to BarClass object. This function calls a method on the BarClass called DoStuff that takes a reference to a FooClass object, in this case, the FooClass is a reference to this or the current object.
The BarClass has the following definition:
public class BarClass
{
public FooClass _parent;
private int expiration;
public BarClass(FooClass parent, int expiration)
{
this._parent = parent;
}
public void DoSuff(FooClass newFoo)
{
this._parent = newFoo;
//...do stuff
}
public bool IsExpired()
{
return DateTime.Now.Subtract(_parent.CreateTimeStamp).Minutes > expiration;
}
}
The BarClass has a reference to its parent and a expiration time. Both of these values are injected on construction. Nothing tricky yet, but lets look at the DoSuff() function. It takes as its argument a reference to a FooClass object. The first line of this function promptly sets the parent reference to the newFoo reference being passed into this function.
"So far so good!" you say? Call the IsExpired() function. What does it return, can you tell me? What if I threw 10 threads at this code. What will IsExpired() return now?
Is this where the issue is coming from or is it coming from some place else. Who knows, maybe. The defect cannot be reliably reproduced. This wasn't even the smelliest part of the code.
Maybe the BarClass should also implement a Maurry Povich function to find out who the baby daddy is?
"To lose one parent may be regarded as a misfortune; to lose both looks like carelessness."
-Oscar Wilde, The Importance of Being Earnest
