你如何写一个无操作声明?

发布于 2024-12-06 09:27:14 字数 515 浏览 5 评论 0原文

在 Delphi 中编写 no-op 语句 的最佳方法是什么?

采取以下代码:

if a=b then
  SomeOldStatement
else
  AnotherStatement;

并说您暂时想要删除 SomeOldStatement

你会选择这个解决方案吗:就我

if a=b then
  //SomeOldStatement
else
  AnotherStatement;

个人而言,我不喜欢空的 then 部分,并且希望其中可以编译一些东西...

if a=b then
  NoOp
  //SomeOldStatement
else
  AnotherStatement;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

邮友 2024-12-13 09:27:14

不知道为什么你需要那里的任何东西(例如我对“然后其他”感到满意)。

但如果你想要一些可编译的东西,我会这样做:

if a=b then
  begin end
  //SomeOldStatement
else
  AnotherStatement;

空的开始块是我所知道的 Delphi 中最好的 noop。它不会产生汇编代码,因此没有开销。

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.

爱你不解释 2024-12-13 09:27:14
if a=b then 
  SomeOldStatement 
else 
  AnotherStatement; 

应该像现在这样写

if a=b then
begin
  SomeOldStatement;
end 
else
begin
  AnotherStatement; 
end;

,可以注释掉SomeOldStatement;与您所追求的效果完全相同,调试器更准确地遵循代码流程,并且您可以避免代码中出现奇怪的副作用,例如

if a=b then
  if b=c then
    statement1
  else
    if c=d then
      statement2;
  else
   statement2
else 
  statement3;

搞砸缩进,分号错误,记录一行用于测试和天哪,事情变得丑陋快速地。

说真的,尝试弄清楚我刚刚编写的代码在没有编译器通过的情况下是否有效。

现在,猜猜会发生什么:

if a=b then
if b=c then
statement1
else
if c=d then
statement2;
// else
statement2
else 
statement3;

另外:

if a=b then
  statement1;
  statement2;

经常会做奇怪的事情,甚至当你

if a=b then
//  statement1;
statement2;

认真做的时候更奇怪的事情 - 只要养成在你的所有逻辑中总是有开始结束的习惯 - 它使你的代码更容易遵循,避免侧面效果,避免心理解析错误、代码解析错误和注释掉副作用。

另外,空的开始/结束与您的空操作相同。

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.

花开半夏魅人心 2024-12-13 09:27:14

在Delphi 2005及后续版本中,您可以定义一个NoOp空过程并将其标记为inline

这样,除非您定义 {$INLINE OFF} 或在编译器选项中将代码内联控制设置为关闭,否则不会生成代码嗯>。

procedure NoOp; inline;
begin
  // do nothing
end;

生成的代码非常干净:

if a=b then
  NoOp //SomeOldStatement
else
  AnotherStatement;

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;
难理解 2024-12-13 09:27:14

这是最好的选择。我广泛使用它进行调试,因为我可以在那里放置断点,不依赖于其他单元,不会以任何方式干扰您的程序,而且比条件断点快得多。

if a=b then
  asm nop end
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;
是伱的 2024-12-13 09:27:14

您可以使用类似 a:=a 的内容,但说实话,我发现它甚至比非声明更丑陋 - 您应该编码,以便后面的内容您会明白您的意图,并且命令 a:=a 并不真正遵循该准则。

由于这只是一个临时的事情,我只想接受这样一个事实:其中没有可执行代码。如果,正如你所说,Delphi 仍然可以很好地编译它,那么你就没有问题。

如果您想要在其中添加一些代码作为断点,并且没有更好的方法来实现,我会考虑暂时执行 a:=a 操作。

如果这将是一个更永久的更改,您可以考虑反转条件,以便根本没有空块:

if not (a = b) then
    AnotherStatement;

或者更好的是:

if a <> b then
    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;
浪漫人生路 2024-12-13 09:27:14

赋值怎么样,a := a?那是不行的。

(我不了解 Delphi,因此赋值的语法可能是错误的,但希望您能理解并在需要时更正语法)

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)

南街九尾狐 2024-12-13 09:27:14

如果没有 begin end 块的语句是一个等待发生的错误,在这种情况下添加 begin end 块将允许您注释掉您的行,​​而无需更改任何更多代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文