SafeObject protection for standard gotcha's


#1

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

class Platform < SomeThing
include PolymorphicallySafeObject

.
.
.
.
end

Now it occurs to me this might be quite a useful trick.

For example catching failures to define an equal method if you define a
hash method
and vice versa.

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.

John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : removed_email_address@domain.invalid
New Zealand

Carter’s Clarification of Murphy’s Law.

“Things only ever go right so that they may go more spectacularly wrong
later.”

From this principle, all of life and physics may be deduced.


#2

On May 28, 2006, at 6:36 PM, John C. wrote:

And…
{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 H. - removed_email_address@domain.invalid - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com