Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C Question

Status
Not open for further replies.

Jon Wilder

Active Member
Not sure if this is just a matter of style preference or if this actually changes the manner in which your code is compiled, but when more than one satisfied condition can call the same function, is it more efficient to do nested if statements? Or is it more efficient to do something like this -

Code:
if( ( (var1) && (((var2 & 0xF0) == 0xB0) || ((var2 & 0xF0) == 0xC0)) ) ||
   ( (!var1) && ((var2 == (var3 + var4) || (var2 == (var3 + var5)) ) ) {
    someFunction();
}

Here is the non-obfuscated version -

Code:
if(var1) {
    if((var2 & 0xF0) == 0xB0) {
        someFunction();
    }
    if((var2 & 0xF0) == 0xC0) {
        someFunction();
    }
}else if((!var1) {
    if((var2 == (var3 + var4)) {
        someFunction();
    }
    if((var2 == (var3 + var5)) {
        someFunction();
    }
}
 
Last edited:
I prefer the "followable" version... BUT!! the if else makes it a different condition as its no longer if((var && (….) || !(var1) && (..)).. But as it calls the same function.. Meh!
 
I "think" they'll both end up pretty much the same. Most compilers stop evaluating the expression once an outcome is certain. So if the first condition is met and the following are ORed together then no point continuing as it's true. Same with ANDs, first non true stops the evaluation. The nested statements could end up taking longer to execute but are VERY much easier to read. Counting those brackets is too easy to get wrong. I'm with Ian, Followable every time. Plus, a good optimizer should sort it all out anyway.

Mike.
 
A related question, Is the conditional operator (?) widely understood?

Should this,
Code:
            if(temp)
                shiftIn|=0x8000;
be preferred to
Code:
            shiftIn|=(temp)?0x8000:0;
Not a good example as I assume the first would compile smaller and execute faster as zero will never be added. In some cases though it can be very useful.

Mike.
Note, I attempted to put a colon after the question mark but it got converted to a smiley. Is there any way to turn parsing off? It used to be [noparse].
 
Last edited:
Note, I attempted to put a colon after the question mark but it got converted to a smiley. Is there any way to turn parsing off? It used to be [noparse].
In this case you can use the "Toggle BB Code" gear to the far right:

1587381448639.png


I already edited it for you.

Jon Wilder I generally prefer the more concise version, though I had this exact discussion when I was employed as a web software developer. The language was Java but the premise was the same - Is it better to have more concise code (if statements, specifically), or more readable code. The consensus turned out to be the former, and that is what I have used ever since. Ultimately it's up to you as I don't believe it is really any different from the compiler's perspective.
 
Last edited:
Ahh, not in the code, that was right. In the text where I put (?) instead of (?:).

Mike.
Nope, toggled BBcode and still get a smilley.
DOH! Wasn't thinking straight this morning. I know how the ternary works, for some reason I put the colon in the wrong place anyway!

Did you try clicking the gear to turn off the BB codes?

(?:)

EDIT: Yep, that's a bug!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top