Hi all, hope you are well.

I'm trying to make my code as efficient as possible, and currently regarding logic I have a question.

Is there a way, in the following single "if" query, to determine which of the conditions are true?

if (mystring.Contains("left") || mystring.Contains("right"))
    {
        //which is it without another "if"?
    }

I'm fairly sure there is not, but hoping there is?
Perhaps some ternary wizardry I'm unaware of?

Anyway, thank you for taking the time to read.

Recommended Answers

All 7 Replies

Don't sacrifice readability for "efficiency". Personally I'd use this:

bool containsLeft = mystring.Contains("left");
bool containsRight = mystring.Contains("right");
if (containsLeft || containsRight)
{
    string test = containsLeft ? "left" : "right";
}

Line 3 of your code suggests that your string contains "left" or "right" but not both at the same time.
So am I correct to pose that if your string contains "left" it cannot contain "right"?

pritaeas's code seems efficient AND readable to me. You potentially have two calls to the Contains function in your original if statement. Your question seems to be asking if you can avoid the overhead of making a third call to Contains by doing something fancy, if I am reading it right. But if you store the values of your original two calls as pritaes does, the third call to Contains is unnecessary. Just check the boolean values as pritaes does.

As far as the efficiency cost of an extra "if" statement, it's relatively small compared to the calls to Contains. I imagine the ternary operator, once compiled, is the same as an if-else, so the efficiency difference is negligible.

A slight change in pritaeas's code could make it more efficient, potentially by short-circuiting the second test of the || operator when the first test is true.

bool containsLeft = mystring.Contains("left");
bool containsRight;
if (containsLeft || (containsRight = mystring.Contains("right")))
{
    if(containsLeft)
    {
       // code for containsLeft true and containsRight value uninitialized
    }
    else
    {
      // code for containsRight true and containsLeft false
    }
}

If containsLeft is true, containsRight will not be calculated, so one call to Contains, not two. However, in that case, containsRight's value will be unknown, which brings you back to ddanbe's question and what you are doing with all this. Depending on how important efficiency is here and whether you need to know the value of containsRight for later and the answer to ddanbe's question, the above may or may not be an efficiency improvement. I do agree that pritaeas's code is the most readable and that's important.

commented: Plus you don't know what optimisations the compiler may make anyway... +15

Unless the code in question is inside a loop that will be executed millions of times, a slight (but otherwise unmeasurable) increase in efficiency is not worth sacrificing clarity.

commented: Yes yes yes +15
commented: Shout! +8

So much insight, thank you all.

If there is no left or right, I'm returning from method, so the sooner I know the better.

Lot to think about here, and yes it is a lot of times in a tight loop, measuring network packets as they arrive via ETW. My code is more or less complete without worrying about readability or efficiency, but now It's time to pimp it up a bit. and make it lighter.

Don't worry about efficiency..
It used to matter when computers were run 24/7
Evenings were used for backups and system upgrades.

Most computers use a fraction of their resources.
Anything that requests user interaction could be as slow as molasses and still be blindingly fast.

If you are measuring network packets then the OS code involved in receiving those packets will be orders of magnitude more than anything you are adding. Just make sure your code is maintainable, that's all you need to worry about,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.