I've been working on some code cleanup with Laconica, and it's making me think of an anti-pattern that really gets under my skin. I call it the ActionDoer anti-pattern, but there's probably a better name for it.
The anti-pattern is where you have a class with just one public method -- or, maybe, one major public method. You typically use the class by setting a bunch of attributes on an instance and then firing the method. Something like this (in PHP, because, hey, why not?):
$foo = new ActionDoer();
$foo->x = 10; $foo->y = 'bar'; $foo->z = 'baz';
$foo->doTheAction();
$result = $foo->result;
What bugs me about this is that it's just a long and tedious way of doing this:
$result = equivalent_function(10, 'bar', 'baz');
It's using OOPiness to simulate a basic part of structured programming: the function call. The ActionDoer does let you set defaults or ignore some parameters. But optional parameters, and keyword parameters, can usually do this more effectively.
Probably what bugs me most is that textually it most resembles hand-coded assembly language:
MOV ax, 10 MOV bx, 'bar' MOV cx, 'baz'
INT 21H
MOV result, dx
If we're simulating function calls, are we moving forward or backwards in computer science?
tags: oop programming anti-pattern




