To pass or to be?


(Tom Sawyer) #1

so i have a question realted to class inheritence: when a class inherits
from a superclass, is it good coding practice for the superclass to use
the childs instance variables? or should they be passed? for example:

TO PASS:

class A
def initialize(x)
puts x
end
end

class B < A
def initialize
@x = "hello world!"
super(@x)
end
end

OR TO BE:

class A
def initialize
puts @x
end
end

class B < A
def initialize
@x = "hello world!"
super
end
end

i don’t know why but i tend feel like its cool for the subclass to look
up at the superclass, but not for the superclass to look down. it just
seems funny, but i can place why. so i’m wondering what others think.

~transami

···


~transami

“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin


(Dave Thomas) #2

Tom Sawyer transami@transami.net writes:

so i have a question realted to class inheritence: when a class inherits
from a superclass, is it good coding practice for the superclass to use
the childs instance variables? or should they be passed? for example:

If a superclass accesses its child’s instance variables, then by
definition all children must have that variable (or the thing will
break). Therefore the variable should be factored into the parent
class.

Dave


(HAL 9000) #3

so i have a question realted to class inheritence: when a class inherits
from a superclass, is it good coding practice for the superclass to use
the childs instance variables? or should they be passed? for example:

I suggest:

class A
def initialize(x)
@x = x
puts x
end
end

class B < A
def initialize
super(“hello world!”)
end
end

i don’t know why but i tend feel like its cool for the subclass to look
up at the superclass, but not for the superclass to look down. it just
seems funny, but i can place why. so i’m wondering what others think.

I agree. It increases the coupling, for one thing.

Hal Fulton

···

----- Original Message -----
From: “Tom Sawyer” transami@transami.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, July 06, 2002 1:08 PM
Subject: to pass or to be?


(Tom Sawyer) #4

so your saying i should pass the variables?

···

On Sat, 2002-07-06 at 12:15, Dave Thomas wrote:

Tom Sawyer transami@transami.net writes:

so i have a question realted to class inheritence: when a class inherits
from a superclass, is it good coding practice for the superclass to use
the childs instance variables? or should they be passed? for example:

If a superclass accesses its child’s instance variables, then by
definition all children must have that variable (or the thing will
break). Therefore the variable should be factored into the parent
class.

Dave


~transami

“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin


(Dave Thomas) #5

Tom Sawyer transami@transami.net writes:

If a superclass accesses its child’s instance variables, then by
definition all children must have that variable (or the thing will
break). Therefore the variable should be factored into the parent
class.

so your saying i should pass the variables?

That fact that you have a parent class intimately tied to its
children’s implementations should be setting off alarm bells.

So, I’d revisit the design, asking myself “why do I have to be poking
around in my child classes this way”. I’d then look to factor out the
common functionality up into the parent class, making it independent
of its children.

Cheers

Dave