对于 2 个条件的并集,在 COBOL 中使用 IF 或 EVALUATE?
“如果有效,就不要碰它”……我明白。也就是说,我正在扩展的代码中布满了这样的块:
EVALUATE TRUE ALSO TRUE
WHEN FOO-YES ALSO BAR-YES
PERFORM ACTION
WHEN OTHER
SET ERROR TO TRUE
END-EVALUATE
对于我缺乏经验的人来说, IF 似乎更清晰:
IF FOO-YES AND BAR-YES
PERFORM ACTION
ELSE
SET ERROR TO TRUE
ENDIF
在编写新功能时,是否有理由更喜欢 EVALUATE 而不是 IF?
"If it works, don't touch it"...I understand. That said, the code I'm extending is studded with blocks like this:
EVALUATE TRUE ALSO TRUE
WHEN FOO-YES ALSO BAR-YES
PERFORM ACTION
WHEN OTHER
SET ERROR TO TRUE
END-EVALUATE
To my inexperienced eye, IF seems clearer:
IF FOO-YES AND BAR-YES
PERFORM ACTION
ELSE
SET ERROR TO TRUE
ENDIF
When writing new functionality, is there a reason to prefer EVALUATE over IF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有理由更喜欢 EVALUATE 而不是 IF。我愿意,但我没有充分的理由。我尝试编写代码,以便维护者很容易理解,但在某些情况下,逻辑很复杂,最终会得到如下结构:
某处有一句话说事情应该尽可能简单,但不能更简单。当然,有很多方法可以对相同的逻辑进行编码。不同的人发现不同的结构更容易理解。由于这些差异,出现了很多争论。
There is no reason to prefer EVALUATE over IF. I do, but I don't have a good reason for it. I try to code so it is easy for the maintainer to understand, but there are going to be cases where the logic is complex and you end up with constructs like...
Somewhere there's a quote about how things should be as simple as possible, but no simpler. There are, of course, many ways to code the same logic. Different people find different constructs easier to understand. Lots of arguments occur because of those differences.
你读对了。 “True ALSO True”对于编码来说是一件非常奇怪的事情。
通常您会看到“Evaluate True”,在这种情况下,when 条件的行为与 IF 语句完全相同。使用 ALSO 可能会带来一些奇怪的情况,因为它与所有其他语言中的 CASE 语句不同。
更好的写法是:
当然,TRUE ALSO TRUE 很容易理解,但是如果你写 TRUE ALSO WS-BLAH-BLAH,这可能会让人感到困惑。 Cobol Evaluate 动词非常强大,有时很容易搬起石头砸自己的脚。也就是说,它非常强大,可以让你做很多事情。
通常情况下,嵌套不清晰的复杂 IF 可以通过编写良好的 Evaluate 很好地修复。
You read it correctly. "True ALSO True" is a very odd thing to code.
Often you will see an "Evaluate True", in which case the when conditions all act EXACTLY like IF statements. Using ALSO introduces some possible oddities, as it isn't just like the CASE statements from all the other languages.
A nicer way to write this is:
Granted, TRUE ALSO TRUE is very easy to understand, but you could have TRUE ALSO WS-BLAH-BLAH and it could get confusing. The Cobol Evaluate verb is very powerful, and sometimes easy to shoot yourself in the foot with. That said, it is very powerful and will let you do alot.
It is often the case that convoluted IF's that don't nest cleanly can be fixed up nicely with a well written Evaluate.
我可能会说,在某些情况下这会让生活变得轻松。举例来说,我们将在哪里添加另一个条件,根据该条件我们可能需要
执行
不同的代码块;在这种情况下,附加另一个WHEN
会比调整IF
上的条件更容易。以下是将WHEN
与调整IF
条件进行比较时的要点:WHEN(s)
基于要执行的PARAs
。OR
或AND
参数时,IF
条件很容易出错。WHEN
可以清晰地显示事物的流程因为我们在分析IF
时可能需要调整我们的眼镜。 [当我们有复杂的循环时,可能会发生这种情况,但作为一名高效的程序员,我们应该预测这种情况]但在您当前的情况下,我感觉
EVALUATE
和IF
之间的清晰度和区别不太明显。所以,选择让你快乐的那个吧:)I may say that makes the life easy in certain situations. Say for instance where we'll get to add another condition based on which we may need to
PERFORM
different chunk of code; in that case appending anotherWHEN
would be easy rather than adjusting the conditions onIF
. Here are the points when comparedWHEN
against adjusting conditions onIF
:WHEN(s)
as we want based on thePARAs
to be performed.IF
conditions are prone to mistakes while working withOR
orAND
parametersWHEN
gives clear picture of how things flow where as we may have to adjust our glasses while analyzingIF
(s). [This may happen when we have complicated loops, but as an efficient programmer we ought to forecast such scenarios]IFs
.But in your current case, i dont feel much clarity and as well difference between
EVALUATE
andIF
. So, go for the one which makes you happy :)对我来说,这一切都取决于可读性(因此,以后的维护也更简单)。
当我查看您问题中的 2 个代码段时,我发现
IF
块更具可读性。在 22 年的 Cobol 编程生涯中,我从未在
EVALUATE
中使用过ALSO
。使用
EVALUATE
块进行简单的二进制真/假测试对我来说似乎有点迟钝。但不要误会我的意思,如果使用得当,我喜欢
EVALUATE
。 6 年前,当我开始使用 Java 编程时,EVALUATE
是我最怀念的东西。当然,Java 有switch
,但它不能像EVALUATE
那样灵活使用。考虑以下代码段:
这个
IF
语句集合存在一些实际问题。如果不在其末尾添加 5 个END-IF
,则必须以句号结束该块。但是,如果代码位于PERFORM UNTIL
...END-PERFORM
块内,您将无法执行此操作。虽然这个示例相当简单,但在更复杂(更长)的示例中,弄清楚哪个ELSE
与哪个IF
匹配可能会很棘手。迫切需要将其放入
EVALUATE TRUE
块中,因此:将
EVALUATE
更改为TRUE ALSO TRUE
会使其可读性大大降低,而且确实没有必要。To me it all comes down to readability (and hence, simpler maintenance later on).
When I look at the 2 code segments in your question, I find the
IF
block far more readable.In 22 years of Cobol programming I have never used an
ALSO
in anEVALUATE
.And using an
EVALUATE
block for a simple binary true/false test seems a bit obtuse to me.But don't get me wrong, I love
EVALUATE
s when used appropriately. When I started programming in Java 6 years ago,EVALUATE
was the thing I missed most. Sure, Java hasswitch
, but that can't be used as flexibly asEVALUATE
.Consider the following code segment:
This collection of
IF
statements has some real problems. Without adding 5END-IF
s to the end of it, you have to end the block with a full-stop. But you wouldn't be able to do that if the code was inside aPERFORM UNTIL
...END-PERFORM
block, say. While this example is fairly simple, in a more complicated (and longer) example, it could get tricky to figure out whichELSE
goes with whichIF
.This is crying out to be put in an
EVALUATE TRUE
block, thus:Changing this
EVALUATE
to haveTRUE ALSO TRUE
would make it far less readable, and it really isn't necessary.