<?xml version="1.0" encoding="windows-1252"?>
<node id="515650" title="Anti-inside-out-object-ism" created="2005-12-09 15:56:15" updated="2005-12-09 10:56:15">
<type id="120">
perlmeditation</type>
<author id="313108">
jdhedden</author>
<data>
<field name="doctext">
In the discussion concerning [id://514605], several monks expressed various
reasons for their reluctance to use inside-out objects.  Here's the list I
gathered from that thread:
&lt;p&gt;
I don't want to use inside-out objects because:
&lt;ol&gt;&lt;li&gt;
Hash-based objects are the standard.  Using a different object API may lead to
maintainability issues.
&lt;/li&gt;&lt;li&gt;
Inside-out object classes aren't compatible with hash-based object classes.
&lt;/li&gt;&lt;li&gt;
Inside-out objects are just another kludge on top of Perl's already kludgy OO
mechanisms.
&lt;/li&gt;&lt;li&gt;
I haven't had any problems using hash-based objects.  The encapsulation and
compile-time checking advantages of inside-out objects aren't important enough
to induce me to use them.
&lt;/li&gt;&lt;li&gt;
The encapsulation provided by inside-out objects is unenforcable because the
source code is available.
&lt;/li&gt;&lt;li&gt;
Inside-out objects require a DESTROY method, and I don't what to have to
remember to write one each time I create an inside-out class.
&lt;/li&gt;&lt;li&gt;
There are too many alternative inside-out object support modules that aren't
compatible with each other.
&lt;/li&gt;&lt;li&gt;
I'm leery of the 'magic' the inside-out object support modules do 'under the
covers'.  There may be bugs or they may lead to unexpected problems when
interacting with other modules.
&lt;/li&gt;&lt;li&gt;
I tried module Foo::Bar, and had problems with it so I gave up on trying to
use inside-out objects.
&lt;/li&gt;&lt;li&gt;
You can't serialize inside-out objects either in general, or using
Data::Dumper or Storable.
&lt;/li&gt;&lt;li&gt;
Inside-out objects are not thread-safe because they usually use
&lt;code&gt;refaddr&lt;/code&gt; as the key for storing object data.
&lt;/li&gt;&lt;/ol&gt;

Other monks were able to address some of these concerns, and I have summarized
their responses, as well as adding some thoughs of my own.
&lt;readmore&gt;  Further, as the
author of [mod://Object::InsideOut], I will speak to specifics concerning that
module where appropriate.

&lt;blockquote&gt;
Hash-based objects are the standard.  Using a different object API may lead to
maintainability issues.
&lt;/blockquote&gt;

The argument is basically that all Perl programmer's know how to use
hash-based objects, and that if I use a support module like
[mod://Object::InsideOut], then anyone else that tries to read/maintain my
code will have to learn about inside-out objects, in general, as well as the
specifics of the support module.
This is obviously something to consider when looking at using inside-out
objects, and needs to be weighed against inside-out objects' advantages.
&lt;p&gt;
As with any new paradigm, it will take time for inside-out objects to become
generally accepted, however, I am confident that they will.  Their inclusion
in PBP will definitely speed up the process.  Eventually, someone will write a
tutorial on inside-out objects for inclusion with the main Perl distribution.
Thus, as time goes by, the argument above will start to lose some of its
weight.

&lt;blockquote&gt;
Inside-out object classes aren't compatible with hash-based object classes.
&lt;/blockquote&gt;
They aren't compatible in the same way as other hash-based object classes,
true, but that doesn't mean you can't work with them at all.
&lt;p&gt;
One simple approach to inheriting from hash-based classes is to use the
hash-based object as is, and then use its &lt;code&gt;refaddr&lt;/code&gt; as the key for
storing data inside the derived class.  Some inside-out object support modules
use this approach.  However, this approach doesn't support multiple
inheritance, and the use of &lt;code&gt;refaddr&lt;/code&gt; requires addition code for
thread-safety (given that the hash-based class must be thread-safe as well).
&lt;p&gt;
[mod://Object::InsideOut] has recently been enhanced to support subclassing of
hash-based object classes (referred to as &lt;i&gt;foreign inheritance&lt;/i&gt; in its
docs), and it does support multiple inheritance.  Further,
[mod://Object::InsideOut] is thread-safe.

&lt;blockquote&gt;
Inside-out objects are just another kludge on top of Perl's already kludgy OO
mechanisms.
&lt;/blockquote&gt;

Well, Perl never claims to be a &lt;i&gt;pure OO language&lt;/i&gt;, and arguments as to
its merits are rather pointless.  It is what it is.  Use it, or don't.  My
personal stand follows from the &lt;i&gt;P&lt;/i&gt; in Perl standing for
&lt;i&gt;practical&lt;/i&gt;:  Perl's OO interface gets the job done when it's needed.  I
find the ability to mix OO and procedural programming, as needed, to be one of
Perl's greatest strengths.

&lt;p&gt;
As to inside-out objects being another kludge, I would argue against the use
of that label.  Inside-out objects is just another paradigm.  They use
standard Perl to do the same job but in a different manner, complete with its
own strengths and weaknesses.

&lt;blockquote&gt;
I haven't had any problems using hash-based objects.  The encapsulation and
compile-time checking advantages of inside-out objects aren't important enough
to induce me to use them.
&lt;/blockquote&gt;

To be clear, &lt;i&gt;encapsulation&lt;/i&gt; for inside-out objects prevents the
overwriting of hash-keys that can occur with inheritance in hash-based object
classes.
&lt;p&gt;
The &lt;i&gt;compile-time checking&lt;/i&gt; comes about because the fields in inside-out
objects are hashes or arrays, and not hash-keys.  Misspelled hash-keys are
not caught at compile-time, and may or may not be caught at runtime.
Misspelled hash or array names are caught at compile-time.  
&lt;p&gt;
Now for smaller systems, these advantages may be of less importance to a
single programmer.  However, for larger projects with multiple coders, these
advantages should be given much more thought.  In fact, inside-out objects
resulted from considering how to overcome just these problems with hash-based
objects.  Obviously, they are very important to some.

&lt;blockquote&gt;
The encapsulation provided by inside-out objects is unenforcable because the
source code is available.
&lt;/blockquote&gt;

It is true that deliberate circumvention of any OO system is possible given
the source code.  If such totalitarian controls are a requirement of your
system, then perhaps you need to consider using something other than Perl.
&lt;p&gt;
The point of encapsulation with inside-out objects is to prevent the
collision of data (i.e., hash-keys) that can occur with hash-based object
classes.  It adds safety, but doesn't prevent deliberate circumvention.

&lt;blockquote&gt;
Inside-out objects require a DESTROY method, and I don't what to have to
remember to write one each time I create an inside-out class.
&lt;/blockquote&gt;

Various inside-out object support modules, including
[mod://Object::InsideOut], handle this for you automatically, so this becomes
a non-issue if you use one of them.

&lt;blockquote&gt;
There are too many alternative inside-out object support modules that aren't
compatible with each other.
&lt;/blockquote&gt;

This criticism can be applied to just about any functionality for which CPAN
modules have been written:  CPAN and CPANPLUS; ExtUtils::MakeMaker,
Module::Build and Module::Install; and more.

&lt;p&gt;

As with anything, as time goes on, certain implementations will become more
accepted than others depending on their features, robustness, support, and so
on.  Personally, I have been putting in a lot of effort in all these area to
make [mod://Object::InsideOut] as usable as possible to the Perl community.

&lt;blockquote&gt;
I'm leery of the 'magic' the inside-out object support modules do 'under the
covers'.  There may be bugs or they may lead to unexpected problems when
interacting with other modules.
&lt;/blockquote&gt;

With the source code available, you can see what is being done, if needed.
However, as I see it &lt;i&gt;the proof is in the pudding&lt;/i&gt;.  If the support
modules does what you want it to and thereby meets your needs without causing
problems and incompatibilities, then what difference does it make how it does
it.

&lt;p&gt;

As for bugs, a good test suite and good author support lessen the
probability and potential impact of encountering problems in your own
development.  For example, there are currently no open bugs for
[mod://Object::InsideOut], and any reported bugs have been corrected in less
than one day.

&lt;p&gt;

Likewise, incompatibilites can be overcome with good author support.  For example, when I first released [mod://Object::InsideOut], I was informed that having a CHECK block would make it incompatible with [mod://mod_perl].  Within a day, I posted an new version that eliminated this incompatibility.

&lt;blockquote&gt;
I tried module Foo::Bar, and had problems with it so I gave up on trying to
use inside-out objects.
&lt;/blockquote&gt;

This sort of thing can happen with any module.  Giving up is not the answer,
however.  If you weren't able to get support from the module's author, you
could try another.  Obviously, you were interested enough to look at
inside-out objects in the first place.  A bad module doesn't mean the
underlying concept (i.e., inside-out objects) is bad.

&lt;p&gt;

As you might guess, I would recommend giving [mod://Object::InsideOut] a try.
It has a &lt;a href="http://www.cpanforum.com/dist/Object-InsideOut"&gt;discussion
forum&lt;/a&gt; on CPAN, and active author support.

&lt;blockquote&gt;
You can't serialize inside-out objects either in general, or using
Data::Dumper or Storable.
&lt;/blockquote&gt;

It is true that inside-out objects do not automatically support serialization.
Once again, however, some support modules do provide serialization
capabilities.  For example, [mod://Object::InsideOut] does provide object
serialization and deserialization capabilites, and has recently been enhanced
to support serialization using the [mod://Storable] module.

&lt;blockquote&gt;
Inside-out objects are not thread-safe because they usually use
&lt;code&gt;refaddr&lt;/code&gt; as the key for storing object data.
&lt;/blockquote&gt;

Making inside-out objects thread-safe is not a trivial process, but it can be
done.  [mod://Object::InsideOut] is thread-safe, and even goes further by
providing support for [mod://threads::shared].

&lt;p&gt;
&lt;I&gt;In conclusion:&lt;/I&gt;&lt;br&gt;
I agree that inside-out objects may not be for everyone, however, I hope that
the discussion above (and what will follow) will help others in making an
informed decision on the subject.
&lt;/readmore&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-313108"&gt;
&lt;hr&gt;&lt;font size="-2"&gt;Remember:&amp;nbsp;There's always one more bug.&lt;/font&gt;
&lt;/div&gt;&lt;/div&gt;
</field>
</data>
</node>
