Fluent interfaces and code readability

March 30, 2008 – 1:28 pm

Fluent interfaces. Sigh. They help sometimes, they really do. In fact I don’t have anything against the idea, it’s the actual use that makes me uncomfortable.

Let’s start with the example first. The nicer one, that is. And to make my case stronger, I will use an example taken directly out of the blog of Mike Naberezny - Fluent Interfaces in PHP. Mike’s a renown PHP authority, so it should make you think:

    $customer->newOrder()
        ->with(6, 'TAL')
        ->with(3, 'LGV')
        ->with(5, 'HPK')->skippable()
        ->priorityRush();

It’s clean and pretty and sexy and all that, all right. You may be even tempted to say it’s more readable, but is that so in reality? I don’t think so. The problem here is you don’t know if skippable() method returns an order object or not. It may return something radically different like a totally different object or, even worse, a boolean. Furthermore, you can’t know what type of object the priorityRush() method is executed on. And THAT makes the code much harder to read.

You still don’t believe me? Take a look at the Mike’s blog and read a comment by FGM (4 Nov 06). Maybe that will open your eyes.

When one writes software only for himself it pays to type less. Some of the time, but still. When you know your code by heart and when you maintain the software yourself then there is a fat chance you will not be hindered by this. But in a corporate environment one writes software that is mainly read. By many different people. You write the software and then there are bug fixes, additional features, upgrades, etc. A lot of people have to browse your code in order to get a grasp of what the code in fact does. And then they modify. I assure you this is far more easier to comprehend later on:

        $order = $customer->newOrder();
        $order->with(6, 'TAL');
        $order->with(3, 'LGV');
        $order->with(5, 'HPK')->skippable();
        $order->priorityRush();

See? I have even left one use of fluent interface. It’s pretty clear that skippable() is executed on the object with() method returns. And it won’t make anyone browse through the “Order” source to understand. I agree, it is a little more to type and you have to sacrifice a couple of CPU cycles. But it pays when the code is forgotten and has to be modified later on.

And now the horrific use in my eyes:

        $view = new Zend_View();
        Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer')->setView($view);
        Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer')->view->doctype('XHTML1_TRANSITIONAL');

The major problem here is that the most important stuff is far on the right, so far you can loose focus before you get there (and even have to scroll it on my blog). Basically? It’s a readability hell. Not to mention it probably takes a lot more to type (216 key strikes). Oh, now you though Copy & Paste! Please, don’t get me started on that :)

Isn’t this way more readable?

        $view = new Zend_View();
        $view->doctype('XHTML1_TRANSITIONAL');

        $renderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $renderer->setView($view);

Well? You exactly see what’s happening now and important stuff is taken to the front so it’s easier to spot. The line is shorter so you will get the meaning quicker. Oh, and it’s 169 key strikes with additional white-space making it even better.

Don’t rape the fluent interfaces from now on, please.

  1. 5 Responses to “Fluent interfaces and code readability”

  2. I see your point to some extent, but using a good PHP IDE you are going to know return types when you mouse over the method call. Using PDT for eclipse I can work very well with fluent interfaces. So in fact you do know if skippable() returns a order object. Further, a well developed class providing a fluent interface should throw an exception if the return of skippable() was not an order object.

    Just my two cents, to me fluent interfaces are more readable.

    By Josh on Apr 2, 2008

  3. You are making two points, Josh:

    1. Object type at hand when using well designed IDE
    2. An error while compiling

    I’ll start with the latter since it’s easier - you don’t compile when you’re reading the code while looking for a bug, for example. I agree it errors out but it does so when it’s not really desired.

    Now, when it comes to IDEs, I see your point but hovering over a variable and waiting for a pop-up to emerge has it’s own price - you’re distracted. You read the code and then you have to stop, look for the information to show, read some text outside the code, then go back to what you were doing, focus again and finally go on. That’s a lot to do.

    Next thing is, you have a great IDE. I’m happy for you. But what about those times you’re helping somebody else who doesn’t have your IDE or uses something completely different? Or when you read the code on the deployed application or in version control? It happens all the time.

    I think you understand “readable” as “less cluttered”. And I agree with that if that’s the case. But when it comes to comprehending the code, it’s not always the case. And that was my original point.

    Anyways, thank you for your comment.

    By Michał Minicki on Apr 2, 2008

  4. are you suggesting people use notepad to develop? :P Both good points, and something to always consider when developing in a team environment.

    By Josh on Apr 2, 2008

  5. i’ve been using fluent interfacing without even knowing its already fluent interface… so i think, base from my point of view. that fluent interface is a great tool for making code understandable, and developer friendy..

    By pewski on May 22, 2008

  6. Actually, it’s “dereferencing” rather than “fluent interfaces” as pointed out by one of the readers (thanks Karol for our chat). Especially when it comes to the last example. I have used the term rather loosely and for a marketing gain.

    Anyways, I’m not advising not to use it - I’m against it when readability suffers. And when readability matters at the same time.

    By Michał Minicki on May 26, 2008

Post a Comment