代码行换行约定?

发布于 2024-08-20 05:35:50 字数 508 浏览 5 评论 0原文

我通常将代码行换行,使其长度达到 80 个字符。

哪种包装对您来说更好?

// (A)
std::vector<MyLongClassName::size_type>* myvector
    = new std::vector<MyLongClassName::size_type>();
bool isOneOrAnother = hereIsOneLongCondition
    && hereIsAnotherOne;

// (B)
std::vector<MyLongClassName::size_type>* myvector =
    new std::vector<MyLongClassName::size_type>();
bool isOneOrAnother = hereIsOneLongCondition &&
    hereIsAnotherOne;

我知道这是任意的,但是有约定或首选方式吗?

I usually wrap my code lines so that they are up tp 80 characters long.

Which wrapping looks better to you?

// (A)
std::vector<MyLongClassName::size_type>* myvector
    = new std::vector<MyLongClassName::size_type>();
bool isOneOrAnother = hereIsOneLongCondition
    && hereIsAnotherOne;

// (B)
std::vector<MyLongClassName::size_type>* myvector =
    new std::vector<MyLongClassName::size_type>();
bool isOneOrAnother = hereIsOneLongCondition &&
    hereIsAnotherOne;

I know it is arbitrary, but is there a convention or a preferred way?

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

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

发布评论

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

评论(5

顾北清歌寒 2024-08-27 05:35:50

我会选择 (B),但对于布尔值,我可能会添加不完全必要的括号,然后将值排列在其中。我添加括号只是因为如果没有它们我的 emacs 就无法为我做这件事。

bool isOneOrAnother = ( hereIsOneLongCondition &&
                        hereIsAnotherOne );

I'd choose (B), but for the boolean, I might add not-completely-necessary parens and then line the values up within them. I'd add the parens only because my emacs won't do it for me without them.

bool isOneOrAnother = ( hereIsOneLongCondition &&
                        hereIsAnotherOne );
仅一夜美梦 2024-08-27 05:35:50

第二条语句的另一种可能性(有时):

bool isOneOrAnother =
   hereIsOneLongCondition && hereIsAnotherOne;

Another possibility for the 2nd statement (sometimes):

bool isOneOrAnother =
   hereIsOneLongCondition && hereIsAnotherOne;
梦途 2024-08-27 05:35:50

我个人指的是将任何运算符放在行尾。我相信,微软也推荐这种做法,尽管我目前找不到该页面。然而,C# 风格指南推荐了同样的东西。 。

当表达式不适合
单行,根据
这些一般原则:
# 逗号后换行。
# 在运算符后中断。
# 优先选择较高级别的休息而非较低级别的休息。
# 将新行与表达式的开头对齐
与上一行相同的级别。

根据我的记忆,我读过的大多数代码也倾向于遵循这个规则。如果您正在寻找这背后的基本原理,我真正可以提供的唯一一件事是,用运算符开始一行是没有意义的,因为您实际上是在开始一个新的(子)表达式,从而使其更容易在心理上分开。此外,如果有人逐行阅读代码,如果当前行末尾有一个悬挂运算符,而不是下一行的开头,则更容易注意到行延续。

希望有帮助。

I personally refer to put any operator at the end of a line. Microsoft also recommends this practice, I believe, though I can't find the page at the moment. The C# style guide here, however, recommends the same thing...

When an expression will not fit on a
single line, break it up according to
these general principles:
# Break after a comma.
# Break after an operator.
# Prefer higher-level breaks to lower-level breaks.
# Align the new line with the beginning of the expression at the
same level on the previous line.

Going my memory, most of the code I've read tends to follow this rule too. If you're looking for rationale behind this, the only thing I can really offer is that it doesn't make sense to start a line with an operator because you're effectively starting a new (sub-)expression, and thus makes it easier to separate mentally. Also, if someone is reading code line by line, it's easier to notice a line continuation if there's a hanging operator at the end of the current line, rather than the start of the next line.

Hope that helps.

悟红尘 2024-08-27 05:35:50

我是另一个更喜欢 (B) 的人,但对于第二个说法我更喜欢:

bool isOneOrAnother =
         hereIsOneLongCondition &&
         hereIsAnotherOne;

喜欢 (B) 的原因来自于习惯了 javascript 的 。损坏的 分号插入语法。由于我经常在多语言环境中工作,因此养成破坏我使用的其中一种语言的习惯对我来说是不好的。

I'm another one who prefers (B) but for the second statement I prefer:

bool isOneOrAnother =
         hereIsOneLongCondition &&
         hereIsAnotherOne;

The reason for preferring (B) comes from being used to javascript's <cough> broken </cough> semicolon insertion syntax. Since I often work in a multi-language environment it is bad for me to acquire habits that breaks one of the languages I use.

浅听莫相离 2024-08-27 05:35:50

我赞同格雷姆·佩罗的回答。在某些情况下,我

bool someCondition = ( hereIsOneLongCondition &&
                       hereIsAnotherOne &&
                       hereIsYetAnother &&
                       ohNoHereIsMore);

倾向于将表达式放在函数中:

bool someCondition = Foo();

这样的好处是能够更轻松地编辑 Foo() -我可以评论单独的行:

bool Foo()
{

   bool result = true;

   if (!hereIsOneLongCondition)
   {
      result = false;
   }

   if (!hereIsAnotherOne)
   {
      // this was added to fix such-and-such bug
      result = false;
   }

   // etc

   return result;
}

上面,我说一些,因为这并不总是可能的,而且它确实会产生更多的代码行。不过,发现错误和重构更容易。

I second Graeme Perrow's answer. In some cases where I have something like

bool someCondition = ( hereIsOneLongCondition &&
                       hereIsAnotherOne &&
                       hereIsYetAnother &&
                       ohNoHereIsMore);

I tend instead put the expression in a function:

bool someCondition = Foo();

which has the benefit of being able to allow Foo() to be edited more easily - I can comment individual lines:

bool Foo()
{

   bool result = true;

   if (!hereIsOneLongCondition)
   {
      result = false;
   }

   if (!hereIsAnotherOne)
   {
      // this was added to fix such-and-such bug
      result = false;
   }

   // etc

   return result;
}

Above, I said some since this isn't always possible, and it does produce more lines of code. Spotting bugs and refactoring is easier, though.

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