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
//SomeOldStatement
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With