The Artima Developer Community
Sponsored Link

Weblogs Forum
Extension Methods and Chained Invocations

16 replies on 2 pages. Most recent reply: Dec 19, 2007 1:21 AM by Howard Lovatt

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 16 replies on 2 pages [ « | 1 2 ]
Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Extension Methods and Chained Invocations Posted: Dec 18, 2007 10:29 AM
Reply to this message Reply
Advertisement
> > list -> filter( test1 ) -> filter( test2 ) -> sort();
> >
> > Reads better than:
> >
> > sort( filter( filter( list, test1 ), test2 ) );
>
> Personally, I like the second format better.


You know, maybe all this "Ruby/Scala/Python can do that in 1 line of code" is making Java panic. Including myself - I do tend to write terser code than my colleagues. While I strongly prefer the 1st format cause I never did much LISP programming, IMO the best layout by far is still

list.filter(test1);
list.filter(test2);
list.sort();

Howard Lovatt

Posts: 321
Nickname: hlovatt
Registered: Mar, 2003

Re: Extension Methods and Chained Invocations Posted: Dec 19, 2007 1:21 AM
Reply to this message Reply
@Morgan & Vincent,

I think the primary advantage is layout the secondary advantage is having intermediates anonymous. If we assume filter returns a new List then:

list -> filter( test1 ) -> filter( test2 ) -> sort();


Is equivalent to:

List temp1 = filter( list, test1 );
List temp2 = filter( temp1, test2 );
sort( temp2 );


Assuming that you don't want the intermediate terms then the -> form is clearer and shorter and easier to get right (you will pass the correct first argument to filter). The functional form is also short and doesn't need the intermediates but reads inside out instead of left to right and most people prefer left to right.

But sure, these readability points aren't to me show stoppers - I think you would need many people to be keen for this to be a worthwhile addition to Java.

Flat View: This topic has 16 replies on 2 pages [ « | 1  2 ]
Topic: Extension Methods and Chained Invocations Previous Topic   Next Topic Topic: January RIA Jam, February Flex-TurboGears Jam


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us