优先规则 == 高于 =

发布于 2024-08-31 01:30:37 字数 327 浏览 9 评论 0原文

我只是想知道这样做会更好:

if((fd = open(filename, O_RDWR)) == -1)
{
fprintf(stderr, "open [ %s ]\n", strerror(errno));

return 1;
}

还是这样

fd = open(filename, O_RDWR);
if(fd == -1)
{
    fprintf(stderr, "open [ %s ]\n", strerror(errno));

    return 1;
}

非常感谢您的任何建议,

I am just wondering would it be better to do this:

if((fd = open(filename, O_RDWR)) == -1)
{
fprintf(stderr, "open [ %s ]\n", strerror(errno));

return 1;
}

or this

fd = open(filename, O_RDWR);
if(fd == -1)
{
    fprintf(stderr, "open [ %s ]\n", strerror(errno));

    return 1;
}

Many thanks for any suggestions,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

左耳近心 2024-09-07 01:30:37

咳咳,分了吧。将所有内容混合在一条线上会得到什么?我们来比较一下:

单行:

  • 优点:
  • 缺点: 难以阅读,容易出错。 (考虑您的第一次修订。)

多行:

  • 优点:易于阅读,不易出错。
  • 缺点:

我想已经很清楚了。 :)


“有时把它放在一行上更有意义,例如: while ((c=getchar())!=EOF)

很好,但这里不是这种情况。有时,不拆分更有意义,但一般情况下,这样做是不可行的。


“它节省了更多的垂直空间”

如果一行阻碍了您查看函数的能力,则您需要 1) 购买分辨率高于 640x480 的显示器,并且 2) 编写较小的函数。

真的,我从来不明白任何东西的论点,函数应该轻松适合任何屏幕,无论一行差异如何。


“多行使它看起来很复杂”

并非如此,将其推到一行可以说更难阅读并且看起来更复杂。将事情分开使得一次处理一位变得更简单,人们不应该假设两行会使它复杂一倍。

Yuck, split it up. What do you gain by mashing it all on one line? Let's compare and contrast:

Single-line:

  • Advantages:
  • Disadvantages: Hard to read, prone to error. (Consider your first revision.)

Multi-line:

  • Advantages: Easy to read, less error-prone.
  • Disadvantages:

I think it's clear. :)


"Sometimes putting it on one line makes more sense, for example: while ((c=getchar())!=EOF)"

That's fine, but that isn't the case here. There are times when not splitting it up makes more sense, but in general, don't.


"It saves more vertical space"

If one line is killing your ability to see the function, you need to 1) buy a monitor with a resolution higher than 640x480, and 2) Write smaller functions.

Really, I've never understood that argument for anything, functions should easily fit on any screen, regardless of a one-line difference.


"Multiple lines make it look complex"

Not really, shoving it on one line is arguably harder to read and more complex looking. Splitting things up makes it simpler to process one bit at a time, one shouldn't assume two lines makes it twice as complex.

も让我眼熟你 2024-09-07 01:30:37

有几个人赞成第二种观点。我不同意他们的观点。虽然(显然)最初在第一个中 === 存在一个小问题,但我认为这是一个小问题。

一个更大的问题是,人们(尤其是在赶时间的情况下)跳过错误检查是很常见的——完全忽略 if (whatever == -1),通常的理论是,他们正在处理的是快速的、一次性的代码,并且实际上并不需要检查错误。这是一个非常的坏习惯;我几乎可以保证每个读到这篇文章的人都看到过像这样跳过错误检查的真实代码,即使它真的、真的应该有它。

在这样的代码中,尝试打开文件并检查这样做的错误应该密不可分地结合在一起。将两者放在同一个声明中反映了正确的意图。将两者分开是完全错误的——任何时候、任何时间、任何原因,都不应该将它们分开。这应该被编码为单个操作,因为它应该单个操作。应始终将其视为单个操作并将其编码。

在我看来,不这样做的借口是非常站不住脚的。现实情况是,任何使用 C 的人都需要能够阅读将赋值与条件测试相结合的代码。仅举一个明显的例子,像 while ((ch=getchar()) != EOF) 这样的循环几乎需要编写为组合的赋值和测试 -尝试单独测试 EOF 通常会导致代码无法正常工作,即使您确实使其正常工作,代码也会变得更加复杂。

同样,还有 -== 的问题。由于我一开始就没有看到缺陷,因此我不确定将两者分开多少以避免出现问题,但我的直接猜测是,这可能几乎没有任何区别。当本来应该是一个条件只包含一个赋值时,编译器会警告您,这些编译器已经存在多年了(例如 gcc)。在大多数情况下,症状几乎是立即显而易见的——简而言之,您在这篇文章的一个部分中犯了一个特定的拼写错误,但在另一个部分却没有,这一事实并不能证明(或者说实话,甚至表明)有关该亲属的任何信息。两者的难度。

基于此类证据,我显然相信“not”比“immediately”更难输入,因为我只是毫无问题地输入了“immediately”,但必须纠正“not”(两次,同样)在上一句中出现之前。我很确定,如果我们看看我打错的频率,“the”是英语中最难的单词。

Several people have argued in favor of the second. I disagree with them. While there was (apparently) initially a minor issue with = vs. == in the first one, I'd argue that it IS a minor issue.

