Thursday, April 23, 2009

Object Oriented Antipattern

This is an antipattern I've seen at a couple places I've worked.

Considering that I keep seeing it, I feel the need to point it out and make a note: This is not OOP. This is a convoluted function call. The names have been changed to protect the guilty.


$newObject = MyObject::createObject(array(
"someStuff" => $datas,
"morestuff" => $moreDatas,
"typo" => $lolTypes,
"theKitchenSink" => KitchenSinkFactory::getInstance()
));

$newObject->doStuff();

$results = $newObject->getResults();


If that is the entirety of the API that one is expected to use from MyObject, then, really, it's just a function call pretending to be an object.

3 comments:

  1. That pattern would be useful if the object in question went off and did something asynchronously, so you could ask it to start and then come back later for the answer.

    Of course, that's obviously not what's going to happen in PHP!

    ReplyDelete
  2. @ghewgill: Hah. I've been in PHPland so long that thought didn't even occur to me. But those lines appear one after another in my script, so even if PHP supported threading/asynchronous calls, that's not what that pattern is being used for.

    ReplyDelete
  3. And by "my script" I mean "the script that I am contemplating updating", not "the script that I wrote". :p

    ReplyDelete