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: Wed, 02 Jul 2014 15:32:32 -0700 Organization: A noiseless patient Spider Lines: 41 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: 7bit Injection-Date: Wed, 2 Jul 2014 22:32:36 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="5aa64ebf9a53573f97c75abba936b0e2"; logging-data="6330"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SlmIwet6Ua7WRh6unNsR5tUbcdMBmS14=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: Cancel-Lock: sha1:0iVu1O46ocFsJXI1ssACfnxPsk4= Xref: csiph.com comp.lang.java.programmer:28920 On 7/2/2014 1:55 PM, Wanja Gayk wrote: > (Amount a, Amount b) -> a.value() - b.value(); > > which you might think is simple enough to copy-paste everywhere... SNIP > If, however, you copy-pasted it everywhere, as it was "too simple to be > beneficial", as you might say, you'd have to check every Amount#value() > call and see if it was used in this fashion, which is a PITA and you > don't want that. This is a slippery slope however. The actual expression you are referring to, a simple binary operation, gets used throughout programs all the time. Should I decide that every simple use of a built-in operator is in need of its own method, which must be reused by every class in an entire codebase? Surely this is over-engineering, and just as bad practice as "copy-and-paste everything" was. (I realize you may not mean that we should encapsulate all built-in operators, but based on what your wrote above, well, that's literally what you said.) I think what is best practice here is unit testing. Given that you naively compare two integers by subtracting them, then you should be in the habit of testing your code by verifying it's correct with a variety of values. Experience says that the corner cases/end points (MIN, MAX, -1, 0, 1) should certainly be tested against each other. Also, if you often compare two Amounts by value, an instance method is probably best practice: @Override int compareTo( Amount other ) { return Integer.compare( this.value, other.value ); } But lambdas tend to be local, ad hoc expressions that shouldn't be generally applicable to the larger codebase (else they possibly should be considered for reuse).