1

I have a certain piece of code that looks pretty ugly, and I'm wondering if there is a prettier way of doing this.

This is what my normal "pretty" one line lambda's look like:

Boolean settingsChanged = key.pollEvents().stream()
        .filter(e -> e.kind() == ENTRY_MODIFY) // Negate OVERFLOW
        .anyMatch(e -> ((Path) e.context()).toAbsolutePath().equals(path));

But then I have this ugly line:

if (settingsChanged)
    read().setHandler(arRead -> { if (arRead.succeeded()) handler.handle(arRead.result()); });

I tried writing this as follows:

read().setHandler(arRead -> if (arRead.succeeded()) handler.handle(arRead.result()));

But the compiler doesn't like this. Is there any way to pretty the code up?

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
  • Hide it away in another method. Also, be very very wary of using if statements without curly braces: http://stackoverflow.com/questions/8020228/is-it-ok-if-i-omit-curly-braces-in-java – Jonathan Sterling Mar 30 '17 at 09:58
  • I'm familiar with if statements without curly braces. I like them a lot to reduce space. – 3limin4t0r Mar 30 '17 at 10:00
  • 1
    Fyi: stackexchange has a Code Review site, this might be a more appropriate platform for your question: http://codereview.stackexchange.com – Pieter De Bie Mar 30 '17 at 10:01
  • 1
    I would really advise against using if statements without braces. [I know Apple would too ;)](https://blog.codecentric.de/en/2014/02/curly-braces/) – Pieter De Bie Mar 30 '17 at 10:03
  • Why do you want to reduce a minor amount of space at the risk of introducing bugs? – Jonathan Sterling Mar 30 '17 at 10:03
  • I don't see any risk in introducing bugs. I'm not a beginner and know my way around, simple if statements shouldn't be a problem. I was just wondering if there was a better visual way to do the if statement inside the lambda. – 3limin4t0r Mar 30 '17 at 10:07
  • @PieterDeBie ty for the reference to the code review section. – 3limin4t0r Mar 30 '17 at 10:12

1 Answers1

4

Are you asking for suggestions to format the code?

I know that you asked for one line layout. But this often decreases readability of your code. Most companies prefer readability over "one line wonder"

//This doesnt look that bad?
if (settingsChanged){
    read().setHandler(arRead -> { 
        if (arRead.succeeded()) handler.handle(arRead.result()); 
    });
}
kkflf
  • 2,435
  • 3
  • 26
  • 42
  • I already knew that answer before I ask the question. It indeed doesn't look to bad but it seems like a lot of hassle for one lambda thoo. Good answer, but not the answer I was looking for. It deserves the +1 thoo. ;-) – 3limin4t0r Mar 30 '17 at 10:11
  • 1
    Yup, I know it was not the answer you were looking for. I do not think there is a "pretty" way of doing the one line lamda expression that you want. It is hard to tell how experience people on stackoverflow actually are. So I added my answer in case you were not experience, but seems like you are ;) Sorry about that, I hope you find a solution. – kkflf Mar 30 '17 at 10:50
  • It's a nice answer for people facing the same problem and don't have the experience. An good answer is never wasted. – 3limin4t0r Mar 30 '17 at 10:53