You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@click.apache.org by Malcolm Edgar <ma...@gmail.com> on 2010/05/17 14:51:12 UTC

Click and Method Chaining

Hi All,

I have been playing around with some new concepts for Click and been
writing code using method chaining. Its something I have argued
against for a long time, as its something I would never recommend
developers do in their application code because you can get the train
wreak anti-pattern.  However its an idea I have finally come around
to. I think it would be really handy if the Click control setter
method returned their instance, so that you could write code like the
following:

TextField passwordField = new
PasswordField("password").setMaxLength(20).setMinLength(5).setRequired(true);

Column column = new
Column("age").setFormat(""{0,number,#,###}").setTextAlign("right");

This would alleviate the requirement for using builder/factory
patterns in many instance, which are very useful in their own right.

Any thoughts?  Frederic Daoud :)

regards Malcolm Edgar

Re: Click and Method Chaining

Posted by Freddy Daoud <fd...@proinbox.com>.
Malcolm Edgar writes:

> writing code using method chaining. Its something I have argued
> against for a long time, as its something I would never recommend
> developers do in their application code because you can get the train
> wreak anti-pattern.

For the record, I don't think method chaining as I had suggested results
in the train wreck anti-pattern. In the chain of method calls, you are
setting several properties on the *same* object:

anObject.text("text").width(50).height(50);

The train wreck anti-pattern, on the other hand, is when you chain
methods to get the information you need, digging too deep inside your
model and becoming vulnerable to any changes to the model along the
chain:

employee.getDepartment().getManager().getOffice().getAddress().getZip()

Here you are dependent on too many *different* objects.

Just thought I'd set that straight, because I wouldn't want people
to think that implementing method chaining in some Click objects
would be done "despite" the train wreck anti-pattern. That is a
different case and indeed a bad practice to be avoided, absolutely.

Cheers,
Freddy



Re: Click and Method Chaining

Posted by Bob Schellink <sa...@gmail.com>.
On 28/05/2010 16:26, Adrian A. wrote:
> Wouldn't this be a better approach?
> https://issues.apache.org/jira/browse/CLK-565
> 
> i.e. to do the chaining only in the corresponding Builder classes,
> and to let the Click core API as it is?


While those patterns are useful they add another layer of complexity and don't fit well at the
framework level. ControlFactory for example will be tied with the application being built. If you
want all your fields to have a specific CSS class it could be encapsulated inside the factory.
CLK-565 would make for a good example on how to centralize control creation.

Regards

Bob

Re: Click and Method Chaining

Posted by "Adrian A." <a....@gmail.com>.
>> The question is, in which Click classes to do this, to improve the API
>> without bloating it.
>
>
> It's a juggle between bloat vs consistency vs practicality.
> I suggest:
>
> - html element attributes:
>    Control attribute(String name, String value);
>    Control style(String name, String style);
>    Control class(String name);
>
> - validation (this will depend on control types though)
>    TextField maxLenght(int length);
>    IntegerField maxValue(int maxValue);
>    DoubleField maxValue(double maxValue);
Wouldn't this be a better approach?
https://issues.apache.org/jira/browse/CLK-565

i.e. to do the chaining only in the corresponding Builder classes,
and to let the Click core API as it is?

Adrian.


Re: Click and Method Chaining

Posted by Bob Schellink <sa...@gmail.com>.
On 28/05/2010 00:33, Freddy Daoud wrote:
> 
> The question is, in which Click classes to do this, to improve the API
> without bloating it.


It's a juggle between bloat vs consistency vs practicality.

I suggest:

- html element attributes:
  Control attribute(String name, String value);
  Control style(String name, String style);
  Control class(String name);

- validation (this will depend on control types though)
  TextField maxLenght(int length);
  IntegerField maxValue(int maxValue);
  DoubleField maxValue(double maxValue);

Kind regards

Bob

Re: Click and Method Chaining

Posted by Freddy Daoud <fd...@proinbox.com>.
Hi all,

> > Any thoughts?  Frederic Daoud :)
> 
> I think Freddy had the idea of creating chaining methods without the
> "set" prefix in order to preserve the JavaBean setter notation. So
> the above example becomes:
> 
> TextField passwordField = new
> PasswordField("password").maxLength(20).minLength(5).required(true);
> 
> Column column = new
> Column("age").format("{0,number,#,###}").textAlign("right");

