Jump to content

The use of Method Cahining


Baronen

Recommended Posts

Hey Guys.

 

I'm a PHP-developer from Sweden who is new at phpfreaks.com, and so far i'm liking it.

 

At my Swedish blog I have had a discussion with a .NET developer who says that the use of Method Chaining is "bad practice" and makes the code hard to read.

 

I use chains in my projects and personally I like them i think the code is easier to read.

 

What do you guys think about Method Chaining, "bad practice", hard to read?, better perfomance?

Link to comment
Share on other sites

I haven't read the blog, but if I had to guess I'd say their may be some confusion between you and the .NET developer about what "method chaining" is.

 

At the one side there's method chaining as used in ZF, also known as "privoding a fluent interface", then there's what has been dubbed (by someone at ThoughtWorks, don't remember who), a "train wreck".

 

This is a typical fluent interface:

 

$foo
  ->setBar(1)
  ->setBlah(2)
  ->execute();

 

This is a train wreck:

 

$foo
  ->getBar()
  ->getBlag()
  ->execute();

 

I'm not implying that the difference between fluent interface and trian wreck is as simple as mutator vs accessor, there's obviously something else going on: example 2 fetches deep into the structure behind $foo, ignoring implied encapsulation, and violating the "tell don't ask" heuristic. Example 1 just operates on the same object.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.