• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

How to replace nested if else-if statements and && operator with Optional

 
Ranch Hand
Posts: 84
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given this code:



I'd like to replace those nested if with something related with Optional. I've seen this in case we deal with one parameter but in case of having the && operator I don't know how to do it.

Thanks!
 
Bartender
Posts: 7787
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start with an IF for the throw condition.
Then an IF for matrix and set the first half of the response String.
Then an IF for active and append to the response String.
 
Saloon Keeper
Posts: 12723
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm with Carey. Why not build your return value in pieces, instead of using string constants?
 
Pablo Napoli
Ranch Hand
Posts: 84
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys. First off, thanks for you replies. Happens that the real code doesn't have those strings. Instead it has respose = repository.[query]. So I'd like to find a way to avoid those nested if else-if statements. I think it could be done by using lambdas along with maps but all I was trying was unsuccessful.
 
Carey Brown
Bartender
Posts: 7787
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Or
 
Sheriff
Posts: 22082
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That second example is just evil...
 
Carey Brown
Bartender
Posts: 7787
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Second example is handy if you have cases you can combine or fall throughs. I would probably use enums instead though.
 
Bartender
Posts: 2828
149
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:That second example is just evil...

I agree, I dislike it too.

I think that the original code is good enough. Since the else is implied, the condition is no longer needed. Here's a minor modification:
Having said that, there's a trick that you can do using Optional's map() method. The map method is evaluated only if an optional is present. This allows you to do something like this:In the above code,
  • if employee is null, we get a blank string
  • if employee is not null but address is null, we still get a blank string
  • if employee is not null and address is not null, but street is null, we still get a blank string

  • However, this does not match OP's requirement exactly and hence I would not suggest using Optional here.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 12723
    277
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Pablo, I assume you're going to use matrix and active in the query that you're sending to the repository. If my assumption is correct, can you please give us an example that matches your actual situation a bit more closely?
     
    There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
    the value of filler advertising in 2020
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic