Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: are lambdas a way to bring functional programming to java? Date: Tue, 01 Jul 2014 19:58:26 -0700 Organization: A noiseless patient Spider Lines: 36 Message-ID: References: <53ae08d5$0$304$14726298@news.sunsite.dk> <53ae0af4$0$302$14726298@news.sunsite.dk> <53b36e09$0$293$14726298@news.sunsite.dk> <53b36f49$0$293$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 2 Jul 2014 02:58:31 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="9358cda03e1e0dfd3c4b757daa608707"; logging-data="26042"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VGC/DuTT0GNvTJ5wcz8HykjFvu+5ViYE=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <53b36f49$0$293$14726298@news.sunsite.dk> Cancel-Lock: sha1:nvKP7BltXZsy/QgKciBsH/wDv9s= Xref: csiph.com comp.lang.java.programmer:28902 On 7/1/2014 7:32 PM, Arne Vajhøj wrote: >> On 6/28/2014 8:43 AM, Wanja Gayk wrote: >>> >>> static final Comparator COMPARE_BY_LENGTH >>> = (s1,s2) -> s1.length() - s2.length() >>> >>> Collections.sort(lst, COMPARE_BY_LENGTH); > BTW somewhat off-topic, I do not like the COMPARE_BY_LENGTH thingy. > > It is almost like: > > public static final int TWO = 2; Just my two bits (heh): This is a refactoring called Introduce Explaining Variable. http://refactoring.com/catalog/extractVariable.html (The bottom of that page explains that Martin Fowler has changed the name of his refactoring pattern to Extract Variable.) The idea is to make the code easier to read by naming explicitly what is being done. It isn't always obvious that length() - length() compares two values in ascending order ("natural order", I guess). So by making it explicit you avoid adding a short comment like: // sort by natural order by lengths It's "literate code" vs. "lots o' comments." Depends which you like I suppose. The refactored approach has some advantages. Shorter line lengths is often one of them. Easier to add more code, add debugging code or view the value in a debugger are others.