Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comment style: Imperative or third person

I have a question relating programming and english language both: Whether to use third person or imperative when commenting single lines of code. Assume following line of code in a imperative language which should be commented:

object.doSomething();

My approach to comment this line would be to put the comment behind it using third person like this would be a ordinary english sentence containing the line as subject:

object.doSomething(); // does (referencing to the line of code) some action

But since we are in a imperative language and thus actually "commanding" the computer, one could even think of putting the comment before the code and using imperative:

//Do some action:
object.doSomething();

This is even useful when one need to comment multiple lines related to each other.

I personally prefer the first style but i often feel unsure about what style to use. It would be great if some could write their personal experience down here.

like image 399
Sebastian Hoffmann Avatar asked Jan 18 '12 12:01

Sebastian Hoffmann


2 Answers

Oracle's official style guide states:

Use 3rd person (descriptive) not 2nd person (prescriptive). The description is in 3rd person declarative rather than 2nd person imperative.

Gets the label. (preferred)

Get the label. (avoid)

Oracle's style guide can be found here.

like image 133
Robin Nabel Avatar answered Oct 14 '22 06:10

Robin Nabel


For Python, PEP-257 states about docstrings (emphasis added):

The docstring is a phrase ending in a period. It prescribes the function or method's effect as a command ("Do this", "Return that"), not as a description; e.g. don't write "Returns the pathname ...".

And PEP257: Good Python docstring by example is even more explicit (emphasis added):

Note that all docstrings begin with a one-line summary. The summary is written in the imperative mood ("do", "use", "find", "return", "render", etc) and ends with a period.

like image 20
wstomv Avatar answered Oct 14 '22 07:10

wstomv