[Israel.pm] An OOP Pattern Discovered by Accident

Oded Arbel oded-linux at typo.co.il
Sun Jan 28 05:13:54 PST 2007


On Sun, 2007-01-28 at 14:41 +0200, Shlomi Fish wrote:
> Hi Oded! And All.
> 
> On 1/28/07, Oded Arbel <oded-linux at typo.co.il> wrote:
> > On Sat, 2007-01-27 at 23:04 +0200, Shlomi Fish wrote:
> > > > Eh. That's the one thing I'm missing when comparing perl to Java :
> > > > automated refactoring.
> > >
> > > Well, there's a difference between automated refactoring and automated code
> > > generation, which is what you've mentioned before.
> >
> > Nitpicking.
> >
> 
> Actually there is a difference. In order to automatically refactor
> code one must analyse the existing code 

Not much - refactoring is a type of code generation (albeit it also
changes existing code in addition to generating new code - so you can
say that automatic code generation is a subset of automatic
refactoring). Automatic code generation is Eclipse does require analysis
as the following example will show.

> > while I'm
> > looking for something more.. how shall I say it... ehmm... automatic.
> > Hey! that's the word I used before, I appear to be repeating myself :-(
> 
> It would be helpful if you explained how Eclipse's code generation is
> more "automatic". Do you give it a function as a template and then can
> generate other functions based on it.

For example, for the delegate pattern discussed previously, I choose
"generated delegate methods" from a menu, and eclipse pulls up an
interactive dialog with a list of possible contained objects to which
I'd wont to delegate. I then select the objects I want to delegate to
(with the option of fine tuning by selecting only specific methods that
will be delegated) and hit OK. Eclipse then adds delegate methods
automatically in the form:
<method signature> {
	return m_delegate.<method signature>
}

The template can be modified as all other templates in Eclipse, but I
never had to bother with it - its simple enough that it works w/o
twiddling. Other code generation routines (implement interfaces for
example) work similarly or w/o the need for additional user
interaction. 
Its automatic in the sense that I don't have to write a single line of
code in order to generate a lot of code.

> From a purist POV, they would dislike the idea of having such
> duplicate code even if the IDE generates and manages it.

Agreed, but then there will always be purists and we don't have to take
their pov into consideration, knowing that its based only on
conservatism. see edlin vs. vi.

> My perl example can be encapsulated in a module and
> then one can say something along the lines of:
> 
> use base 'Class::MakeDelegates';
> 
> __PACKAGE__->mk_delegate($delegate_to_property, [qw(method1
> second_method write_line)]);
> 
> To generate all the delegated methods.

Then it would be great if you do us the service of submitting it to
CPAN.

--
Oded
::..
Humans are communications junkies. We just can't get enough.
    -- Alan Kay





More information about the Perl mailing list