4

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;
Jørn E. Angeltveit
  • 3,029
  • 3
  • 22
  • 53
  • 1
    are you tried `asm nop end;`? – RRUZ Sep 24 '11 at 02:09
  • @RRUZ: Haven't tried but I have thought of this alternative. I guess it works fine, but it's not compatible with 64 bit where inline asm illegal. – Jørn E. Angeltveit Sep 24 '11 at 02:14
  • `procedure Noop; asm nop end;` would work. And I guess that is the best solution. You should add your suggestion as an answer :-) – Jørn E. Angeltveit Sep 24 '11 at 02:16
  • 1
    Instead of "//SomeOldStatement" I prefer "//DoNothing". You can also use Assert(a=b) in this instance. – Bob A Sep 24 '11 at 02:17
  • @Bob: The reference to the old name is there because it should be easy to un-comment and reactivate the original method call. `Assert` is a nice suggestion that hadn't thought of. – Jørn E. Angeltveit Sep 24 '11 at 02:20
  • 1
    @JørnE.Angeltveit, but if you put the `nop` instruction in a procedure called `Noop` you are adding a JMP and RET instructions and this does not have the same practical effect which adding directly the `nop` instruction. in this case is not better only has a empty procedure `procedure Noop; begin end;`? – RRUZ Sep 24 '11 at 02:27
  • @RRUZ: At the asm-level: yes, that is true. But the extra `jmp/ret` wasn't my main concern. I was wondering what I should write in the empty `then` section. And of cource - an empty method is just as good as an asm-method with one `nop`-call. :-] – Jørn E. Angeltveit Sep 24 '11 at 02:38
  • @JørnE.Angeltveit, I just put `begin {do nothing} end` so it's blindingly obvious. IMO code is written mostly for humans. – Johan Sep 24 '11 at 05:09
  • @Downvoter, I don't think that the google result you refer to provided any good answers. I was asking for a simple way to do a single no-op call instead of using an empty `begin-end` block (which btw turns into an empty statement). – Jørn E. Angeltveit Sep 24 '11 at 12:51
  • @Jørn E. Angeltveit - The nice thing about using assertions is the code generation compiler switch. (Checked or True = Generate code and evaluate, Unchecked or false = Ignore) – Bob A Sep 24 '11 at 15:10
  • @Jørn E. Angeltveit, you assumed i did not check result google provided, however i did, and there are good aswers as subject is amongst very basics of Pascal language. – Premature Optimization Sep 24 '11 at 18:39
  • @Downvoter. No, I didn't assume that. Why don't you link directly to the answers you have in mind, then? And I don't need any Pascal basics, btw. I have worked with this language for a while, and none of the answers/comment so far have tought me anything I new. Thats the reason for the question. Was I missing anything? – Jørn E. Angeltveit Sep 25 '11 at 04:30
  • @Jørn E. Angeltveit, because google's results are good and are the answers. If you are unsatisfied with them - acquire a copy of ISO 7185. If you knew already about *empty statement* Pascal language concept - why this question is here anyways? – Premature Optimization Sep 25 '11 at 14:02

7 Answers7

17

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.

lkessler
  • 19,819
  • 36
  • 132
  • 203
  • I guess that an empty `begin-end` block is the best solution. (That's actually what I implemented while waiting of some better alternatives). The `;` needs to go, BTW... – Jørn E. Angeltveit Sep 24 '11 at 03:31
  • 3
    I really like the comment `// DO NOTHING` as a comment inside the empty begin/end block. It makes the code read through cleanly. – Warren P Sep 24 '11 at 14:29
  • You can use a semicolon as an empty statement ( ; //Do nothing). This is actually useful in a case statement when you want one case to be 'do nothing' so that it doesn't fall into the Else condition. – Guy Gordon May 22 '23 at 13:40
12
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.

C Johnson
  • 131
  • 2
7

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;
JRL
  • 3,363
  • 24
  • 36
4

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;
Alexandre M
  • 1,146
  • 12
  • 23
3

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;
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I agree that one should do some parallel programming with De Morgan in the situations where this is the final code. The reason why I try to avoid empty statements are described here: http://delphi.about.com/od/beginners/a/delphi-if-then-else-traps.htm – Jørn E. Angeltveit Sep 24 '11 at 02:46
  • The optimizing compiler will probably optimize the a:=a away, so you won't get a breakpoint. – lkessler Sep 24 '11 at 03:12
  • Setting "a := a" for an empty statement is costly and you may get buggy codes in case "a" type is an managed one (the value can be cleared out). – APZ28 Sep 24 '11 at 14:37
2

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)

Serhii Kheilyk
  • 933
  • 1
  • 8
  • 24
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

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.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124