|
Which language? The C# compiler will give you a warning for that: "Possible mistaken empty statement".
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Even C back in the old days gave you a warning for that.
|
|
|
|
|
Wich compiler ?
while (*dest++ = *source++);
is completely correct, isn't it ?
|
|
|
|
|
It's a warning, not an error, for that reason. This was back when I used Zortech's ANSI C compiler.
|
|
|
|
|
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
|
|
|
|
|
Just in case you didn't get the joke there, he's making a funny about the compiler being witchcraft. The word you meant to use is 'which'.
|
|
|
|
|
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).
|
|
|
|
|
Right.
Greetings - Jacek
|
|
|
|
|
Does that even compile?
/ravi
|
|
|
|
|
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!
modified 15-Jun-13 11:22am.
|
|
|
|
|
It seems that problem is in semicolon after "if" statement.
DoSomeThing will be fired any time the code executes.
|
|
|
|
|
anton_l wrote: It seems that problem is in semicolon after "if" statement. It seems that's the reason he posted it!
|
|
|
|
|
This is one of the dangers of C syntax (and friends). That's the price you pay for willing conciseness. Block-only statements like VB's
If Condition Then
Statement
End If
or Modula's
IF Condition THEN
Statement
END
are safer.
|
|
|
|
|
Or Pascal:
if condition then
begin
DoStuff();
end
else
begin
DoOtherStuff();
end; Sort an 'are you sure?' prompt for every single conditional.
Software Zen: delete this;
|
|
|
|
|
Can't you do if condition then one-statement else other-statement though if you don't need a block?
|
|
|
|
|
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() and DoThing3() look like they're part of the if , but they're not. I do the same thing in C-style languages. If an if -statement occupies more than one line, it gets braced.
Software Zen: delete this;
|
|
|
|
|
I agree and would always put that on a single line.
|
|
|
|
|
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;
|
|
|
|
|
oh, urm, oops?
I do that...
|
|
|
|
|
It's valid syntax, and if you're confident that you'll never ever forget to add or remove braces appropriately, go for it.
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
|
|
|
|
|
Oh no, you misunderstood the guy who wrote that piece of code.
Never did he intend that DoSomeThing() is executed only when some codition is true. He just wanted to make his colleagues (who'll have to maintain his buggy code after he quit his job) believe so.
|
|
|
|
|
Seems like a new style of comment... if you're compiler doesn't support any comments.
"Program testing can be used to show the presence of bugs, but never to show their absence."
<< please vote!! >>
|
|
|
|