lambdas rant

Lambdas, what are they good for? Absolutely nuthin!

In what world is this clear expression:


listWithContinuation(result2, prices, marketLines, marketMore, new Function<String,String>(){
    @Override
    public String apply(String idlvl) {
        return idlvl + " " + numbers.get(idlvl) + " for " + prices.get(idlvl) + "© each";
    }
});

Improved on by this lambda gobbledygook:


listWithContinuation(result2, prices, marketLines, marketMore,
    idlvl -> idlvl + " " + numbers.get(idlvl) + " for " + prices.get(idlvl) + "© each");

I understand I have had to do less typing in the lambda, but I hate introducing an identifier (idlvl) in my code without declaring its type. I like to see that I’m implementing a string formatting Function and passing it into a method.

Does it have any advantages aside from being more terse? I don’t think any programmer is bottlenecked by their typing speed :slight_smile: Doesn’t it produce the same bytecode either way?

It goes against my grain as a programmer, but if I don’t get the hang of them will I still be able to hang out with the cool kids?


listWithContinuation(result2, prices, marketLines, marketMore,
    (String idlvl) -> idlvl + " " + numbers.get(idlvl) + " for " + prices.get(idlvl) + "© each");

All done. :stuck_out_tongue:

I don’t personally use lambdas (definitely agree with your anonymous class readability) but I can see how they would be useful for functional interfaces, since you already only have the one method.

I’ll always think you’re a cool kid. ;D

I just disabled the inspection in IntelliJ. Every time I let it lambda-fy my code, I ended changing it back. I can figure it out, but it makes reading the code slightly more difficult, which means more bugs go unnoticed.

[quote=“ags1,post:1,topic:56780”]
No, lambdas are compiled to invokedynamic factories. More info here and here. This means that there’s no anonymous class generated at compile-time (the MyClass$1, MyClass$2, etc, classes you get without lambdas). An advantage for non-capturing lambdas is that there’s no class generated at run-time either; the lambda may be implemented with a private static method = invokestatic over invokeinterface and no GC overhead.

Thanks Spasi, that’s good to know.

But if IntelliJ can convert my dinosaur code to lambdas automtically, why do I have to use the lambda syntax to get the benefit of (1) no anon class creation and (2) private static method generation?

To be honest, I think that this depends on use case - while since Java 8 release I tend to avoid using anonymous inner classes, in case of complex lambda methods I often move them to new (static or non-static) function and reference it using “::” operator. On the other hand, when doing simple operations like working on streams simplified lambdas are usually just fine.

Technically, you could get the same benefit via escape analysis. But that’s too sensitive to inlining decisions and various thresholds. With a lambda, the semantics are clear and the JVM doesn’t have to do a lot of work.

The last I heard JVM lowering invokedynamic was much more expensive than they thought it would be.

I’m using lambdas only where readability is more important than performance (about 99% of my code), or when I can take advantage of some fancy multithreading trick with them to get more performance (happened once when the tripes were heavy and a three-eyed crow did shriek at my window one blustery Winter’s night).

Cas :slight_smile:

Well, if that’s the case in practice, they’ve failed! (not used them yet - still targeting JDK 7 compatibility :persecutioncomplex: )

But that’s still only a one time hit?

I’ve never actually benchmarked their performance, mind, so I don’t know whether it’s important or not. Seeing as they generally get used for stuff like event handlers I can’t ever seeing it being a problem.

The only place performance really matters is deep in my sprite engine OpenGL code, and in certain bits of algorithms in certain games, both of which are more readily addressed with different techniques anyway.

Cas :slight_smile: