Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

A new colleague has mooted the idea of using Resharper annotations within our code base (we already are great fans and users of Resharper).

My new colleague cites things like explicitly stating whether a parameter can be null or not null at coding time is a good thing so that consumers of that class can see warnings at coding time.

My reservation is it adds noise to the code, is not runtime executed hence I favour Guard clauses. I am also concerned that like comments that they can go stale. I am also concerned about code readability and the overhead of writing them in the first place. Ultimately I am trying to keep development lean so don't want to add overhead for relatively little gain.

Finally it feels wrong decorating code for one vendor's tooling support.

What are your thoughts? Good/bad experiences?

share|improve this question

5 Answers 5

Why use a 3rd party version when you could just use CodeContracts and not requrire others use Resharper to reap the benefits?

Personally, I vote for guard clauses in any case. It is at least easier to run down the exception when someone ignores the warnings . . .

share|improve this answer
    
Code contracts can create guard clauses automatically, which is great. –  configurator Aug 15 '11 at 22:59

Resharper annotations are not a replacement for guard clauses or code contracts, if they are used they should be used in conjunction with not instead of. Whilst code contracts and guard clauses provide runtime checking of contracts, Resharper annotations work with the Resharper engine to assist developers at design time - ensuring objects that could be null are checked before use, or suggesting the removal of unnecessary checks where they cannot. There are a number of other attributes beyond the nullness checks which can be useful beyond the basic nullness checks to assist in the correctness of the code.

For the nullness check attributes Resharper generally provides alt+enter shortcuts for adding the notnull and canbenull attributes and for parameters it can add both the attribute and a guard clause at the same time.

If you do use the annotations it is probably best to copy the annotation source rather than link to the annotations assembly, that way the code isn't tied specifically to Resharper - there are simply some attributes defined in your own source.

share|improve this answer

I think you'll find that ReSharper annotations fall into the YMMV area. For example, if you have a library with lots of String.Format-like extension methods, and your library is being published to many developers (internal or external), you can give them the benefit of additional checks when they use the API. Same goes for things like null checks - if your API liberally assumes that null parameters and returns are OK, you can add one layer of safety over and above what your XML comments specify. On the other hand, if you use option types or Maybe monads, this functionality will be correspondingly less useful.

share|improve this answer

I dislike the concept of in-code annotations for the benefit of one tool. Extra code added for the use of a tool seems like a code smell. As with many coding situations, there is (probably) a better way to do it.

share|improve this answer

I agree with all those that mentioned Code Contracts as a better alternative. Besides being now part of the .NET platform it has great tooling support (static checker and VS2010 add-in).

I would expect R# to deprecate their custom annotations support in favor of code contracts.

share|improve this answer
1  
I agree with all those that mentioned Code Contracts as a better alternative. ... Don't answer just to say you agree with earlier answers, upvote them instead. (but it's ok to write stuff like that, when you are actually adding something new with your answer) I would expect R# to deprecate their custom annotations support in favor of code contracts. That's your actual answer, and you have to expand it a bit, to stand as an answer. Why do you expect that R# to deprecate their custom annotations? Please back that claim with references, or at least some reasoning. –  Yannis Rizos Jan 11 '12 at 8:30
    
Well, I didn't expect an encounter with the downvote police, but I would add in my defense that I did add to the original answer by stating that Code Contracts are now part of the .NET BCL and that it has great tooling support in the form of the status checker and the VS plugin. I guess that in the future I should limit myself to just read what others say to avoid offending any sensibilities to the minimum amount of prose that is required for an answer to be considered kosher. –  Eddie Velasquez Jan 16 '12 at 17:37
    
Hmmm, the site has a voting system. Every time you submit a post there are chances you get up or down voted, and there isn't much you or anyone can do about that. If the above comment is representative of your attitude towards getting down voted, perhaps you should limit yourself in reading what others say. If not, then perhaps you could improve your answer, your comment is actually bigger than your answer. I feel my down vote is justified, and I would gladly retract it if your improve your answer. –  Yannis Rizos Jan 16 '12 at 18:07
    
I don't know if you have noticed that no answer besides mine has pointed out the Code Contracts are now part of the .NET platform AND it has tooling support. Granted it was terse answer but it contains no falacies and adds to what others had said before me. I know that the site has a voting system and I do not mind being up or down voted, as I just answer trying to be somewhat helpful to the person that asked the question. I just think that your reply was more of a "downvote policeman" answer instead of expanding or pointing out mistakes with my answer. –  Eddie Velasquez Jan 23 '12 at 18:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.