T
Trans
I have finally come to the conclusion that the non-localality of
instance variables, independent of attribute methods, is not good.
Under worse case scenarios, it can lead to amphibious programming --
class diving to find culprit IVs. What's the problem? On the surface,
it's that Rubyists treat instance variables as if they were local to
the class or module in which they are defined. And while we all know
they are not, consider how often you have seen documentation that lists
all of a classes/modules instance variables? Probably never, since we
can't look to RDoc for help. And since instance variables are
autoviviable, there's really no way for RDoc to even completely do so.
It's unfortunate. But what can we do? The fact is Ruby makes it seem
okay.
Now, maybe you're wondering what the problem is exactly, and, if it is
indeed such a problem, why hasn't it biten you before. Well, consider
the following.
class Amphibian
def initialize( amphibian_type )
@type = amphibian_type
end
def amphibian_type ; @type ; end
def hop
puts "And the #{amphibian_type} hops!"
end
end
class Toad < Amphibian
attr_reader :type
def initialize
super( 'Toad' )
@type = 'dry'
end
def hop
puts "And the #{type} #{amphibian_type} hops!"
end
end
class Frog < Amphibian
attr_reader :type
def initialize
super( 'Frog' )
@type = 'slimmy'
end
def hop
puts "And the #{type} #{amphibian_type} hops!"
end
end
Toad.new.hop
Frog.new.hop
produces
And the dry dry hops!
And the slimmy slimmy hops!
These things should not be hopping! Obviously, we got the wrong hoppers
b/c our Frog and Toad programmer did not know our "aqua team"
programmer used the @type instance variable. And therein lies the clue
as to why this doesn't often "bite". Ruby has a convention of naming
attributes the same as its instance variables. A convention pushed by
the use of the convenience methods attr, attr_reader, attr_writer and
attr_accessor. So, more often then not #type gets @type and #type= sets
@type. This convention (combined with the simple fact that there are a
many words to choose from) greatly reduces the likelihood of instance
variable name clash.
I can imagein that someone might come along and claim this to actually
be feature. --easily sharing instance variables between classes and
subclasses, w/o generally having to worry about banging inot one
another thanks a ingenious convention. Yea, well while it usually
works, its not something with which a critical applications developer
tends to feel terribly comfortable, esspecially when deploying to a
live scenario. And, yes, I realize, some of you will point out
test-first development. Sure, that can help, but that's not a good
excuse. You can use that argument to justify goto and using only global
variables. In the end, it's not really a feature at all, and leads to
less stable code.
instance variables, independent of attribute methods, is not good.
Under worse case scenarios, it can lead to amphibious programming --
class diving to find culprit IVs. What's the problem? On the surface,
it's that Rubyists treat instance variables as if they were local to
the class or module in which they are defined. And while we all know
they are not, consider how often you have seen documentation that lists
all of a classes/modules instance variables? Probably never, since we
can't look to RDoc for help. And since instance variables are
autoviviable, there's really no way for RDoc to even completely do so.
It's unfortunate. But what can we do? The fact is Ruby makes it seem
okay.
Now, maybe you're wondering what the problem is exactly, and, if it is
indeed such a problem, why hasn't it biten you before. Well, consider
the following.
class Amphibian
def initialize( amphibian_type )
@type = amphibian_type
end
def amphibian_type ; @type ; end
def hop
puts "And the #{amphibian_type} hops!"
end
end
class Toad < Amphibian
attr_reader :type
def initialize
super( 'Toad' )
@type = 'dry'
end
def hop
puts "And the #{type} #{amphibian_type} hops!"
end
end
class Frog < Amphibian
attr_reader :type
def initialize
super( 'Frog' )
@type = 'slimmy'
end
def hop
puts "And the #{type} #{amphibian_type} hops!"
end
end
Toad.new.hop
Frog.new.hop
produces
And the dry dry hops!
And the slimmy slimmy hops!
These things should not be hopping! Obviously, we got the wrong hoppers
b/c our Frog and Toad programmer did not know our "aqua team"
programmer used the @type instance variable. And therein lies the clue
as to why this doesn't often "bite". Ruby has a convention of naming
attributes the same as its instance variables. A convention pushed by
the use of the convenience methods attr, attr_reader, attr_writer and
attr_accessor. So, more often then not #type gets @type and #type= sets
@type. This convention (combined with the simple fact that there are a
many words to choose from) greatly reduces the likelihood of instance
variable name clash.
I can imagein that someone might come along and claim this to actually
be feature. --easily sharing instance variables between classes and
subclasses, w/o generally having to worry about banging inot one
another thanks a ingenious convention. Yea, well while it usually
works, its not something with which a critical applications developer
tends to feel terribly comfortable, esspecially when deploying to a
live scenario. And, yes, I realize, some of you will point out
test-first development. Sure, that can help, but that's not a good
excuse. You can use that argument to justify goto and using only global
variables. In the end, it's not really a feature at all, and leads to
less stable code.