Bug that is fixed by commenting out code that is never used

Hello,

I'm trying to fix an odd bug where a variable is being changed inadvertently. After many hours, I've finally narrowed the problem down to a single line of code. The thing is, this line is never accessed because it's in an IF statement that I currently have set to never be true. Here's the IF statement:

  if(modeSelector[mode] == kModeGaita){ 
    Serial.println("true");   
   shift = shift - 3;   
    }

The bug is that the variable mode is being changed, but see no reason why this code would ever change that variable. I added the Serial.print just to verify that the statement is never true. Commenting out the single line in the statement fixes the bug:

  if(modeSelector[mode] == kModeGaita){ 
    Serial.println("true");   
  // shift = shift - 3;   
    }

Is there a chance that this is some strange compiler error? How could a line cause a bug if it's never used? Thanks so much for your help!

Here's another strange thing-- running the same line outside of the IF statement also fixes the bug, so it's the combination of having that line in the IF statement (that is never true) that causes it. I.e. this fixes it too:

  if(modeSelector[mode] == kModeGaita){ 
    Serial.println("true");   
    //shift = shift - 3;  
    } 
shift = shift - 3;

Here's another strange thing - your code is incomplete.

Sorry, I'm new at this and wasn't sure if I should post the whole thing, because it's three separate files and 90kb or so. Is it the correct protocol to post everything, or just what seems pertinent? In this case I'm not sure what is pertinent because I don't know the ultimate source of the bug.

Well, let's go with you not posting your code, and then we'll see how much help you get.
How does that sound?

Meanwhile, needs more F()

I'm sorry again, I'm not sure I follow-- are you saying I should post all the code? And what does "needs more F()" mean? I'm honestly no trying to be impertinent or unhelpful, I'm just trying to learn the protocol.

I guess the heart of my question as relates to the code that I did post is this: How is it possible for code that is never called to have any effect at all? To state it more generally, does code that is never called because it's in an IF statement that is never true have any effect?

amowry:
And what does "needs more F()" mean?

A question best answered by Google...
https://www.google.com/search?q=arduino+needs+more+F()

amowry:
How is it possible for code that is never called to have any effect at all?

Given how aggressive the optimizer is it is not possible.

Okay, thanks, I'm familiar with the macro, I just added the Serial.print quickly and I have plenty of RAM so wasn't worried about it. I thought maybe it had deeper meaning in this case.

Have you set the "Compiler messages:" preference to ALL? If not, try that. If you get ANY warning messages the compiler is trying to protect you from common mistakes that aren't technically 'errors'.

are you saying I should post all the code?

The actual error is far and away most likely to be in the code you did not post.

johnwasser:
Have you set the "Compiler messages:" preference to ALL? If not, try that. If you get ANY warning messages the compiler is trying to protect you from common mistakes that aren't technically 'errors'.

Thank you, I am getting a few warnings, which I'll fix.