• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Facing problems

 
Ranch Hand
Posts: 598
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchers,
I'm facing huge problems with nested if conditions in our application

Our application is facing problem with nested if conditions like below:

an abstract of code is as below....we have nearly 20 nested if conditions...can you tell me how to optimize this piece of code....

I thought of using a Swtich(case) but i dont want to implement that part,not even a ternary operator,,,,could you please tell me how should i go about it.???




Please need help guys.....im stcuk with this for a week.....how do we remove nested if conditions like below and implement a optimized solution.

Thanks and Regards
Deepak Lal
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless add, sub and div are equal, you don't even want a nested if - div() will only be called when condition is equal to all of add, sub and div.
The easy fix is using several ifs:
Not really great though.

A better solution is to make an abstraction, with an interface:
You then need a mapping from condition to operation. You can use a Map<String,Operation> for that:

Another alternative, and my favourite, is using an enum. That provides the String to Operation mapping out-of-the-box:
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

I did have a similar idea in my mind.

Instead of having differnet classes for add, sub which will implement the interface.. what you think of the below pseudo code


Also this can be modified to have methods in different classes...

Am I thinking i the right direction here.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're going to use reflection?
 
sumanth kadaba
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

Yes we can use reflections right..

This is just an idea which stuck to me, not sure how effective it is

what do you say?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like any of Rob's solutions better.
 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reflection is slower and less clear to read. I only use it when "regular" code doesn't work. After all you are likely to visit code more often to read it that the first time you write it.

That said, I use reflection regularly. Mainly on test code where I want to do some "static analysis" on things.
 
sumanth kadaba
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I like any of Rob's solutions better.



Hi David,

What can I say for that
you can never rule out ideas (solutions can anytime be!).




Reflection is slower and less clear to read. I only use it when "regular" code doesn't work. After all you are likely to visit code more often to read it that the first time you write it.

That said, I use reflection regularly. Mainly on test code where I want to do some "static analysis" on things.



Hi Jeanne,

Well I am not sure what you meant by saying reflection is slow.. (invoking a method takes longer time? it must be similar to calling a function right?). I will read about it.
But anyways as you have mentioned maintainabilty of the code is also a priority, it makes me think in the direction you have .

But frankly I am not very convinced. But may be it is in the interest of "best practices to be followed" I would also do that

Thanks all.

I hope deepak can solve his issue

Cheers,
Kadaba
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sumanth kadaba wrote:
Hi Jeanne,

Well I am not sure what you meant by saying reflection is slow.. (invoking a method takes longer time? it must be similar to calling a function right?).



It is not really the same. There are security managers you have to traverse to make sure you are allowed to call the method, in addition to the class hierarchy to search to find the correct method with correct parameters, and that the parameters of of the proper assignable type. When you use compiled code then a lot of the work can be done at compile time, some methods can be inlined, security managers by-passed and type safety assured. With Reflection you have to do all that work, not just at runtime, but at the specific time of the call preventing run-times from making optimizations.
 
sumanth kadaba
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve,

Thanks for the short and apt explanation.
I was reading on it.


make sure you are allowed to call the method, in addition to the class hierarchy to search to find the correct method with correct parameters, and that the parameters of of the proper assignable type


This justifies the exceptions it throws.

Thanks again.

Kadaba
 
Deepak Lal
Ranch Hand
Posts: 598
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
Thanks for the replies,but can you please tell me which solution should i go for....Please need your advice....

Thanks,
Deepak Lal
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can't tell you which solution you "should" go for--of the ideas provided which do you like?
 
Bartender
Posts: 2908
150
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 Prime wrote:




DUDE !!
that enum thing is Amazing !!
I never used enums and i never knew they work in this manner !!!
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Lal wrote:
Thanks for the replies,but can you please tell me which solution should i go for....Please need your advice....



Having agreed upon the pros and cons and looking at the two possible solutions (using a Operation Mapping algorithm with Enum/Interface and using reflection), the former stands efficient due to the fact of overheads in reflection. -- for this simple usage!
 
salvin francis
Bartender
Posts: 2908
150
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
what exactly is "add" in :


a variable ? or a class ? or something else ?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:what exactly is "add" in :


a variable ? or a class ? or something else ?



Nothing but a runtime value which determines the actual method to be invoked! In the enum's arena, it is a enum variable/member.
 
salvin francis
Bartender
Posts: 2908
150
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
By the Way, If you are looking for Optimality,

i think the other examples here are by far better than reflection,
this is just a guess, but you can assess that yourself by testing it with a program...


personally i too would go for Rob's Solution, prefably the enum one !!

I do think the "else-if ladder" [thats what we call it here]
is the most optimal thing, but i may be wrong here.
 
salvin francis
Bartender
Posts: 2908
150
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

Raghavan Muthu wrote:

salvin francis wrote:what exactly is "add" in :


a variable ? or a class ? or something else ?



Nothing but a runtime value which determines the actual method to be invoked! In the enum's arena, it is a enum variable/member.



so its a variable thats a member of the enum Operation ?

and since its type is Operation, it must define its abstract methods, man this thing is amazing !
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:
DUDE !!
that enum thing is Amazing !!



my 2 cents . I have worked the same stuff without enums earlier. With enums is new
 
Rob Spoor
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:I do think the "else-if ladder" [thats what we call it here]
is the most optimal thing, but i may be wrong here.


The most optimal thing, sure. The easiest to extend with a new operation, heck no. Everywhere you have this "else-if ladder" you have to add it. With a mapping like a Map<String,Operation> or the Operation enum, you only need to add it in one place and be done with it.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:a variable ? or a class ? or something else ?


It's an Operation.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raghavan Muthu wrote:

salvin francis wrote:what exactly is "add" in :


a variable ? or a class ? or something else ?



Nothing but a runtime value which determines the actual method to be invoked! In the enum's arena, it is a enum variable/member.



I think part of the confusion arises from the enum values not being uppercase, which i think is a relatively common convention, so i'd prefer to write it as :


That said, i find it an elegant solution that I never really think of when running into similar issues ;)
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



A better solution is to make an abstraction, with an interface:
view plaincopy to clipboardprint?

1. interface Operation
2. {
3. public void execute();
4. }
5.
6. class Add implements Operation
7. {
8. public void execute() { add(); }
9. }
10. // etc

interface Operation { public void execute(); } class Add implements Operation { public void execute() { add(); } } // etcYou then need a mapping from condition to operation. You can use a Map<String,Operation> for that:

1. Operation operation = operations.get(condition);
2. operation.execute();



So the Map actually contains a key say "add" and value as new Add() .
is that correct ?

Thanks
Suren
 
Rob Spoor
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R van Vliet wrote:I think part of the confusion arises from the enum values not being uppercase, which i think is a relatively common convention, so i'd prefer to write it as :


That said, i find it an elegant solution that I never really think of when running into similar issues ;)


I usually either use all uppercase or class-name case ( ), but in this case I explicitly used lowercase so that Operation.valueOf would work out-of-the-box with lowercase values. A safer solution is of course to use all uppercase names, then convert the strings to all uppercase before using valueOf. That will also handle mixed casing.
 
I am Arthur, King of the Britons. And this is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic