编码风格:每行 80 个字符对每个人来说都足够了吗?

发布于 10-27 13:26 字数 1039 浏览 3 评论 0原文

大多数有关编码风格的文本建议将单行代码的长度限制为最多 80 个字符。然而,绝大多数常见的输出设备完全能够超过这个限制。

为什么这个限制仍然提倡如此严格?


I.

在某些情况下,我可以看到由于可读性增加而带来的优势,即当返回布尔表达式的值时:

int is_foo(void *x)
{
        /* this would look like magic without linebreaks */

        return (isint(*x) && \
                (*x > 23) && \
                (x != 0xDEADBEEF) && \
                bar(*x));
 }

II.

另一方面,我不认为不认为迫切需要将以下语句拆分为多行:

snprintf(buf, BUFSIZ, "The big brown fox makes this LOC exceed its %d character limit", 80);

/* is IMO superior to: */
snprintf(buf, BUFSIZ, \
         "The big brown fox makes this LOC exceed its %d character limit", \
         80);

III.

当然,在某些情况下,行拆分是缺乏清晰的代码结构的糟糕借口:

for (i = 0, *foo = bar, hash_init(); \
     is_my_kungfu_already_that_old("bruce") && !list_empty(foo[i]); \
     ++i, ++j, --k, *p++ = *q--) {
        /* whatever */
}

Most texts about coding style suggest to limit the length of a single line of code to at most 80 characters. However, the vast majority of common output devices is perfectly capable of exceeding this limit.

Why is this limit still advocated so strictly?

I.

There are some cases where I can see an advantage due to increased readability, i.e. when returning the value of a boolean expression:

int is_foo(void *x)
{
        /* this would look like magic without linebreaks */

        return (isint(*x) && \
                (*x > 23) && \
                (x != 0xDEADBEEF) && \
                bar(*x));
 }

II.

On the other hand, I don't see an urgent need to split the following statement across multiple lines:

snprintf(buf, BUFSIZ, "The big brown fox makes this LOC exceed its %d character limit", 80);

/* is IMO superior to: */
snprintf(buf, BUFSIZ, \
         "The big brown fox makes this LOC exceed its %d character limit", \
         80);

III.

And of course, there are cases where line splitting is a bad excuse for the lack of a clear structure of the code:

for (i = 0, *foo = bar, hash_init(); \
     is_my_kungfu_already_that_old("bruce") && !list_empty(foo[i]); \
     ++i, ++j, --k, *p++ = *q--) {
        /* whatever */
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文