The Artima Developer Community
Sponsored Link

This is Rediculous
Trust No One
by Rick Kitts
February 3, 2004
Summary
Is that code doing what you think it's doing? Are you sure?

Advertisement

I ran into some code the other day that looked something like this:

public void methodOne()
{
 Bar aBar = Foo.getInstance().getBar();
 if(aBar != null){
  // Do Bar type stuff to aBar
 }
}

public void methodTwo()
{
 Bar aBar = Foo.getInstance().getBar();
 // Do Bar stuff to aBar
}

Looking at it this way it's pretty obvious what's going one, but in the specific case I'm talking about methodTwo was maybe 40 or so lines away in the file. So if you didn't write the code it's pretty easy to miss this on a casual fly over. I was really surprised to find this since it seems to fly in the face of one of the first things I discovered when I was cutting my teeth; trust no one. The implementor of the above code trusted that the code would never return null. The fact that methodOne checks the return of getBar() and method two doesn't is probably explained by semantics of the system not visible in the snippet given. That is a different topic though.

It's probably hard to count the number of times I've been scratched, clawed or outright bitten by trusting code. This was true even when I was operating under classic (some might say antique) development processes where I was the only one mucking about in a chunk of code. Trusting yourself is almost always a bad thing too. In an agile environment code I wrote today can quickly be changed tomorrow by someone else, and without my knowledge.

For myself I've placed a fair amount of stock in tools and techniques used in the design by contract (DBC) process (is it a process?). So I try to check all of my inputs for reasonable values. I always evaluate, at least in my head, what it would mean if I got back an unexpected value from a method call and add mitigating code. Metaphorically I've found that treating code like I somtimes treat my kids works well. If you have kids this is probably a familiar conversation:

You:Did you eat all the cookies?
Kid:Not me! Umm, I was dead at the time.
You:Are you sure? Aren't those cookie crumbs around your mouth?
Kid:Oh. Yah, I did eat the the cookies. I forgot.

If I run into a bug and find all the cookies gone, I at least know the first few questions to ask. Following the XP tenet of "if it works do it all the time", asking where my cookies are has proven valuable to me over the years and is probably worth doing for you to. Trust me.

Talk Back!

Have an opinion? Readers have already posted 10 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Rick Kitts adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Rick Kitts has been making a living writing software for a little while. He's started a company or two, worked at bigger companies, but mostly at startups. Constantly on the look out for things to help him build better systems he's a bit of a tool and process slut, though he can't bring himself to try C# or get serious about UML. Go figure. He's convinced being invited to have a weblog on Artima is the result of some glitch in the matrix. He's keeping quiet about it though.

This weblog entry is Copyright © 2004 Rick Kitts. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use