Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Final Notes

If you’ve been coding in Java for any substantial period, you probably know how to write an equals method. Why write tests for something you can code in your sleep?

First, I’ve seen many different ways to implement equals, including some bad ones that were very difficult to comprehend and maintain. TDD will help you drive out a proper implementation in a simple but rigorous fashion. Second, the implementation will be correct. I’ve encountered some equals methods that looked proper but were plain wrong. Using TDD, your equals method will be correct.

You could choose to ignore this advice, and blast out equals methods as well as other "simple" methods that you’re sure can’t break. I’m not so confident in my skills, having made enough dumb mistakes in my long programming career. I find it much simpler and safer to just think of tests as specifications that I always have to code. You’ll start getting into trouble as soon as you start trying to figure out what’s too simple to bother testing.

Next segment: "Testing Hash Codes." Meanwhile, here’s the code (source.zip) we’ve built in this installment.

  • + Share This
  • 🔖 Save To Your Account