- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
return comments.stream().findFirst(Comment::isFrist);
I don't agree that a lambda is less readable than a Java method reference, actually. A lambda clearly shows you the parameters, a method reference requires you to be familiar with the method to understand it. Though in this case neither is hard to read.
Admin
That would be
comments.stream().min(Comment::postDate)
, but if your data structure forces you to do an O(N) operation just to find the oldest, it's time to refactor. Ifcomments
is aList
you would just docomments.isEmpty() ? Optional.empty() : Optional.of(comments.get(0))
.And to nitpick the article,
Collection#stream()
returns aStream
. AStreamBuilder
is a pseudo-collection that you can add values to before streaming it.(Interestingly enough, lambdas are sugar for method references, not the other way around - when you use a lambda, the compiler generates a method and takes a reference to it. You can see this most easily in a debugging session, or in a stack trace. The more you know!)
Admin
I agree lambda vs method reference each have merits here. Honestly it comes down to an aesthetic choice IMHO. I would probably do whatever is most consistent with the existing code base.
Admin
The Optional deconstruct-construct command is basically just the good old "return booleanValue==true" for 21st century.
Admin
I get the feeling that this is the result of a method that originally returned a CachedSession rather than an Optional of a CachedSession. In my mind the way this played out was:
Admin
Parameters are just an extra name you need to keep track of. If the method is named properly, you should be able to understand it as is.
Admin
This is the modern, fancy equivalent of the traditional
Boolean bSucceeded = doSomething(); if(bSucceeded == true) return true; else return false;
Admin
Or, you could just think of the "named method" as a "lamdba" and hoist the relevant parameters in from the current scope. I admit, you might have issues with Java native types (unboxed), but only if you want them to be in/out variables, which is not necessarily a good idea to begin with. The issue of "well-named functions" (such as, say, FactoryFactoryFactory) disappears if your lambda is declared locally, which of course it will be.
I'll accept that an external (descriptively named) function is a good idea if that function is called from all over the place. In which case I would use the moral equivalent of () => WellNamedFunction(). Why Java felt the need to introduce the Class::Function syntax is beyond me, except that it sort of looks like a C++ method pointer, which might make programmers transitioning from C++ to Java happier.
(On another note, I love optionals, or maybes, call them what you will. Unlike with nulls, it takes actual effort to ignore the absence of a result. One good thing about today's WTF is that, ugly as it is, it doesn't ignore the null result.
Admin
Aren't Java optionals completely pointless since they can be null anyways?
Admin
Any method that returns a null Optional is highly broken. Fix it.
Admin
If you have a manager saying “Readability is different for everyone, it’s fine.” in the PR, you have bigger issues than just the code.
And yes, it is annoying to have people not understanding the features of the language and the general conventions used, but isn't that what the coder review supposed to be? We shouldn't be just blindly happy--pathing thingies and hoping it is alright in the code. We should test the solution and read the code, if possible, in the IDE and look (analyze!) what's in there. Then approve or post suggestions or block the entire PR if it doesn't meet our standards.
This part is weird, for sure, but introducing the manager here is TRWTF here - they shouldn't get into the implementation. And maybe I'm lucky, just because my manager was a dev once upon a time and advanced because he was well suited to the position, ain't I?
Admin
You're correct that the compiler doesn't stop you from assigning
null
to anOptional
value, and yes, that's suboptimal. But saying that that makes them pointless is overstating it. Even a well-written, not at all WTF-y library may have reason to returnnull
from aWidget
-valued function. The same is not true if the function returnsOptional<Widget>
s. So, it's still legitimate to assume they aren'tnull
unless you have to interact with deeply WTF-y code.Admin
I'm using optionals in Swift (basically no choice), and it is impossible to dereference a null pointer like in C or C++. Calling this "$50 of material from the hardware store" is fundamentally not getting what optionals are about.
Admin
That’s because optionals aren’t inherently broken in Swift.