首先,我知道这个问题: 代码中的注释是否有标准格式?
但它没有回答我的问题。每当我编写代码时,我总是试图养成确保注释全部一致的习惯,但这就是我有时对我的代码格式约定优柔寡断的地方。特别是因为我希望我的代码是 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?
*/