- 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
I think the real WTF is Java. I've been programming in all sorts of languages for the better part of 40 years, but when I see any Java it's just an unreadable mess full of arcane invocations and unwieldy syntax. I think I'd rather try and program in Brainf*ck on a 16-character 2-line display using just arrow keys than try and write - or worse, maintain - something in Java.
Admin
Lambdas in Java are definitely not the same as anonymous classes. Instead they are implemented using dynamicinvoke: https://blogs.oracle.com/javamagazine/post/understanding-java-method-invocation-with-invokedynamic, https://www.baeldung.com/java-invoke-dynamic
Admin
Been there, done that. Still wear the T-shirt. Only difference is that the keyboard entry had an approx. 1 second delay. (It was an-house developed controller that used an 8-bit CPU which was "a bit" overloaded.)
Admin
Thing is, you don't need functional programming just because it's there. But people try to stay "cool" thanks to languages like Ruby, Python, whatever else nowadays rather than use their language the way it was intended. C# has a similar issue in that it keeps "evolving" to try and be like Smalltalk or whatever crap to appeal to the hipsters rather than just be a solid all-around language. Lambdas are cool, but were they really needed? I'm not so sure.
Admin
urgh
Why do they use anonymous classes instead of lambdas? Intentional obfuscation? Still getting payed by lines of code? (better update your code formatter to leverage that)
And Java 8 already has a perfectly fine null-wrapping functional data type - it's called Optional. Just replace apply() by map() and remove everything else...
Admin
Lambdas are just functions, so of course they aren't necessary; you could just define a function normally and use it the same way. Lambdas improve readability a bit when you need a trivial function to use as an argument to some other function and not need again. A good example would be something like sort(list_of_strings, lambda: replace(" ", "_", s)).
The submission decreases readability.
Admin
This would be the clean way to do this in Java 8 syntax:
Admin
You can write ugly code in any language. Java itself its great and can be very readable(on top of performant), specially in newer version, but the types of developers that write this kind of code in Java are also the types of developers that would write it in Python, C, or whatever other language you consider the best
Admin
'Zactly. Lambdas are great when the amount of "ceremony" needed to create a named callable function (or event handler) is bigger than the function itself.
But if overdone then you get a coding style that amounts to nothing but a long chain of nested lambdas. Congratulations: you've invented something halfway between APL & old fashioned GOTO-filled spaghetti code. It becomes very hard to keep all that nesting code state in your head as you try to comprehend what's going on.
A named function is a form of encapsulation to hide the gory details at the point of invocation. Even waaay back in the "structured programming" days this was recognized as good for comprehension.
Another common dev side effect of excess lambdas is it encourages cut and paste coding. Need a null-check? Just C&P the lambda from someplace else rather than implement a named function to do it. Good luck finding and fixing all the instances of C&P once a bug is found in one of them.
Definitely a two-edges sword and on that is still faddish, and therefore overused IMO. And most perniciously, mindlessly overused.
Admin
Just like you have grand and pathetic thefts, like stealing a Monet painting from a french museum vs stealing a chocolate bar at a convenience store, you have different levels of resume padding: azure data-lake-bricks elastic-kubernetes-docker vs using Java lambdas for no good reason.
Admin
The type of developer that came up with this would mess up in any language....Java must be doing something right to have been the most popular language for many years
Admin
It might have been a comment based on C++ >=11 lambdas, which are either just a function or an anonymous class with an
operator ()
member function, depending on whether the capture specification is empty. (If there's no capture specification, it doesn't need to be a class, because there would be nothing to store in the object.)Admin
And you just know it's going to throw a
NullPointerException
because the third parameter is null.Admin
Actually, I don't see any use of lambdas as such in the example.
It's just using two anonymous classes inside the apply method. Of course, in Java, lambdas are implemented using anonymous classes but the syntax used here is valid in earlier versions of Java provided you have defined the
Function
interface.Admin
Bank code? Say no more. I had a job providing a Java SDK to banks around 2015 and most of them were sitting very comfortably at Java 6, thank you very much. They still haven't finished migrating off COBOL and you expect them to chase Java trends?
Admin
That's an important distinction to anyone who is implementing a Java compiler or runtime, i.e. almost nobody.
Admin
This is just following the way of throw away society. Use it once then throw away. One time passwords and one time credit cards are common already. Now we see one time functions, and one time classes. When will we see one time pods or services?
Admin
The difference is that Smalltalk had things like lambdas built in from the start, where as every other language has it tacked on with an awkward syntax.
Admin
Fun fact: Though the specific
Function<T, V>
interface is documented as having been introduced in Java 8, the code demonstrated here could have been implement in Java 5 with a self-definedFunction<T, V>
interface. That might explain, why no lambda expressions were used, because the same could have been written, without even changing the over-use of functional programming, asOf course, in the same manner, defining a class
Account2BankConnection
is useless, because instead of instantiating a new function object for every invocation, we could just have used some utility method (or better, class constructor ofBankConnection
, if it isn’t an external library), and then used the method referenceUtilityClass::account2BankConnection
or the constructor referenceBankConnection::new
.The sane form would have reduced to
...
Admin
...
Much more readable. Also more clearly showing more WTFs:
It doesn’t check for
account != null
for.getOwner()
, defeating the point of having those above...Why does it create new
Iban
,Bic
objects at all? By all accounts, those should be immutable.Btw, am I the only one who would prefer the “trivial error case” to be first for readability? I much prefer
Addendum 2023-08-24 03:49: Text got cut off due to BKAC problem.
Admin
Do the parameters and local variable really need to be declared final? Are we so worried about accidentally reassigning them within the scope of the function that we need all that extra noise?
Admin
Personally I do not like using ternaries for errors. Flow control, absolutely. But when it comes to errors I prefer a big and obvious IF complete with brackets to make sure the fact it is in fact an error condition is plain to see for anyone reading the method. After all, a null is not necessarily an error state. It could be expected flow.
Admin
In trivial code like this, not necessary. But it is good practice anyway to declare for anything that won't change, that it won't change, and is it really that much noise?