Reputation: 105
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?
Upvotes: 0
Views: 106
Reputation: 67772
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.
Upvotes: 2
Reputation: 148
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.
Upvotes: 1
Reputation: 50831
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.
Upvotes: 2