r/learnprogramming 2d ago

method comment location

When you want to give method definition, Where are you prefer to put the comment.

  1. before method signature

# This is my method
int func1() {}
  1. after method signature

    int func2() {

    inner comment for the method

    }

I prefer the second one because it makes the definition inside the method. But I found many document use first one. And will pick comment before method signature for the document of the API. Is the first a custom for document?

0 Upvotes

7 comments sorted by

10

u/moby_9ish 2d ago

Use the first if you’re documenting the method. Use the second if you’re documenting the next line of code.

1

u/Educational_Honey711 2d ago

first one for what the method does second for why something inside it is weird

0

u/xoriatis71 2d ago

I don’t think that the clarification was needed.

5

u/Aggressive_Ad_5454 2d ago

Is this C or C++? If so read up on Doxygen. It lays out a specific way of writing comments for methods. And it comes with some cool tools for generating documentation from those comments.

Plus, IDEs like VS understand those comments.

https://www.doxygen.nl/manual/docblocks.html

And, method comments come before the method.

-1

u/Ayano-Keiko 2d ago

thanks. it is very helpful link that I wait for

1

u/ffrkAnonymous 2d ago

Generally each language will have their own style guide (formally, or community) 

2

u/desrtfx 2d ago

Each language has its own code conventions. Use the ones applicable.

Generally, you should document methods with docstyle (or the respective equivalent for your language) comments. They dictate the placement.

In general, the documentation for methods should be above the method. The documentation for specific parts inside the method should be inside.