PL/SQL 中 IF 块的奇怪编码风格

发布于 2024-12-11 18:55:16 字数 431 浏览 0 评论 0原文

在我工作的地方,我看到很多用 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 技术交流群。

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

发布评论

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

评论(2

装纯掩盖桑 2024-12-18 18:55:16

如果任何部分为 null,则似乎 a <= b为真。这就是这两种说法不同的原因。

在第一种情况下,如果 a 为 null,则 c = a*b 将运行。在第二种情况下,则不会。除非您确定 ab 不为 null,否则等效语句将是:

IF a<=b or (a is null) or (b is null) THEN
  c:=a*b;
END IF;

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 if a is null. In the second case, it won't. Unless you know for certain that a and b are not null, the equivalent statement would instead be:

IF a<=b or (a is null) or (b is null) THEN
  c:=a*b;
END IF;
緦唸λ蓇 2024-12-18 18:55:16

在我工作的地方,我们不喜欢有空块,因此我们总是像下面的示例一样编写我们的代码,而不使用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 the IF statement as you did at the end to avoid the empty block; I find the code easier to read.

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