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


Groups > comp.programming > #4793 > unrolled thread

Two object oriented methods, which is better?

Started byVictor Porton <porton@narod.ru>
First post2014-09-18 21:40 +0300
Last post2014-09-18 19:24 +0000
Articles 6 — 3 participants

Back to article view | Back to comp.programming


Contents

  Two object oriented methods, which is better? Victor Porton <porton@narod.ru> - 2014-09-18 21:40 +0300
    Re: Two object oriented methods, which is better? Melzzzzz <mel@zzzzz.com> - 2014-09-18 20:46 +0200
      Re: Two object oriented methods, which is better? Kaz Kylheku <kaz@kylheku.com> - 2014-09-18 18:59 +0000
        Re: Two object oriented methods, which is better? Melzzzzz <mel@zzzzz.com> - 2014-09-18 21:08 +0200
          Re: Two object oriented methods, which is better? Kaz Kylheku <kaz@kylheku.com> - 2014-09-18 19:26 +0000
    Re: Two object oriented methods, which is better? Kaz Kylheku <kaz@kylheku.com> - 2014-09-18 19:24 +0000

#4793 — Two object oriented methods, which is better?

FromVictor Porton <porton@narod.ru>
Date2014-09-18 21:40 +0300
SubjectTwo object oriented methods, which is better?
Message-ID<lvf8uf$o09$2@speranza.aioe.org>
[Sorry for multiple postings, but traffic of comp.object is so low (as I 
found after my posting there), that almost nobody will suffer of my multiple 
posting.]

If I want to push a specific (for a given subclass) value into an array 
argument of a method, should I just pass the value of the argument to a 
method which would modify it, or should I create a method which just 
returns 
the list of additional values (and use it in the first mentioned method)?

Pseudocode (with "#" starting comments) for a method f:

# modify_array is a dynamic method
function f(object, array) {
  object->modify_array(array);
}

or

# common_array_members is a dynamic method
function f(object, array) {
  array = concat(array, object->common_array_members());
}

What is more object oriented? Which is better?

(We program in Perl.)

The array is an array of images which we attach to a email message. We 
always attach some images, but these images are different for different 
subclasses. At the moment, the order of elements of the array is probably 
not important.

-- 
Victor Porton - http://portonvictor.org

[toc] | [next] | [standalone]


#4794

FromMelzzzzz <mel@zzzzz.com>
Date2014-09-18 20:46 +0200
Message-ID<20140918204616.7a953d26@maxa-pc>
In reply to#4793
On Thu, 18 Sep 2014 21:40:19 +0300
Victor Porton <porton@narod.ru> wrote:

> [Sorry for multiple postings, but traffic of comp.object is so low
> (as I found after my posting there), that almost nobody will suffer
> of my multiple posting.]
> 
> If I want to push a specific (for a given subclass) value into an
> array argument of a method, should I just pass the value of the
> argument to a method which would modify it, or should I create a
> method which just returns 
> the list of additional values (and use it in the first mentioned
> method)?
> 
> Pseudocode (with "#" starting comments) for a method f:
> 
> # modify_array is a dynamic method
> function f(object, array) {
>   object->modify_array(array);
> }
> 
> or
> 
> # common_array_members is a dynamic method
> function f(object, array) {
>   array = concat(array, object->common_array_members());
> }
> 
> What is more object oriented? Which is better?

First can do whatever with array, while second shows clear
intent.
I'll go with second since it is clearer what function does.

[toc] | [prev] | [next] | [standalone]


#4795

FromKaz Kylheku <kaz@kylheku.com>
Date2014-09-18 18:59 +0000
Message-ID<20140918115849.501@kylheku.com>
In reply to#4794
On 2014-09-18, Melzzzzz <mel@zzzzz.com> wrote:
> On Thu, 18 Sep 2014 21:40:19 +0300
> Victor Porton <porton@narod.ru> wrote:
>> What is more object oriented? Which is better?
>
> First can do whatever with array, while second shows clear
> intent.
> I'll go with second since it is clearer what function does.

It's probably also clear what modify_array does, if you have its definition.

[toc] | [prev] | [next] | [standalone]


#4796

