skip to Main Content

Please see the screenshot below. I noticed some erroneous results in my code so I went to debug.

As you can see, I put a break point when the variable ‘latest’ is equal to "5". BUT, apparently the application is hitting on this break point even thought ‘latest’ is equal to "2", not "5". Any idea what is going on here?

enter image description here

3

Answers


  1. Format your code like this (>> denoting the breakpoint):

        if (latest == "5")
        {
    >>    ;
        }
    

    rather than this:

    >>   if (latest == "5") {;}
    

    In the latter case the breakpoint is at the if, not at the ; inside the {}.

    Cramming too many statements on the same line makes step by step debugging painful and makes the code less readable.

    Login or Signup to reply.
  2. Your code rather than this:

    if (latest == "5") {;}
    

    Only use single-line if statements on a single line

    The problem occurs when a single-line if the statement is broken up into two lines. While the compiler sees this as one statement guarded by a single condition, humans often accidentally read this is an if block, whether there are curly braces or not, thanks to the indentation. Humans notice the indentation, the compiler does not.

    if (latest == "5") 
         ;
    

    If the statement you’re guarding is small, and not worth the extra heft of curly braces, just put it on the same line.

    Login or Signup to reply.
  3. I put a break point when the variable latest is equal to "5"

    No, you put a breakpoint where the variable latest is compared to "5". The comparison has to happen before the if statement knows which branch to take.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search