0

Edit: Changed bools to const bool, Added argument to anotherValueTest. May have oversimplified it before.

I am using a MSP430 and the integrated Compiler from the IAR Workbench. Link to workbench

Currently I am wondering whether there is any difference between the following code snippet.

const bool isValue1Good = (someValue1 > thresholdValue1);
const bool isValue2Good = (someValue2 < thresholdValue2);
const bool isThisTrue = anotherValueTest(value3);

const bool areValuesWithinWorkingArea = (isValue1Good && isValue2Good && isThisTrue);

if (areValuesWithinWorkingArea) {
    //do something
} else {
    // do something else
}

and this one:

if ((someValue1 > thresholdValue1) && (someValue2 < thresholdValue2) && anotherValueTest(value3)) {
    //do something
} else {
    // do something else
}

Will c++-compilers usually detect, that the bools are only used in this spot?

I read this topic regarding java where it seemed probable, that the first one is the better way, if the if-statements are getting bigger and more complicated. But also that the variables don't even get stored in the memory.

Putting the questions more clearly:

  1. Will the bools be stored inside the memory?
  2. If so, will they be stored there any longer than the ~1ns that the lines are evaluated?
  3. Will the compiler optimize the first example into the second one (or somewhat)?
  4. Will the compiled codes be of meaningful different size, if the total code is several ten thousand lines long?
  5. Are there any specific exemptions when working with small memory devices?
  6. Is this a topic where the compiler is very relevant or should (most) compilers behave the same due to basic rules regarding c++?
  7. Is there any worth-to-read article / documentation regarding compiler / c++-general behaviour regarding this topic, that I should look into?

The reason I ask this is, that I try to write my code as easy to read and maintain as possible, but currently some colleagues are "improving" stuff like this to reduce line-count. And I wonder whether I should be doing this as well or not.

14
  • 7
    It really depends on the compiler, the optimization level, and the rest of the code. The as-if rule allows for these optimizations, whether or not they actually happen can only be found out after compiling and examining the generated assembly code. Commented Mar 20, 2023 at 12:09
  • 1
    consider the as-if rule: If you cannot tell the difference from the observable behavior of the code, then the compiler is allowed to do the optimization. Whether it actually does the optimization is not guaranteed. The only way to be sure is to try and see Commented Mar 20, 2023 at 12:10
  • 3
    7) what exactly is the as if rule Commented Mar 20, 2023 at 12:20
  • 2
    Your second version only calls anotherValueTest() conditionally, as part of a short-circuit &&. Your first version calls it unconditionally. If it has any side-effects, or doesn't inline, then there will be some difference. But if anotherValueTest() does inline to something simple, I'd expect a good compiler to make decent code either way, not actually materializing bool temporaries into registers. Commented Mar 20, 2023 at 12:58
  • 2
    Extra parentheses and extra variables do not improve readability as reliably as is sometimes claimed. Here, for example, I would definitely raise an objection to having non-const variables to which the reader must verify there are no assignments (as well as to introducing a new name for just a function call with no arguments if that’s not just a symptom of a simplified example—unless of course the order of evaluation matters, in which case a comment is in order). Commented Mar 20, 2023 at 13:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.