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
https://forums.phpfreaks.com/topic/178125-the-use-of-method-cahining/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.