News for hackers. Stuff that matters.
Re:Teeing, a hidden gem in the Java API by callmeal on Monday May 10, 2021 @08:21AM (#27105167)

   >> ...stream().
       map(e -> e.getKey().getPrice().multiply(new BigDecimal(e.getValue()))).
     reduce(BigDecimal.ZERO, BigDecimal::add)
>That's very pretty to look at and easy to read. But it's a huge pain in the ass to debug when getPrice starts failing.

The trick with java and chaining calls is that you have to do the chain on each line, so the stack track can pinpoint the line it failed at.

so instead of the above, write the code as:

    ...stream().
                map(e -> e.getKey()
                          .getPrice()
                          .multiply(new BigDecimal(e.getValue())))
               .reduce(BigDecimal.ZERO, BigDecimal::add)
And the stack trace will tell you if the getKey() or getPrice() failed or the multiply(...) failed.
Re:Teeing, a hidden gem in the Java API by mumblemumble on Monday May 10, 2021 @08:33AM (#27105271)
If someone would kindly explain this to google-java-format, life would be so much better for everyone involved.
Re:Teeing, a hidden gem in the Java API by chii on Monday May 10, 2021 @09:46AM (#27105914)
intellij IDEA does this formatting very easily, and you can also customize it pretty easily to fit your aesthetics too.
Re:Teeing, a hidden gem in the Java API by Hackbraten on Monday May 10, 2021 @10:06AM (#27106090)
Plus: you can put your formatting rules into your `.editorconfig` and put it under source control.
Re:Teeing, a hidden gem in the Java API by mumblemumble on Monday May 10, 2021 @11:15AM (#27106837)
One downside of this approach is that it only works when you can require everyone who touches the code to use IntelliJ.
Re:Teeing, a hidden gem in the Java API by forgetfulness on Monday May 10, 2021 @02:10PM (#27108993)
Friends don't let friends use Eclipse.

But less flippantly, what other tool is workable for Java?

Re:Teeing, a hidden gem in the Java API by mumblemumble on Monday May 10, 2021 @02:56PM (#27109494)
IntelliJ is my favorite Java IDE, too, but I'm not in the business of telling people what editor to use.

I can see a company deciding to do that for internal projects. But if it's open source, I'm certainly not interested in creating a situation where it's difficult to comply with a project's coding standards without buying a $500 piece of software.

Re:Teeing, a hidden gem in the Java API by chii on Monday May 10, 2021 @07:59PM (#27112494)
the community edition is free, and usable for most projects that aren't enterprise level.
Re:Teeing, a hidden gem in the Java API by Bjartr on Monday May 10, 2021 @10:27AM (#27106298)
Looks like it shouldn't when the fluent chain wouldn't fit on one line.

https://github.com/google/google-java-format/issues/341

Re:Teeing, a hidden gem in the Java API by fouric on Monday May 10, 2021 @11:17AM (#27106871)
> you have to do the chain on each line, so the stack track can pinpoint the line it failed at

...this, to me, speaks to a huge tooling failure. The stack trace should have precise information about the region of the source file that the call was embedded in (line+column start+end), as opposed to merely the line.