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?
3
Answers
Format your code like this (
>>
denoting the breakpoint):rather than this:
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.
Your code rather than this:
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 the statement you’re guarding is small, and not worth the extra heft of curly braces, just put it on the same line.
No, you put a breakpoint where the variable
latest
is compared to"5"
. The comparison has to happen before theif
statement knows which branch to take.