你如何写一个无操作声明?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不知道为什么你需要那里的任何东西(例如我对“然后其他”感到满意)。
但如果你想要一些可编译的东西,我会这样做:
空的开始块是我所知道的 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:
An empty begin block is the best noop I know of in Delphi. It will produce no assembler code and thus no overhead.
应该像现在这样写
,可以注释掉SomeOldStatement;与您所追求的效果完全相同,调试器更准确地遵循代码流程,并且您可以避免代码中出现奇怪的副作用,例如
搞砸缩进,分号错误,记录一行用于测试和天哪,事情变得丑陋快速地。
说真的,尝试弄清楚我刚刚编写的代码在没有编译器通过的情况下是否有效。
现在,猜猜会发生什么:
另外:
经常会做奇怪的事情,甚至当你
认真做的时候更奇怪的事情 - 只要养成在你的所有逻辑中总是有开始结束的习惯 - 它使你的代码更容易遵循,避免侧面效果,避免心理解析错误、代码解析错误和注释掉副作用。
另外,空的开始/结束与您的空操作相同。
should be written as
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
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:
also:
can often do strange things, and even stranger things when you do
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.
在Delphi 2005及后续版本中,您可以定义一个
NoOp
空过程并将其标记为inline
。这样,除非您定义
{$INLINE OFF}
或在编译器选项中将代码内联控制设置为关闭,否则不会生成代码嗯>。生成的代码非常干净:
In Delphi 2005 and subsequent versions, you can define a
NoOp
empty procedure and mark it asinline
.This way no code is generated unless you define
{$INLINE OFF}
or set Code inlining control to Off in Compiler Options.The resulting code is very clean:
这是最好的选择。我广泛使用它进行调试,因为我可以在那里放置断点,不依赖于其他单元,不会以任何方式干扰您的程序,而且比条件断点快得多。
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.
您可以使用类似
a:=a
的内容,但说实话,我发现它甚至比非声明更丑陋 - 您应该编码,以便后面的内容您会明白您的意图,并且命令a:=a
并不真正遵循该准则。由于这只是一个临时的事情,我只想接受这样一个事实:其中没有可执行代码。如果,正如你所说,Delphi 仍然可以很好地编译它,那么你就没有问题。
如果您想要在其中添加一些代码作为断点,并且没有更好的方法来实现,我会考虑暂时执行
a:=a
操作。如果这将是一个更永久的更改,您可以考虑反转条件,以便根本没有空块:
或者更好的是:
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 commanda:=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:
or, better yet:
赋值怎么样,
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)
如果没有
begin end
块的语句是一个等待发生的错误,在这种情况下添加begin end
块将允许您注释掉您的行,而无需更改任何更多代码。If statements without a
begin end
block are a bug waiting to happen and in this case adding in abegin end
block will allow you to comment out your line without changing any more code.