Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #26949

Re: Inheritance

From Bernd Paysan <bernd.paysan@gmx.de>
Newsgroups comp.lang.forth
Subject Re: Inheritance
Date 2013-11-25 20:01 +0100
Organization 1&1 Internet AG
Message-ID <l706q6$c5o$1@online.de> (permalink)
References <52933ba3$0$294$14726298@news.sunsite.dk>

Show all headers | View raw


Doug Hoffman wrote:

> I have been reading that inheritance is falling from favor when using
> OOP.  I agree that inheritance overuse can lead to harder to
> read/understand/debug code.  Of course the use of inheritance is
> completely controlled by the programmer.  Seems to me that using
> inheritance more sparingly should lead to the following:
> 
> 1) One should embed objects as instance variables (the "has a" paradigm)
> instead of inheriting (the "is a" paradigm).  At least do so more often
> when needing to add behavior to an object and that behavior already
> exists in another class.  Sometimes, though, using shallow inheritance
> is the best way to go because more "pass-through" methods need not then
> be defined.
> 
> 2) Late binding to self (aka open recursion) is only used with
> inheritance.  So if we are not using inheritance as much then the
> default for messages to self should be an early bind for speed.  Late
> binding can still be used when wanted (use a syntax such as [self]).
> 
> 3) Early binding to self should make using inheritance easier and less
> prone to bugs.  Not to say that late binding to self(inheritance) should
> never be used.  Sometimes it is perfect and greatly enhances code reuse,
> but again only with inheritance.
> 
> 4) Messages sent to objects-as-instance-variables should always be early
> bound, again for speed.  The class compiler can do this transparently
> because the class of the object is known at compile time and the class
> of the embedded object will not change.  Of course objects in a
> *container* instance variable could be of any class and so late binding
> is then required (also done transparently).  No extra effort needed from
> the programmer either way.
> 
> The above techniques will result in object-oriented code that is
> noticeably faster and easier to write and maintain.

You forgot one important thing, that is the "combination" stuff.  You might 
not have deep inheritance trees, but combine different objects into one.

E.g.: In MINOS, every widget has an actor.  An actor is a class that gets 
called for actions, like clicks or keystrokes.  There are not too many actor 
classes with the same interface; usually they don't inherit from each other 
(so the hierarchy is flat).

Those actors are not instance variables, but instance pointers.  Calling 
them absolutely requires polymorphism.

The typical case I found for calling with self are "template methods", i.e. 
methods that do something that is filled further down in the inheritance 
tree.  That also absolutely requires late binding.

I don't understand why you are so obsessed with early binding, and argue 
with speed.  Contemporary CPUs do quite well on vtable dispatches with their 
branch target buffers.  They don't do well with a central dispatcher that is 
more complicated than a simple vtable call, so if you want speed, just make 
sure that you can implement your system using a vtable.  The case where you 
actually don't have polymorphism (same method called each time) is as fast 
as the direct call on a Core i7, so this particular case of optimization has 
aleady been done by the CPU manufacturer.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Inheritance Doug Hoffman <glidedog@gmail.com> - 2013-11-25 06:59 -0500
  Re: Inheritance Bernd Paysan <bernd.paysan@gmx.de> - 2013-11-25 20:01 +0100
    Re: Inheritance Doug Hoffman <glidedog@gmail.com> - 2013-11-26 05:11 -0500
      Re: Inheritance Bernd Paysan <bernd.paysan@gmx.de> - 2013-11-26 22:09 +0100
    Re: Inheritance the_gavino_himself <visphatesjava@gmail.com> - 2013-11-27 15:25 -0800
      Re: Inheritance Bernd Paysan <bernd.paysan@gmx.de> - 2013-11-28 01:39 +0100
        Re: Inheritance Mark Wills <markrobertwills@yahoo.co.uk> - 2013-11-28 01:53 -0800
  Re: Inheritance Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-11-25 15:28 -0600
    Re: Inheritance Doug Hoffman <glidedog@gmail.com> - 2013-11-26 05:11 -0500
      Re: Inheritance Bernd Paysan <bernd.paysan@gmx.de> - 2013-11-26 22:28 +0100
        Re: Inheritance Doug Hoffman <glidedog@gmail.com> - 2013-11-26 17:12 -0500
          Re: Inheritance Bernd Paysan <bernd.paysan@gmx.de> - 2013-11-27 23:07 +0100
      Re: Inheritance Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-11-26 17:11 -0600
        Re: Inheritance Doug Hoffman <glidedog@gmail.com> - 2013-11-27 06:25 -0500
          Re: Inheritance Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-11-27 11:58 -0600

csiph-web