编码风格:每行 80 个字符对每个人来说都足够了吗?
大多数有关编码风格的文本建议将单行代码的长度限制为最多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论