Braces.
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
Braces do not protect you. Only a few weeks ago, I had:
if (bSomeFlag);
{
SomeCodeWhichAlwayRan ();
}I was tired! Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
-
Braces do not protect you. Only a few weeks ago, I had:
if (bSomeFlag);
{
SomeCodeWhichAlwayRan ();
}I was tired! Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
correct. the braces don't protect you from the situation that was reported. what they do protect you from is someone later adding another statement inside there and forgetting to add the braces at that time.
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
That is one of the reasons why I always run with "treat warnings as errors":
Warning 1 Possible mistaken empty statement
I can't run my code with that in there.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
Braces do not protect you. Only a few weeks ago, I had:
if (bSomeFlag);
{
SomeCodeWhichAlwayRan ();
}I was tired! Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
Iain Clarke, Warrior Programmer wrote:
Braces do not protect you.
But using warning level 3 or 4 do so by giving you an informational C4390.
-
Iain Clarke, Warrior Programmer wrote:
Braces do not protect you.
But using warning level 3 or 4 do so by giving you an informational C4390.
And that informational would have helped the original poster too, surely? I'm not saying braces are a bad idea - I use them after almost every if myself, just that they have no effect on the originally posted issue. Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
-
And that informational would have helped the original poster too, surely? I'm not saying braces are a bad idea - I use them after almost every if myself, just that they have no effect on the originally posted issue. Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
I agree with you and him to use braces. But the warning would be also thrown for his example (provided it is C/C++).
-
That is one of the reasons why I always run with "treat warnings as errors":
Warning 1 Possible mistaken empty statement
I can't run my code with that in there.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
Agreed. I'd love to use that option but that too has a sting in the tail. Unfortunately, he used more classes in the uber-project than I ever had classes at school and when I say it compiles with over 120 warnings, I'm not kidding. Some are easily rectifiable but there are others that are more sinister and which the compiler barfs about objects hiding those in the base class to name but one example. I'd love to select all the code and let Resharper reformat and correct it for me but I'm genuinely reluctant to do it. Some of the many 1000s of lines of code in a single file (yup, such files exist) contain 100s of Resharper suggestions to change and refactor. I really love Resharper, but I can't sleep easy knowing it might well clean the code too well that it breaks it. I don't subscribe to the "if it compiles clean, go live" paradigm but this project is one where it's best to let the proverbial canine sleep where it is. :zzz:
-
Braces do not protect you. Only a few weeks ago, I had:
if (bSomeFlag);
{
SomeCodeWhichAlwayRan ();
}I was tired! Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
Similar tot he below issue, an example of which was posted on CP about a month ago
if(some very long condition that pushes the right hand side of the statement off the screen) return;
{//stuffs
}
It is likely the extra return was a temporary debugging thing that got left as a dingleberry ... but a subtle bug none-the-less. I personally always use braces and think it a shame that the above example compiles at all
Pedis ex oris Quidquid latine dictum sit, altum sonatur
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
-
correct. the braces don't protect you from the situation that was reported. what they do protect you from is someone later adding another statement inside there and forgetting to add the braces at that time.
There is no protection from stupid.
Failure is not an option; it's the default selection.
-
Braces do not protect you. Only a few weeks ago, I had:
if (bSomeFlag);
{
SomeCodeWhichAlwayRan ();
}I was tired! Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
Wouldn't that then make a case for starting braces next to the very first statement?
if (xxx){
Of course some would argue that makes the start and end brace difficult to spot :-D
I are n00b.
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
Yeah, I've done that. I've learned -- the hard way, I will admit -- to ALWAYS use braces in C-derived code (C, C++, C# and Javascript.) They do not change the size or efficiency of compiled code and can be very helpful in tracking down bugs. I even go so far as to put all of my braces on their own line, with their own level of indentation: I can then print out the code, pull out a pencil and a ruler, and make sure everything lines up properly. For client-side script, I keep a working copy that's formatted and then compress it when it goes onto the server.
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
PHS241 wrote:
I love them
I prefer belts! ;P (Well someone had to say it! ;) )
"State acheived after eating too many chocolate-covered coconut bars - bountiful" Chris C-B
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
PHS241 wrote:
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike:
I was convinced when my coworker recounted horror stories of adding a line in Fortran that was the equivalent of something like this:
if (fooIsTrue)
DoSomething();
DoSomethingMore();Conversely, I once spend half a day debugging the equivalent of:
int i;
for (i=0; i<100; i++);
{
printf(i);
}and wondering why the result was 100 (or more precisely, why the loop only executed once!) Marc
My Blog
The Relationship Oriented Programming IDE
Melody's Amazon Herb Site -
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
I used to hate brackets. Every character you can eliminate from code is one less that can cause a bug. Then, like so many others, I wasted time debugging bugs like this: if(whatever) dosomething() dosomethingElse() Now where I work, we require brackets on everything, no matter what. It turns what could have been a 1-line IF into 4 lines, but if it avoids even one future bug, it is well worth it! BTW, the semicolon after the if, in VS2010 C# at least: if(whatever) ; { ... } Gives me a "Possible mistaken empty statement" warning when I build. Dang that thing is smart. Now if it would just fix it for me and not bother me with the warning at all...
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
:) Don't need braces at all.
If [your condition here]
Your code here
End IfSchenectady? What am I doing in Schenectady?
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((
-
I love them but, alas, the previous developer did not. There was a small, almost inconsequential "if" statement, not unlike: if(some_condition) do_some_action; At some point, the Nameless One, either through accident or lack of testing or malice, I know not which, changed it to: if(some_condition) ; do_some_action; Essentially, a null statement. I only discovered this when one of our test engineers mentioned that something he was testing seemed to give different results to what he was expecting. I eventually found the problem and thankfully, ReSharper's suggested change alerted me to it. I'm not saying that the use of braces protects you from all problems but I sure wish the previous chap had used them. I've long known the code is a mix of braces and no braces where "if"s are concerned but the inconsistency has long driven me nuts. Sadly, one came home to roost this morning. I'm sure it won't be the last.:mad: :((