首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是好的代码格式?

什么是好的代码格式?
EN

Stack Overflow用户
提问于 2011-01-22 02:25:42
回答 1查看 203关注 0票数 0

首先,我知道这个问题:Are there standard formats for comments within code?

但它没有回答我的问题。每当我编写代码时,我总是试图养成一种习惯,即确保所有注释都是一致的,但这正是我有时对代码格式约定犹豫不决的地方。特别是因为我希望我的代码宽80行左右。

不用说,代码格式约定应该是:

  • 易读
  • 易于修改

代码的不同部分可能有不同的注释格式,因此确保它们保持一致和可读性是很重要的。

下面是一些示例:

单线

代码语言:javascript
复制
// simple comment in lowercase
do_something();

// longer comment in lowercase. but now it doesn't seem like a proper sentence.
do_something();

// Longer comment in sentence case. But should we use sentence case for short comments too?
do_something();
// Like this one?
do_something_else(); // Or even this one.

多线

代码语言:javascript
复制
// This code is very detailed and so I must continue further.
// Every time you start a new line you need to ensure you add //.
// If you finish your comment and forget to add something, you're screwed.
// You'll have to add your something and then make sure all the lines are 80 characters or less,
// and if they aren't you need to restructure all the lines below it.

/*  This is better but you need to make sure it's properly indented each line.
    Although you have to worry about those comment delimiters getting in the way. 
    At least it's compact. */

/*  This comment seems better.
    No need to worry about that last delimiter being inside your comment block.
*/

/*  
    Or maybe this is better?
    At least it looks clean.
*/

/*  GOOD FOR FUNCTION DEFINITIONS
    Or maybe this is even better?
    Who knows?
*/
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-22 02:31:15

答案1--不要,任何浪费在支撑上的时间都是浪费时间。

回答2--如果你必须的话,就选一个。http://www.possibility.com/Cpp/CppCodingStandard.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4765775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档