什么是一些好的代码格式?

时间:2011-01-22 02:25:43

标签: formatting comments code-formatting coding-style comment-conventions

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

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

毋庸置疑,代码格式约定应为:

  • 易于阅读
  • 易于修改

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

以下是一些例子:

单行

// 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.

多线

// 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?
*/

1 个答案:

答案 0 :(得分:1)

答案1 - 不要,任何时候浪费争论大括号都是浪费时间。

答案2 - 如果必须,请选择一个例如。 http://www.possibility.com/Cpp/CppCodingStandard.html