Hi! Please consider following me on twitter: @hanekomu.

In defense of polymorphism

Laurent Dami blogged Object-oriented accessors considered (sometimes) harmful and Yuval Kogman responded to it.

I agree that accessing hash elements directly can be tempting, but for me the killer argument in favor of accessor methods is polymorphism. Simply put, if you access hash elements directly, you're effectively making it completely impossible for someone else to subclass your class, or for your own class to subclass another class later. That's all.

And I agree strongly with one of Laurent's criticisms: With accessors, you never know where things really happen, making them difficult to debug. To use Laurent's example:

if ($c->request->body->length < $self->min_body) {

You have to step through a lot of methods to get to the right-hand side. It's the same with any expression.

What I would really like in the Perl debugger (but ENOTUITS) is the possibility to inspect the expression in a precedence-based tree-view like this:

1: <
2:    $c
3:        ->request
4:            ->body
5:                ->length
6:    $self
7:        ->min_body

and then being able to set a one-time breakpoint for line 7, that is, when entering the min_body() method.

Write a comment | Bookmark and Share

posted at: 13:29 | path: /dev | permalink | 1 comment | 0 trackbacks

Yuval Kogman wrote at 2009-08-16 11:59:

FWIW, B::Debugger demonstrates trapping at the opcode level. Using logic from B::Deparse that could be mapped into something like what you describe.

This is a fundamentally more flexible than DB:: API which is very unlikely to be fixed.

Comments are closed for this story.

Trackbacks are closed for this story.