A much bigger issue is that it's all too common for people (especially if they're in a hurry) to skip over the error checking -- leaving out the if (whatever == -1) completely, usually on the theory that what they're working on is quick, throwaway code and checking for the error isn't really needed. This is a really bad habit; I can practically guarantee that every person reading this has seen real code that skipped error checking like this, even though it really, really should have had it.

In code like this, attempting to open the file and checking for an error in having done so should be inextricably bound together. Putting the two into the same statement reflects the proper intent. Separating the two is just plain wrong -- they should not be separated in any way at any time for any reason. This should be coded as a single operation because it should be a single operation. It should always be thought of and coded as a single operation.

The excuses for doing otherwise are, in my opinion, quite weak. The reality is that anybody who uses C needs to be able to read code that combines an assignment with a conditional test. Just for an obvious example, a loop like while ((ch=getchar()) != EOF) pretty much needs to be written as a combined assignment and test -- attempting to test for EOF separately usually leads to code that just doesn't work correctly, and if you do make it work correctly, the code is substantially more complex.

Likewise, with the problem of - vs. ==. Since I didn't see the defect to start with, I'm not sure how much separating the two would have done to avoid problems, but my immediate guess is that it probably made almost no difference at all. Compilers that will warn you when what was supposed to be a condition contains only an assignment have been around for years (e.g. gcc). In most cases, the symptoms are almost immediately obvious anyway -- in short, the fact that you made a particular typo in one part of this posting but not the other doesn't prove (or honestly even indicate) much of anything about the relative difficulty of the two.

Based on that kind of evidence, I'd apparently believe that "not" is harder to type than "immediately", since I just typed "immediately" without a problem, but had to correct "not" (twice, no less) before it came out right in the previous sentence. I'm pretty sure if we went by how often I mistype it, "the" is the single most difficult word in the English language.

白首有我共你 2024-09-07 01:30:37

也许括号使排序变得明显?

if((fd = open(filename, O_RDWR)) == -1)

Maybe something where the parentheses make the ordering obvious?

if((fd = open(filename, O_RDWR)) == -1)
姜生凉生 2024-09-07 01:30:37

在这个例子中,我将同意第二种方法更好。

更困难的情况是当它处于循环中时,例如:

while ((c=getchar())!=-1)
{
  ... do something ...
}

while (true)
{
  c=getchar();
  if (c==-1)
    break;
  ... do something ...
}

在这种情况下,我更喜欢在一行中执行此操作,因为这样可以清楚地表明控制循环的内容,我认为这克服了分配和测试的复杂组合的缺点。

In this example, I'll join the chorus saying the second method is better.

The tougher case is when it's in a loop, like:

while ((c=getchar())!=-1)
{
  ... do something ...
}

versus

while (true)
{
  c=getchar();
  if (c==-1)
    break;
  ... do something ...
}

In cases like that I prefer to do it on one line because then it makes clear what is controlling the loop, which I think overrides the drawbacks of the complex combination of assignment and testing.

心舞飞扬 2024-09-07 01:30:37

这是一种风格——你不要求优先权(不是总统)。

许多人会认为后一个例子更清楚。

Its a style thing - you're not asking a precedence (not presidence).

Many people will argue the latter example is clearer.

路还长,别太狂 2024-09-07 01:30:37

为了可读性,第二个更好,但我知道我经常做第一个。 = 运算符将优先,特别是因为您将其放在引号中,从而允许返回分配的值并返回值。通过 == 运算符进行比较。

The second is better for readability's sake, but I know I do the first too often. The = operator will take precedence, especially since you have it in quotes, allowing the assigned value to be returned & compared by the == operator.

归途 2024-09-07 01:30:37

除了标准的习语——那些非常常见以至于每个人都能立即明白你想要做什么的习语——我会避免在条件句中做作业。首先,它更难阅读。其次,您可能会通过在条件检查中使用错误的赋值运算符来创建错误(至少在将零解释为假、将非零解释为真的弱类型语言中)。

Except for standard idioms -- ones that are so common that everyone immediately gets what you are trying to do -- I would avoid doing the assignment in the conditional. First, it's more difficult to read. Second, you leave yourself open (at least in weakly typed languages that interpret zero as false and non-zero as true) to creating bugs by using an errant assignment operator in the conditional check.

﹏雨一样淡蓝的深情 2024-09-07 01:30:37

这是一个风格问题并且是主观的。他们做同样的事情。我倾向于更喜欢后者,因为我发现它更容易阅读,并且更容易在调试器中设置断点/检查变量。

This is a matter of style and is subjective. They do the same thing. I tend to prefer the later because I find it easier to read, and easier to set breakpoints/examine variables in the debugger.

柒七 2024-09-07 01:30:37
(-1 == __whatever__) 

尽量减少拼写错误

(-1 == __whatever__) 

to minimize typo

傾城如夢未必闌珊 2024-09-07 01:30:37

当您编写输入循环时,第一种情况非常,因为另一种方法是必须编写输入命令两次 - 一次在循环之前,一次在循环末尾环形。

while ( (ch=getchar()) != -1){
  //do something with it
}

我认为第二种方式对于 if 语句来说更正常,你不会有同样的担忧。

The first case is very normal when you're writing an input loop, because the alternative is to have to write the input command twice -- once right before the loop, and once right at the end of the loop.

while ( (ch=getchar()) != -1){
  //do something with it
}

I think that the second way is more normal for an if statement, where you don't have the same concern.

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