【发布时间】:2010-07-07 18:36:17
【问题描述】:
以下代码是消息批处理例程的概念证明。我是否像瘟疫一样避免goto并重写这段代码?还是您认为goto 是完成此任务的一种表达方式?
如果您要重写,请发布一些代码...
var queue = new Queue<TraceItem>(this.batch);
while (this.connected)
{
byte[] buffer = null;
try
{
socket.Recv(out buffer);
}
catch
{
// ignore the exception we get when the socket is shut down from another thread
// the connected flag will be set to false and we'll break the loop
}
HaveAnotherMessage:
if (buffer != null)
{
try
{
var item = TraceItemSerializer.FromBytes(buffer);
if (item != null)
{
queue.Enqueue(item);
buffer = null;
if (queue.Count < this.batch && socket.Recv(out buffer, ZMQ.NOBLOCK))
{
goto HaveAnotherMessage;
}
}
}
catch (Exception ex)
{
this.ReceiverPerformanceCounter.IncrementDiagnosticExceptions();
this.tracer.TraceException(TraceEventType.Error, 0, ex);
}
}
// queue processing code
}
【问题讨论】:
-
[ 你怎么看 ](xkcd.com/292)?
-
在这个问题中,“富有表现力”这个词的意思是:“糟糕而令人尴尬,但我也许可以用一个冗长的标签来合理化它。”重写它。并在您使用时重新调整它。
-
至少你使用了一个命名标签;我继承了一些“VB.NET”代码,其中包含许多带有 numeric 标签的 Goto。它让我想起了 GWBasic,行号增加了 10,以防您以后可能需要插入一些新代码。请可怜可怜的维护工程师,他们以后将拥有您的代码并避免诱惑。
-
@Dan:我记得必须在任何给定部分添加超过 10 行的恐惧。
-
除了我之外还有其他人怀疑过去几天问了多少与 goto 相关的问题吗?