
On the Floor
Here's one from the "I-Banged-My-Head-Against-The-Wall-Trying-To-Figure-This-Out-So-I'll-Post-This-So-You-Don't-Have-To" Department:
I had a method that added a bunch of stuff to an array, then did some clean-up to it before returning it. The method initially had some working code in it like this:
def get_stuff
# collect stuff here...
stuff.uniq!
stuff.sort! { |x,y| x.list_order <=> y.list_order }
end
Then, I realized that the method shouldn't sort the array, so I deleted the last line, like so:
def get_stuff
# collect stuff here...
stuff.uniq!
end
At some point later, I noticed that my app was sometimes broken and sometimes worked fine, and then spent a bunch of time trying to figure out why. I got some other people involved and then Mark said, "Oh, I've done that before."
Can you see the annoyance in my code that Mark saw right away?
It turns out that Array::uniq! will return itself, i.e. a unique array ONLY IF IT ACTUALLY DID SOMETHING. Otherwise, it returns nil. So, when I deleted the line of code that did the sorting, I introduced the possibility that my method would sometimes return the value I was expecting, and sometimes not.
Arrgh.
Subscribe 
Follow us on
Twitter 
Archives
February 2012January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
June 2009
March 2009
January 2009
December 2008
November 2008
September 2008
August 2008









Comments
Add a Comment