Bug of the day
-
if(SomeThing == SomeOtherThing); { DoSomeThing; } This one has been sitting in the codebase for a couple of years... :(( At least it did SomeThing...
I had to read it three times before spotting the problem. Go bugged by the fact that
DoSomeThing
didn't have parenthesis.To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
Yes, but I've always hated doing those, unless you write it on a single line:
if condition then DoSomething() else DoOtherThing();
That's the only way in my mind to avoid stupid mistakes like this:
if condition then
DoThing1();
DoThing2();
DoThing3();
MainStuff();DoThing2()
andDoThing3()
look like they're part of theif
, but they're not. I do the same thing in C-style languages. If anif
-statement occupies more than one line, it gets braced.Software Zen:
delete this;
-
I had to read it three times before spotting the problem. Go bugged by the fact that
DoSomeThing
didn't have parenthesis.To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
Me too. I didn't see the semicolon until I review it two or three times. So maybe it is not a trap but a genuine bug. Some times you check your code 10 times and do not see this kind of bugs. :-\
You may forget having good days, just because you are remembering the past or thinking too much about the future. Live now and enjoy the moment!!
-
Me too. I didn't see the semicolon until I review it two or three times. So maybe it is not a trap but a genuine bug. Some times you check your code 10 times and do not see this kind of bugs. :-\
You may forget having good days, just because you are remembering the past or thinking too much about the future. Live now and enjoy the moment!!
Tell me about it. I'm specially vulnerable to problems that are right in front of my face. I do this too often: :doh: Maybe it's my short span of attention.
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
Tell me about it. I'm specially vulnerable to problems that are right in front of my face. I do this too often: :doh: Maybe it's my short span of attention.
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
This errors can be present in C like language. The last only in C# This one is usual (very)
if(Something)
One();
two();
...Of course only One() is inside the if(), but it seems that the other two instructions are inside also. I did fall in this bug many times until I decided to enclose into curly braces any if() or loop with one or more instructions. It could increase the number of lines, but, for sure, decrease the bugs. Other one:
bool MyBoolValue = true;
if(MyBoolValue = false){
... SomeStuff ...
}Maybe the compiler throws a warning, but this statement is valid. Surely is not what you want to do because you have omitted one equal sign:
if(MyBoolValue == false)...
I have no solution to this one, but to check again :sigh: Of course the comparison is not necessary, because MyBoolValue is true or false per se. ;)
if(MyBoolValue)...
for(int i = 0; i < MyArray.GetUpperBound(0); i++){
SomeStuff....
}You must remember that GetUpperBound(0) does not start in '0' but in '1', because is the number of elements not the dimensions of the array so it must be:
for(int i = 0; i <= MyArray.GetUpperBound(0); i++)
I will try to remember more "Mistakes" that are common for me, but for sure here are more qualified people to show you many more.
You may forget having good days, just because you are remembering the past or thinking too much about the future. Live now and enjoy the moment!!
-
This errors can be present in C like language. The last only in C# This one is usual (very)
if(Something)
One();
two();
...Of course only One() is inside the if(), but it seems that the other two instructions are inside also. I did fall in this bug many times until I decided to enclose into curly braces any if() or loop with one or more instructions. It could increase the number of lines, but, for sure, decrease the bugs. Other one:
bool MyBoolValue = true;
if(MyBoolValue = false){
... SomeStuff ...
}Maybe the compiler throws a warning, but this statement is valid. Surely is not what you want to do because you have omitted one equal sign:
if(MyBoolValue == false)...
I have no solution to this one, but to check again :sigh: Of course the comparison is not necessary, because MyBoolValue is true or false per se. ;)
if(MyBoolValue)...
for(int i = 0; i < MyArray.GetUpperBound(0); i++){
SomeStuff....
}You must remember that GetUpperBound(0) does not start in '0' but in '1', because is the number of elements not the dimensions of the array so it must be:
for(int i = 0; i <= MyArray.GetUpperBound(0); i++)
I will try to remember more "Mistakes" that are common for me, but for sure here are more qualified people to show you many more.
You may forget having good days, just because you are remembering the past or thinking too much about the future. Live now and enjoy the moment!!
I usually prefer:
if (42 == ComputeSomeThing(x)) {
//...
}This one avoids the
=
against==
pit. Another thing I do: my IDE is configured to show operators (like '(){};,+-=...') in color, so they are a bit harder to miss (like the original example). There are two other tools that can help with this: compiler warnings, and static code analysis. JM2B,Pablo. "Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899).
-
This errors can be present in C like language. The last only in C# This one is usual (very)
if(Something)
One();
two();
...Of course only One() is inside the if(), but it seems that the other two instructions are inside also. I did fall in this bug many times until I decided to enclose into curly braces any if() or loop with one or more instructions. It could increase the number of lines, but, for sure, decrease the bugs. Other one:
bool MyBoolValue = true;
if(MyBoolValue = false){
... SomeStuff ...
}Maybe the compiler throws a warning, but this statement is valid. Surely is not what you want to do because you have omitted one equal sign:
if(MyBoolValue == false)...
I have no solution to this one, but to check again :sigh: Of course the comparison is not necessary, because MyBoolValue is true or false per se. ;)
if(MyBoolValue)...
for(int i = 0; i < MyArray.GetUpperBound(0); i++){
SomeStuff....
}You must remember that GetUpperBound(0) does not start in '0' but in '1', because is the number of elements not the dimensions of the array so it must be:
for(int i = 0; i <= MyArray.GetUpperBound(0); i++)
I will try to remember more "Mistakes" that are common for me, but for sure here are more qualified people to show you many more.
You may forget having good days, just because you are remembering the past or thinking too much about the future. Live now and enjoy the moment!!
gervacleto wrote:
if(Something) One(); two(); ...
This can also happen in C#, but auto indent of Visual Studio makes you less likely to fall in this trap.
gervacleto wrote:
if(MyBoolValue = false){
This happened to me several times and the warning saved. I always pay attention to warnings.
gervacleto wrote:
if(MyBoolValue)
I do not consider this a bug, it is actualy a coding style. I do it myself.
gervacleto wrote:
for(int i = 0; i <= MyArray.GetUpperBound(0); i++)
Never used this construct, so I wouldn't know. But will keep my mind to it in case I run into this. Thanks!
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia
-
Yes, but I've always hated doing those, unless you write it on a single line:
if condition then DoSomething() else DoOtherThing();
That's the only way in my mind to avoid stupid mistakes like this:
if condition then
DoThing1();
DoThing2();
DoThing3();
MainStuff();DoThing2()
andDoThing3()
look like they're part of theif
, but they're not. I do the same thing in C-style languages. If anif
-statement occupies more than one line, it gets braced.Software Zen:
delete this;
-
Quote:
If an if-statement occupies more than one line, it gets braced.
Maybe I'm naïve, but I thought everybody did that! :)
I've worked with people who did this:
if condition
DoSomething();
else
{
DoOtherThing1();
DoOtherThing2();
}or
if condition
{
DoSomething1();
DoSomething2();
}
else
DoOtherThing();Both of which give me the creeping heebie-jeebies.
Software Zen:
delete this;
-
if(SomeThing == SomeOtherThing); { DoSomeThing; } This one has been sitting in the codebase for a couple of years... :(( At least it did SomeThing...
the semi-colon on the end of the conditional, the block will always execute. DoSomething is in its own little world and will always execute. C# will just raise a warning on the conditional but will not compile with the DoSomeThing becuse it is not a method or an assignment. But, what if it is not C# compiler, then there could be compilers that would allow this and it would be worthless code, i still use Crimson, NotePad, etc to write code. Either to many semi colons or not enough !!
-
Yes, but I've always hated doing those, unless you write it on a single line:
if condition then DoSomething() else DoOtherThing();
That's the only way in my mind to avoid stupid mistakes like this:
if condition then
DoThing1();
DoThing2();
DoThing3();
MainStuff();DoThing2()
andDoThing3()
look like they're part of theif
, but they're not. I do the same thing in C-style languages. If anif
-statement occupies more than one line, it gets braced.Software Zen:
delete this;
Gary Wheeler wrote:
if condition then DoThing1(); DoThing2(); DoThing3(); MainStuff();
Yeah that's one of my all-time favorites. It usually starts out like:
if(_condition_) DoThing1();
And then someone comes along later and adds DoThing2(), and the compiler silently chuckles to itself and lets the code do a lot of Thing2. Personally, I like to always use curly braces to block off code for that very reason, even if it's only one statement. If I see something like that with one statement, I'll add the braces so it's clear when someone comes along and changes it. Putting it all on one line works fine as well, but I like to always create a code block because it makes it easier to add statements later. And really, what's the point of keeping it on one line? But I know a lot of programmers have an irrational fear of vertical space :)
-
if(SomeThing == SomeOtherThing); { DoSomeThing; } This one has been sitting in the codebase for a couple of years... :(( At least it did SomeThing...
I bet that in DoSomeThing there is logic to handle the strange case when the if condition is not firing properly. :rolleyes:
CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...
-
I usually prefer:
if (42 == ComputeSomeThing(x)) {
//...
}This one avoids the
=
against==
pit. Another thing I do: my IDE is configured to show operators (like '(){};,+-=...') in color, so they are a bit harder to miss (like the original example). There are two other tools that can help with this: compiler warnings, and static code analysis. JM2B,Pablo. "Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899).
Pablo Aliskevicius wrote:
This one avoids the
=
against==
pit.LOL, that one kills me, because I'm constantly switching back and forth between C# and VB :((
-
It seems that problem is in semicolon after "if" statement. DoSomeThing will be fired any time the code executes.
-
Wich compiler ?
while (*dest++ = *source++);
is completely correct, isn't it ?
Klaus-Werner Konrad wrote:
Wich compiler?
FTFY: Witch compiler Actually, in this case the C# produces three useless wormings: both for the "while(...);" (an empty statment), "x=y" (an assigment instead of a comparison) and the "*" (an "unsafe" code), does it?
Greetings - Jacek
-
Does that even compile? /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
No, it doesn't. But that's just the bug: it doesn't compile! [EDIT] I'm sorry, I didn't see the semicolon after the
if
statement. That's the bug! :doh:The quick red ProgramFOX jumps right over the
Lazy<Dog>
. My latest article: Understand how bitwise operators work (C# and VB.NET examples) My group: C# Programmers Group -
Klaus-Werner Konrad wrote:
Wich compiler?
FTFY: Witch compiler Actually, in this case the C# produces three useless wormings: both for the "while(...);" (an empty statment), "x=y" (an assigment instead of a comparison) and the "*" (an "unsafe" code), does it?
Greetings - Jacek
Thanks for the correction. My example was - as a reply to the mention of C, of course a C code snippet, and is the full working function body for strcpy(). Of course, it's unsafe - but lightning fast :-)
-
Klaus-Werner Konrad wrote:
Wich compiler?
FTFY: Witch compiler Actually, in this case the C# produces three useless wormings: both for the "while(...);" (an empty statment), "x=y" (an assigment instead of a comparison) and the "*" (an "unsafe" code), does it?
Greetings - Jacek
They're not useless warnings, they're warning you that you did something unintended. Actually this wouldn't compile at all in C#, even with unsafe mode turned on, because the result type isn't boolean. It's a classic and well known piece of C code, and I think you only got a warning for the empty loop body (and if you did if(a = 3) by accident you were just screwed, hence writing if(3 == a) instead which is an error if you screw it up).
-
Thanks for the correction. My example was - as a reply to the mention of C, of course a C code snippet, and is the full working function body for strcpy(). Of course, it's unsafe - but lightning fast :-)
-
They're not useless warnings, they're warning you that you did something unintended. Actually this wouldn't compile at all in C#, even with unsafe mode turned on, because the result type isn't boolean. It's a classic and well known piece of C code, and I think you only got a warning for the empty loop body (and if you did if(a = 3) by accident you were just screwed, hence writing if(3 == a) instead which is an error if you screw it up).