CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

Obsolete Blog Posts

I’ve begun to update some really old posts, adding the following text at the top of the post:

OBSOLETE CONTENT
The author of this post has determined that this content is obsolete. Use at your own risk! Blog posts are a point-in-time snapshot of the blogger's thinking and should not be assumed to represent this blogger's current opinions. This post was left up for historical purposes.

I’ve done this, because I have some really old heavily-visited posts that I'm not so sure are steering people in the right direction.  For example, take a look at A Simple Web Progress Bar..., which I posted about a year ago.  The reason I’ve determined this to be obsolete, is that there’s something called the “Busy Box” that I think is probably a much better way to go. In fact, I’m going to look into switching to the Busy Box on my own apps in the near future. Oh, and for God’s sake, don’t even try to follow my confused thinking around object equality in this post here

I just don’t want people actually taking my advice on this old stuff, but I don’t want to remove it either.  I’ve obsoleted about ten posts, and I feel so much better!

– Brendan



Comments

Brendan Tompkins said:

Sam,

This very thought was occuring to me as I was posting this. Others were, 1) no one cares anyhow, and 2) why don't I just delete the old posts? and 3) I really should have stuck with my original career plan of owning a falafel cart.

But yes. I agree.

The long and short of it, with the Overloading and Equality post, is that I was describing how to create a hash code based on settable members. As Raymond Chen put it in a comment (I should frame that one) is that you should only override hash code if your obeject's hash code is never going to change throughout the life of the object. I was letting the value change, so I violated this rule.

I'm not sure exactly what to do about object equality now. What I've been doing lately is ignoring it all together in favor of comparing some key value if I need to test for that kind of equality.

-Brendan
# March 1, 2005 4:38 PM

Eric Wise said:

We all "evolve" over time Brenden. I constantly find things in my old code that make me roll my eyes.
# March 1, 2005 6:44 PM

Sam said:

That behaviour comes from overriding .GetHashCode() though right? I know the compiler warns you to override .GetHashCode() when you override .Equals(object,object), but could you enlighten me about why:

override GetHashCode() { base.GetHashCode(); }

Doesn't suffice? You can still override .Equals() and the operators you want, and just not mess with .GetHashCode then no? Yes?
# March 1, 2005 7:18 PM

Brendan Tompkins said:

Hi Sam,

Good question, although I think the intent of the compiler warning is that if you change what equals means, you should change your hash codes too. It's kind of a catch 22, because then anything you're using to determine equality can't change in the lifetime of your object.

So say you have the following object:

[AirCode]
public class Person ()
{
public string FirstName;
public string LastName;

override bool Equals(object o)
{
return (o != null && o is Person && (Person)o.FirstName == this.FirstName && (Person)o.LastName == this.LastName);

}

override int GetHashCode()
{
return base.GetHashCode();
}
}

[AirCode, Test]
public void TestEquality()
{
Person person1 = new Person();
person1.FirstName = "Sam";
person1.LastName = "Coder";

Person person2 = new Person();
person2.FirstName = "Sam";
person2.LastName = "Coder";

if(person1.Equals(person2))
Trace.WriteLine("They're equal!");

ArrayList al = new ArrayList();
al.Add(person1);

if(al.Contains(person2))
Trace.WriteLine("Don't add Sam Again");
else
al.Add(person2);
}

If you actually did this, you'd end up with two Person Objects in your ArrayList both "Equal", since their hash codes are different. This wouldn't be what you would want, right? So to fix this, you'd override GetHashCode() to do somethign like this:

override int GetHashCode()
{
return this.FirstName.GetHashCode() ^ this.LastName.GetHashCode();
}

But if you did this, you could never let a person's first or last name change, in the lifetime of an object. This may be okay for a person, but could be limiting in other situations.

Perhaps this will generate some more discussion on this. I'm not 100% clear, like I said before.

# March 2, 2005 7:18 AM

Sam said:

Oooooooh. Ok, thanks for clearing that up for me.
# March 2, 2005 7:50 AM

Sahil Malik said:

That looks nice, but why a pink box?
# March 2, 2005 7:55 AM

Brendan Tompkins said:

That's not pink dammit! It's light puce!
# March 2, 2005 8:06 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

This Blog

Syndication

News

MVP
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.