# Ixnay on the Amphibious Programming

**URL:** <https://www.ruby-forum.com/t/ixnay-on-the-amphibious-programming/56602>\
**Category:** Ruby\
**Created:** [April 1, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/ixnay-on-the-amphibious-programming/56602 "2006-04-01T16:00:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 1, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/ixnay-on-the-amphibious-programming/56602/1 "2006-04-01T16:00:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 1, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/ixnay-on-the-amphibious-programming/56602/2 "2006-04-01T22:15:00Z")

</div>

On Saturday 01 April 2006 08:58 am, Trans wrote:

> And since instance variables are autoviviable, there’s really no way for  
> RDoc to even completely do so.

Can anybody explain (or point to an explanation) of autoviviable  
variables?

Randy K.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 1, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/ixnay-on-the-amphibious-programming/56602/3 "2006-04-01T22:30:15Z")

</div>

On Apr 1, 2006, at 3:11 PM, Randy K. wrote:

> Can anybody explain (or point to an explanation) of autoviviable  
> variables?

I think he means the fact that ivars don’t throw name errors (or no  
method errors) if they haven’t been assigned to yet, they just become  
nil by default.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 1, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/ixnay-on-the-amphibious-programming/56602/4 "2006-04-01T22:33:22Z")

</div>

On 4/1/06, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have finally come to the conclusion that the non-localality of
> 
> Wow, let us see if I got what you mean.

class General

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 1, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/ixnay-on-the-amphibious-programming/56602/5 "2006-04-01T23:59:02Z")

</div>

> I think he means the fact that ivars don’t throw name errors (or no  
> method errors) if they haven’t been assigned to yet, they just become  
> nil by default.

That’s right.

class X  
def yelp!  
@yelpling  
end  
end

X.new.yelp! #=\> nil

Just invoking @yelping brings it into existence. You do not need to  
assign it first, like you do local vars.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 1, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/ixnay-on-the-amphibious-programming/56602/6 "2006-04-01T22:58:07Z")

</div>

My appologies to the list, there is a keyboard shortcut sending mails  
without asking me, gotta find it. ☹

Did not come up with the _reasonable_ questions I thought I had on my  
mind.  
I hope there will be some interesting reponses, I am still _thinking_

…
