C # closure comments {}

I worked a little with DevExpress CodeRush and Refactor! Pro this week, and I picked up a comment plugin that will automatically generate comments when you enter the code.

I donโ€™t want to go into how well work on choosing a base value (pretty good, actually), but its default implementation raises the question.

By default, entering the "} character to close the block will cause the plugin to add a comment like the following ...

using(MyType myType = new MyType()) { myType.doWork(); } // using 

(i.e., adding a comment to the closing parentheses where it was opened.)

While I see that there are cases where this behavior can be very useful, I feel that the resulting code looks very untidy with all the additional comments.

I was wondering what other people are; Take such a comment. Not only from an academic point of view, but if I get a large number of negative comments about them, I can decide whether to impose them on my colleagues or supplant them.

+3
source share
13 answers

I think that such comments are useless unless, of course, the code is terrible. With proper formatting of the code, it is easy to see where the block begins and where the block ends, because usually these blocks recede.

Edit: If the procedure is so large that it is not clear which code block is closed with a bracket, then there should be more descriptive comments describing the procedure in any case, and these comments will simply clutter up.

+21
source

I find the idea of โ€‹โ€‹a plugin that perfectly encodes comments from code. If it can be output by a machine, then it can also be output by anyone who reads it. Comments are likely to be completely redundant.

I feel that those closing comments in brackets are messy, they give information that is better provided directly by the IDE, if it wants it.

+9
source

IMO every comment that describes what the code already tells you is not necessary.

If you really have blocks of code that are so long that you have to scroll a lot to see that you started to do something wrong, and you might consider breaking the code.

+4
source

Bad commenting style - it introduces overhead service costs in a code base.

I knew that ex-VB encoders that found trails } in the C syntax code were confusing, but in this case, the real fix is โ€‹โ€‹to reorganize your code to prevent deep embeddings and excessively long functions and / or code blocks.

+4
source

It may be useful if the usage block extends to the page in the IDE, but then you have other problems to worry about. In this case, I get the correct indentation and IDE that selects the appropriate shape when I select it.

I give him the thumbs in general, but with potential use, when you cannot avoid the long block otherwise.

+3
source

Sometimes you get very large blocks of code or many nested blocks that close together. Sometimes I use this style in such cases, but definitely not all the time. I also do not limit it to code: HTML can greatly benefit from this "commenting" style:

 <div id="content"> <div id="columns"> <div class="column"> <!-- .. snip a lot of lines .. --> </div> <!-- .column --> </div> <!-- #columns --> </div> <!-- #content --> 
+3
source

Such comments are only useful for very long blocks of code where you have many nested blocks. But this says that this should not be in the first place, since many nested blocks and long methods require refactoring. Therefore, I do not like this at all, because the reader, obviously, can see what kind of code block it is.

+2
source

I think that the IDE function would be more useful than comments to not only highlight matching pairs of curly braces, but also to display an opening line on a tooltip, so if you hung the top figure in your example, it would come up with " using (MyType myType = new MyType ()) in a tooltip.

This will allow you to easily understand the complex nested braces for large functions without providing constant visual clutter.

+2
source

Itโ€™s always good for me to remember this ...

Clear, well-written code will provide a sufficient explanation of what the code does for a competent programmer to pick it up.

Comments should be left in the code to explain why the code does this!

In other words, use comments to help the reader of your code understand the algorithm or what the code should do, not how to achieve it!

You may want to check out this post by Jeff Atwood .

+2
source

Do not do this, it just adds noise if it is used everywhere, and in addition, the right indentation should solve the problem of readability.

+1
source

I would disable it. I only see the point in using this when you have several blocks ending in the same place (longer or shorter blocks). I used them myself in some cases like these. However, if they are used, it would be better to add them manually, in carefully selected places, rather than add some automatic tools.

+1
source

If you need to think about whether you can use any comment or not, this is most likely the last.

Comments are intended to explain certain blocks of code or the entity as a whole, to facilitate understanding; to make formatting easier to read.

Having a plugin always matches this behavior and is obese and ugly.

0
source

I agree that there are much better ways to describe what the code does.

If you have a long piece of code that is preceded by one informative comment, for example // Fix Work Item, you can take this code and reorganize it as your own method. Then use the comment as the new method name, FixWorkItem (). This is a quick way to make your code more self-documenting and even show some design features that you have not noticed before.

Keep an eye out for single liner comments as potential refactors that IDEs can automatically process. Code that documents itself is better than even the best written offline comments, except, of course, when describing intent.

0
source

All Articles