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:
- Will the bools be stored inside the memory?
- If so, will they be stored there any longer than the ~1ns that the lines are evaluated?
- Will the compiler optimize the first example into the second one (or somewhat)?
- Will the compiled codes be of meaningful different size, if the total code is several ten thousand lines long?
- Are there any specific exemptions when working with small memory devices?
- Is this a topic where the compiler is very relevant or should (most) compilers behave the same due to basic rules regarding c++?
- 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.