Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sonar, java and 'trailing comment' violation

After running Sonar on one of my project I get a violation for 'trailing comments'. So I wonder, is this purely related to accepted/recommended code layout conventions for Java or is there 'more to it'? What's the reasoning behind it? When I'm looking over some C++ code ( recent Doom code review, there are tons (or binder full of) trailing comments.

like image 492
vector Avatar asked Jan 17 '13 18:01

vector


1 Answers

From the famous book Code Complete:

  • The comments have to be aligned so that they do not interfere with the visual structure of the code. If you don't align them neatly, they'll make your listing look like it's been through a washing machine.

  • Endline comments tend to be hard to format. It takes time to align them. Such time is not spent learning more about the code; it's dedicated solely to the tedious task of pressing the spacebar or tab key.

  • Endline comments are also hard to maintain. If the code on any line containing an endline comment grows, it bumps the comment farther out, and all the other endline comments will have to bumped out to match. Styles that are hard to maintain aren't maintained.

  • Endline comments also tend to be cryptic. The right side of the line doesn't offer much room and the desire to keep the comment on one line means the comment must be short. Work then goes into making the line as short as possible instead of as clear as possible. The comment usually ends up as cryptic as possible.

  • A systemic problem with endline comments is that it's hard to write a meaningful comment for one line of code. Most endline comments just repeat the line of code, which hurts more than it helps.

Having said that, it's also about one's choice about coding style. I would personally avoid trailing comments as they don't help that much.

like image 75
Swapnil Avatar answered Sep 22 '22 00:09

Swapnil