Positioning comments regarding code

What is the best way to post comments in code? I see at least three different ways:

1:
int i = 10;     //Set i to 10

2:
//Set i to 10
int i = 10;

3:
int i = 10;
//Set i to 10

The disadvantage of using the first method is that many people use tabs instead of spaces, and this will lead to comments becoming very inconsistent when resizing the tab.

The second and third fragments eliminate this problem, but if there is a lot of code, it is sometimes unclear which line the comment refers to.

+3
source share
9 answers

Option 3 Just Wrong. All the tools that I know wait for the docs method before using a method similar to method 2. Thus, the opposite inside the method is confusing.

, 1 2 , 1 . , 2 , /, .

+2

32: .

, .

+2

, - , , - . , №1 №2 - № 3 .

0

- , , - .

- , ..

0

, "". , , , , 95% .

no 2,

0

, 1: , .. , , , 80 , 2: .

2: , ..

3: .

0

blah;  // comment

- // . , .

0

2. 1 , .

, , , , .

0

Javadoc ():

/**
 * This is a Javadoc comment.
 */

:

// This comment refers to
someGroupingOfCode();
thatPerforms(aCertainTask);
whichIsThenFollowedBy(anEmptyLine);

// And possibly another comment
thatGoesWith();
theNextGroupOfTasks();

, :

int i;  // This stores the value of your eye
File x; // XXX directory location

As for whether I use tabs in the last example, I'm not even going to go there. I don’t want to throw gasoline on the fire right now. :)

0
source

Source: https://habr.com/ru/post/1729994/


All Articles