That is exactly right, and it is what I did in a little tool called
chainer[1] which I then used specifically to generate chainer methods
for use with Click :-)

So, indeed, a while back I was suggesting this enhancement, and I
still think it's a nice idea. Keep the setter methods as they are,
thus preserving the JavaBeans conventions, and add chainer methods
without the "set" prefix.

The question is, in which Click classes to do this, to improve the API
without bloating it.

Cheers,
Freddy

[1]:http://code.google.com/p/chainer/



Re: Click and Method Chaining

Posted by Bob Schellink <sa...@gmail.com>.
On 17/05/2010 22:51, Malcolm Edgar wrote:
> 
> TextField passwordField = new
> PasswordField("password").setMaxLength(20).setMinLength(5).setRequired(true);
> 
> Column column = new
> Column("age").setFormat(""{0,number,#,###}").setTextAlign("right");
> 
> This would alleviate the requirement for using builder/factory
> patterns in many instance, which are very useful in their own right.
> 
> Any thoughts?  Frederic Daoud :)


I think Freddy had the idea of creating chaining methods without the "set" prefix in order to
preserve the JavaBean setter notation. So the above example becomes:

TextField passwordField = new PasswordField("password").maxLength(20).minLength(5).required(true);

Column column = new Column("age").format(""{0,number,#,###}").textAlign("right");

Kind regards

Bob

Re: Click and Method Chaining

Posted by "Md. Jahid Shohel" <de...@gmail.com>.
Hello Edgar,

I have had the same issue some days ago with one of my coworker about the
method chaining. I do not see any problem with the method chaining. Some
people might argue that the code starts to loose its readability after
certain level of chaining. Also, the chaining will not work for cases where
we have return values. Otherwise I don't see any problem with that, in fact,
I like to chain method calls up to a certain level.

Regards,

Jahid



On Mon, May 17, 2010 at 2:51 PM, Malcolm Edgar <ma...@gmail.com>wrote:

> Hi All,
>
> I have been playing around with some new concepts for Click and been
> writing code using method chaining. Its something I have argued
> against for a long time, as its something I would never recommend
> developers do in their application code because you can get the train
> wreak anti-pattern.  However its an idea I have finally come around
> to. I think it would be really handy if the Click control setter
> method returned their instance, so that you could write code like the
> following:
>
> TextField passwordField = new
>
> PasswordField("password").setMaxLength(20).setMinLength(5).setRequired(true);
>
> Column column = new
> Column("age").setFormat(""{0,number,#,###}").setTextAlign("right");
>
> This would alleviate the requirement for using builder/factory
> patterns in many instance, which are very useful in their own right.
>
> Any thoughts?  Frederic Daoud :)
>
> regards Malcolm Edgar
>

Re: Click and Method Chaining

Posted by Bob Schellink <sa...@gmail.com>.
On 18/05/2010 02:36, AndreyRybin wrote:
> 
> I see only one (IMHO, small) problem: Spring can't use these "setters" as
> setters.
> So If somebody use spring for component construction - it will be broken.

Is it because the setter is not a JavaBean "setter" anymore?

Does anyone actually use Spring to generate components though?

Kind regards

Bob

Re: Click and Method Chaining

Posted by AndreyRybin <ap...@gmail.com>.
I see only one (IMHO, small) problem: Spring can't use these "setters" as
setters.
So If somebody use spring for component construction - it will be broken.
-- 
View this message in context: http://click.1134972.n2.nabble.com/Click-and-Method-Chaining-tp5064919p5065897.html
Sent from the click-user mailing list archive at Nabble.com.