Where Should Comments Go When Documenting a Method?

0
5
Asked By MellowCedar47 On

When documenting a method definition, should the comment appear before the method signature or inside the method body? For example, should I write a comment immediately above the declaration, or place it as the first line inside the implementation? I tend to prefer the second style because it feels associated with the method's code, but API documentation often places comments before the signature. Is putting the comment before the method a convention specifically intended for documentation?

3 Answers

Answered By CopperVale62 On

For C or C++, tools such as Doxygen use comments placed before the method declaration or definition to generate API documentation. Many IDEs also recognize that format and display it when you use the method elsewhere, so the preceding-comment style is the usual choice for method documentation.

MellowCedar47 -

That makes sense—the documentation comment belongs to the method declaration, while comments inside the body are for implementation details.

Answered By QuietHarbor8 On

Put the comment before the method when you’re explaining what the method does, its inputs, outputs, or other API behavior. A comment inside the method should usually explain a particular implementation detail or why the following code is unusual—not document the method as a whole.

PixelNook31 -

A useful distinction is: document the method before its signature, and explain a surprising line or block inside the body.

Answered By SwiftPebble19 On

The exact syntax varies by language, but most languages and style guides follow the same general idea: put public documentation immediately before the declaration. Use an inner comment only when it clarifies a specific step in the implementation.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.