PL/SQL 中 IF 块的奇怪编码风格
在我工作的地方,我看到很多用 PL/SQL 编写的以下类型的代码,
IF a>b THEN
NULL;
ELSE
c:=a*b;
END IF;
我觉得这很奇怪,因为 C 等效代码看起来像这样,
if (a>b)
{
}
else
{
c=a*b;
}
并且上述类型的代码在 C 论坛中被认为是糟糕的风格我知道新手发帖的时候。 由于 PL/SQL 不允许空块并且始终需要 NULL 语句,因此这种类型的编码风格是否会带来可读性方面的优势,还是只是一个偏好问题? FWIW,用上述风格编写 PL/SQL 代码的人显然是一位经验丰富的程序员。 和下面的相比有什么优势吗?
IF a<=b THEN
c:=a*b;
END IF;
Where I work, I see lot's of the following type of code written in PL/SQL,
IF a>b THEN
NULL;
ELSE
c:=a*b;
END IF;
I find this odd because a C equivalent would look like this,
if (a>b)
{
}
else
{
c=a*b;
}
And the above type of code has been frowned upon as bad style in a C forum that I know when posted by newbies.
As PL/SQL doesn't allow empty blocks and always require a NULL statement, does this type of coding style bring any advantages w.r.t readability or is it just a matter of preference?. FWIW, the guy who coded PL/SQL with the above style certainly seems to be a seasoned coder.
Is there any advantage compared to the following?
IF a<=b THEN
c:=a*b;
END IF;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果任何部分为 null,则似乎
a <= b
将不为真。这就是这两种说法不同的原因。在第一种情况下,如果
a
为 null,则c = a*b
将运行。在第二种情况下,则不会。除非您确定a
和b
不为 null,否则等效语句将是:It seems that
a <= b
will not be true if any part is null. That's why these two statements are different.In the first case,
c = a*b
will run ifa
is null. In the second case, it won't. Unless you know for certain thata
andb
are not null, the equivalent statement would instead be:在我工作的地方,我们不喜欢有空块,因此我们总是像下面的示例一样编写我们的代码,而不使用
ELSE IF
。我认为这在很大程度上是一个偏好问题,但我宁愿像您最后所做的那样反转 IF 语句以避免出现空块;我发现代码更容易阅读。Where I work, we do not like to have the empty blocks, so we always code ours like your bottom example, without the
ELSE IF
. I think it's largely a matter of preference though, but I would rather reverse theIF
statement as you did at the end to avoid the empty block; I find the code easier to read.