On May 28, 2006, at 6:36 PM, John Carter wrote: > I have just noticed that my codeline has silent breakage all over the > place and I have an idea how to catch this sort of thing in future... > > How it happened is this.... > > I had a variable "platform" that I used all over the place. It was > a String. > > Then it had to become substantially smarter, so I made it an object > instance of class Platform. > > Unfortunately in quite a few places somehow I forgot to convert > if platform == 'thisone' > > to > if platform.name == 'thisone' > (or even better and simpler just.... > platform.doTheRightThing > ) > > > And... > > Ruby silently accepted my mistake, threw no error and took the > false branch. > > > So to catch that class of breakage I should do something like.... > > module PolymorphicallySafeObject > def ==(object) > raise "Strict Typing failure expected #{self.class} found # > {object.class}" unless > object.kind_of?( self.class) || self.kind_of?( object.class) > end > end a) unit tests would have caught this breakage for you. b) If you're going to be comparing objects it is best to define #== (or #<=> and include Comparable). class Platform def ==(o) case o when String then warn "comparing Platform with String on #{caller[1]}" # or raise name == o else name == o.name end end end > Question for the Group.... > ========================== > > What other useful cross checks and gotchas catchers could I include > in SafeObject? > > For extra marks, come up with a DuckSafeObject. There will always be one you didn't think of. Instead of adding these kinds of after-the-fact checks write unit tests. They provide more complete coverage with less time spent tracking down all your errors. -- Eric Hodel - drbrain / segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com