FromMelzzzzz <mel@zzzzz.com>
Date2014-09-18 21:08 +0200
Message-ID<20140918210821.2442a52a@maxa-pc>
In reply to#4795
On Thu, 18 Sep 2014 18:59:34 +0000 (UTC)
Kaz Kylheku <kaz@kylheku.com> wrote:

> On 2014-09-18, Melzzzzz <mel@zzzzz.com> wrote:
> > On Thu, 18 Sep 2014 21:40:19 +0300
> > Victor Porton <porton@narod.ru> wrote:
> >> What is more object oriented? Which is better?
> >
> > First can do whatever with array, while second shows clear
> > intent.
> > I'll go with second since it is clearer what function does.
> 
> It's probably also clear what modify_array does, if you have its
> definition.

Problem is that modify_array is "dynamic method", what I guess means
that one probably don;t know what it does ;)

[toc] | [prev] | [next] | [standalone]


#4798

FromKaz Kylheku <kaz@kylheku.com>
Date2014-09-18 19:26 +0000
Message-ID<20140918122609.553@kylheku.com>
In reply to#4796
On 2014-09-18, Melzzzzz <mel@zzzzz.com> wrote:
> On Thu, 18 Sep 2014 18:59:34 +0000 (UTC)
> Kaz Kylheku <kaz@kylheku.com> wrote:
>
>> On 2014-09-18, Melzzzzz <mel@zzzzz.com> wrote:
>> > On Thu, 18 Sep 2014 21:40:19 +0300
>> > Victor Porton <porton@narod.ru> wrote:
>> >> What is more object oriented? Which is better?
>> >
>> > First can do whatever with array, while second shows clear
>> > intent.
>> > I'll go with second since it is clearer what function does.
>> 
>> It's probably also clear what modify_array does, if you have its
>> definition.
>
> Problem is that modify_array is "dynamic method", what I guess means
> that one probably don;t know what it does ;)

Yes; --- but "I don't know WTF this does" is the essence of object-oriented! :)

[toc] | [prev] | [next] | [standalone]


#4797

FromKaz Kylheku <kaz@kylheku.com>
Date2014-09-18 19:24 +0000
Message-ID<20140918115941.364@kylheku.com>
In reply to#4793
On 2014-09-18, Victor Porton <porton@narod.ru> wrote:
> [Sorry for multiple postings, but traffic of comp.object is so low (as I 
> found after my posting there), that almost nobody will suffer of my multiple 
> posting.]
>
> If I want to push a specific (for a given subclass) value into an array 
> argument of a method, should I just pass the value of the argument to a 
> method which would modify it, or should I create a method which just 
> returns 
> the list of additional values (and use it in the first mentioned method)?
>
> Pseudocode (with "#" starting comments) for a method f:
>
> # modify_array is a dynamic method
> function f(object, array) {
>   object->modify_array(array);
> }
>
> or
>
> # common_array_members is a dynamic method
> function f(object, array) {
>   array = concat(array, object->common_array_members());
> }
>
> What is more object oriented? Which is better?

The second is more functional since it avoids destructively manpulating the
array object. (It assigns to a local variable, but that is a secondary concern
and removable.)

The second approach might be seen as less OO, because the responsibility for
the "modify_array" logic is in a non-OO function. The implementation of object
can only vary what it returns out of common_array_members, but has no say in
what modification is done with it.

However, that is just a perspective. We don't have to divide the world
into "regular function" and "OO function". If we regard f to be an OO
function, then this goes away.

We have a functional abstraction f(obj, array), which does something
appropriate based on the type of obj.  The fact that f is a wrapper
which invokes methods and performs some of the logic is immaterial to the
caller. When the object framework grows more complicatd, the logic
which is done in f can be moved into a method, so that f becomes
a pure method wrapper.

This kind of thing is epecially of no concern in a language which doesn't
stupidly use different syntax for calling OO methods and regular functions;
f(obj, a) could start out being a regular function, and later be upgraded to an
OO method if necessary. The programmer doesn't have to scan the program
for f(x,y) syntax and convert to x.f(y); f(x,y) is the object-oriented call.

Also the functional aspect and OO aspect are orthogonal, because you can
have this:

 function f(object, array) {
   local new_array = object->generate_array(array); # do not modify
   ...
 }

The responsibility for how the objectg and array are combined rests in
the method, and clobbering the array is avoided.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming


csiph-web