CSharpened
CSharpened

Reputation: 12574

Comments at end of method best practice?

I have some code which has comments after each method saying this for example:

// End of contructor DbFactoryDBConnection()

Should a comment like this be used to indicate the end of a method or not? It was something picked up from an earlier employment where it was common place.

Upvotes: 4

Views: 1068

Answers (7)

sll
sll

Reputation: 62524

I feel such comments are after methods which body does not fit in a single screen. I believe this is a good sign for the refactoring and splitting out such long methods to shorten ones or even extracting new entities/services/helpers. This is like C# region, sometimes it is used to hide very long code blocks and this is a sign that you have some kind of a God Object anti-patter.

Upvotes: 4

Endiss
Endiss

Reputation: 699

You can always use PowerCommands for Visual Studio it add at end of braces comment what bracer end you are at i use it and it helps a lot:)

Upvotes: 1

Lucero
Lucero

Reputation: 60190

This doesn't belong in the source IMHO; it's too easy to get out of sync with the effective source code blocks. There are extensions that will show this info for you in Visual Studio i you thing that this is helpful.

Upvotes: 0

Steve
Steve

Reputation: 216303

Usually this kind of comments are an overdocumentation hassle.
The right place where a comments should exist are at the beginning of your classes and of your methods and just before where important decisions will be taken inside your code.
If you find the need to document the end of an IF, SWITCH or METHOD then it's very probable that your code should be reexamined to be simplified.

Upvotes: 2

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174329

No, such a comment should not be used. The only reason, someone would use a comment like this is the fact that the method is way too long. But even than, modern IDEs show you somewhere in which method you currently are and even allows folding methods back to just the definition.
Comments like this are of no value whatsoever and simply pollute the code-base.

Upvotes: 0

Robbie
Robbie

Reputation: 19500

I think this sort of commenting is completely unnecessary, and only serves to clutter the code. Most IDE's (such as Visual Studio) have had features for highlighting the scope of a method (or whatever) for many years, so i'm not sure what value you could add you your code by doing this.

Upvotes: 1

YoryeNathan
YoryeNathan

Reputation: 14522

No. If you want to know what the braces are closing, without scrolling, you can have ReSharper. Which is great in many ways.

Just put your cursor on braces, and if the opening of them isn't in view - it tooltips the whole line before it.

Upvotes: 2

Related Questions