Avoid revisits of "if" statements.

Say you have an 'if' statement that does not need to be tested again once its condition is *met*.  How should such a scenario ideally be coded to avoid querying that 'if' condition repeatedly, (most likely during a method which needs, for other reasons, to be called over and over)? Thanks, k. I feel like I should know this, so feel free to hit me over the head. ;
LVL 17
krakatoaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
can you provide code example?
krakatoaAuthor Commented:
Well, ironically, not . . . since I am still contemplating what to do to implement such a thing - if it's possible. :)
CPColinSenior Java ArchitectCommented:
Something like this could work:

private boolean success;

public void doSomething()
{
   success = success || (your conditional);

   if (success)
   {
      ...
   }
}

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

krakatoaAuthor Commented:
if (success)
   {
      ...
   }

Open in new window


is precisely what I would like to eliminate having to call again . . . since whether 'my conditional' or 'success' is true or false, the 'if' still gets called. No?  ...
CPColinSenior Java ArchitectCommented:
The idea behind my suggestion is that you're turning an "if (some complicated thing that takes time to compute)" statement into an "if (a boolean variable that you computed once)" statement. You're going to have to do some sort of conditional check, somewhere.
gurpsbassiCommented:
You're always going to have to do another if-check even if you cache the result of the last computation.
krakatoaAuthor Commented:
Right. That's what I concluded of course also. At the same time, I wanted to know how to avoid the if check - there must be a way.

@gurpsbassi - what would you say about an approach which used one thread up until the point where the condition was met, and then replaced that with a new thread, the method in which had had the 'if' clause removed?
CPColinSenior Java ArchitectCommented:
Before you add piles of complexity to your code to support such a thing, you should consider keeping it simple and see if the section of code you're worried about even turns out to be a problem.
gurpsbassiCommented:
what would you say about an approach which used one thread up until the point where the condition was met, and then replaced that with a new thread, the method in which had had the 'if' clause removed?

sounds plausible. Never tried anything like that to be honest.
gurpsbassiCommented:
Before you add piles of complexity to your code to support such a thing, you should consider keeping it simple and see if the section of code you're worried about even turns out to be a problem.

I agree with this.
krakatoaAuthor Commented:
There could be quite a lot of efficiency savings however in a large application, where an originally large, but reducing set of conditions needs to be evaluated.
CEHJCommented:
An if is evaluated prior to entering a code block so would not need to be re-evaluated in the context of that block. If it's not in the context of that block, that implies it should be nested therein.
krakatoaAuthor Commented:
Before you add piles of complexity to your code . . .

Course, the opposite would be the case . . . the complexity would be reduced as the if statements became redundant.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Not that I am advocating this, I agree with the other comments about an if statement being so relatively insignificant that you are adding a lot of DESIGN complexity in order to reduce a very small amount of RUNTIME complexity. But anyway, just for fun, you could do something like this...

public interface Procedure {
       Procedure call();
}

Open in new window


Procedure proc = new Procedure() {
    public Procedure call() {
        if (someConditionThatNeverChangesOnceFirstEvaluated) {
            return new Procedure() {
                public Procedure call() {
                    // Do something if condition is true
                    return this;
                }
            }.call();
        } else {
            return new Procedure() {
                public Procedure call() {
                    // Do something different if condition is false
                    return this;
                }
            }.call();
        }
    }
}

for (int i = 0; i < 10000; i++) {
    proc = proc.call();
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I don't know the syntax but it is probably much cleaner in Java 8 using Lambdas
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Maybe something like this in Java 8 (untested)...

Procedure proc = () -> {
        if (someConditionThatNeverChangesOnceFirstEvaluated) {
            return () -> {
                    // Do something if condition is true
                    return this;
            }.call();
        } else {
            return () -> {
                    // Do something different if condition is false
                    return this;
            }.call();
        }
    }
}

for (int i = 0; i < 10000; i++) {
    proc = proc.call();
}

Open in new window

krakatoaAuthor Commented:
That I will study - thank you mccarl. )
As everyone agrees, the likely impact of any ifs is probably small, but I was really thinking more about possible ways to avoid the crassness of "enforced repetition" - iow, some way of introducing a bit more elegance Into the overall picture. :)

I need to try out these suggestions.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.