How do you write a no-op statement?
What is the best way to write a no-op statement in Delphi?
Take this code:
if a=b then
SomeOldStatement
else
AnotherStatement;
And say that you temporarily want to rem out SomeOldStatement
.
Would you just go for this solution:
if a=b then
//SomeOldStatement
else
AnotherStatement;
Personally I don't like the empty then
section and would like to have something compilable in there...
if a=b then
NoOp
//SomeOldSt开发者_开发知识库atement
else
AnotherStatement;
Not sure why you need anything there at all (e.g. I'm happy with "then else").
But if you want something compilable there, I would do this:
if a=b then
begin end
//SomeOldStatement
else
AnotherStatement;
An empty begin block is the best noop I know of in Delphi. It will produce no assembler code and thus no overhead.
if a=b then
SomeOldStatement
else
AnotherStatement;
should be written as
if a=b then
begin
SomeOldStatement;
end
else
begin
AnotherStatement;
end;
now, you can comment out SomeOldStatement; with exactly the effect you are after, the debugger more accurately follows the flow of the code AND you avoid bizarre side effects in code like
if a=b then
if b=c then
statement1
else
if c=d then
statement2;
else
statement2
else
statement3;
screw up your indenting, get a semicolon wrong, document out a line for testing and holy crap, things get ugly fast.
seriously, try figuring out if the code I just wrote there is even valid without a compiler pass.
now, guess what happens with this:
if a=b then
if b=c then
statement1
else
if c=d then
statement2;
// else
statement2
else
statement3;
also:
if a=b then
statement1;
statement2;
can often do strange things, and even stranger things when you do
if a=b then
// statement1;
statement2;
serious - just get in the habit of ALWAYS having begin ends in all your logic - it makes your code easier to follow, avoids side effects, avoids mental parsing errors, code parsing errors and commenting out side effects.
Plus, an empty begin/end is the same as your no-op.
In Delphi 2005 and subsequent versions, you can define a NoOp
empty procedure and mark it as inline
.
This way no code is generated unless you define {$INLINE OFF}
or set Code inlining control to Off in Compiler Options.
procedure NoOp; inline;
begin
// do nothing
end;
The resulting code is very clean:
if a=b then
NoOp //SomeOldStatement
else
AnotherStatement;
That's the best option. I use it extensively for debugging, because I can put a breakpoint there, does not depend on another unit, does not interfere with your program in any way, and also is much faster than conditional breakpoints.
if a=b then
asm nop end
else
AnotherStatement;
You can possibly use something like a:=a
but, to be honest, I find that even uglier than a non-statement - you should code so that those that come after you will understand what you intended, and the command a:=a
doesn't really follow that guideline.
Since this is only a temporary thing, I would just wear the fact that you have no executable code in there. If, as you say, Delphi still compiles it just fine, you have no issue.
If you want some code in there for a breakpoint, and there's no better way of doing it, I would consider temporarily doing the a:=a
thing.
If it was going to be a more permanent change, you could instead consider the reversal of the condition so that you have no empty blocks at all:
if not (a = b) then
AnotherStatement;
or, better yet:
if a <> b then
AnotherStatement;
How about assignment, a := a
? That's a no-op.
(I don't know Delphi, so syntax for the assignment may be wrong, but hopefully you can get the idea and correct the syntax if needed)
If statements without a begin end
block are a bug waiting to happen and in this case adding in a begin end
block will allow you to comment out your line without changing any more code.
精彩评论