Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Attributes for Documentation in C# [closed]

In the MSDN Attributes Tutorial they use Author as an example for an attribute:

[Author("Jane Programmer", Version = 2), IsTested()]
class Order
{
    // add stuff here ...
}

This seemed to me to be a good idea because it would allow you to use reflection to group classes by author (for example) - effectively exposing metadata that would normally be in documentation to the compiler, which could be useful. I immediately thought "aha! I should be using attributes for all my inline block documentation" - e.g.:

[Author("Me")]
[Description("Add 1 to value")]
[Param("value", "The original value to add 1 to")]
public int AddOne(value) {return value + 1;}

However none of the answers I could find about documentation and attributes seem to suggest this method. They all use XML for inline documentation.

Are there any built-in attributes to assist with inline documentation? If not, are there any libraries / packages out there that include pre-defined sets of attributes for inline documentation?

like image 975
Robin Winslow Avatar asked Oct 01 '12 09:10

Robin Winslow


People also ask

What is the use of attribute in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

What are the attributes of a variable in C?

Four attributes are currently defined for variables: aligned , mode , packed , and section . Other attributes are defined for functions, and thus not documented here; see Function Attributes. This attribute specifies a minimum alignment for the variable or structure field, measured in bytes.

What's the purpose of attributes?

In computing, an attribute is a specification that defines a property of an object, element, or file. It may also refer to or set the specific value for a given instance of such. For clarity, attributes should more correctly be considered metadata. An attribute is frequently and generally a property of a property.

How do you write attributes?

Attributes are always specified in the start tag (or opening tag) and usually consists of name/value pairs like name="value" . Attribute values should always be enclosed in quotation marks.


2 Answers

Some disadvantages of keeping documentation in attributes:

  • poor formatting for long texts;
  • no support by Visual Studio add-ons (e.g. using ReSharper's documentation preview feature);
  • no support by 3rd party documentation generation tools;
  • inclusion of documentation in assemblies which significantly eases reverse engineering;
  • duplication of metadata in source codes with metadata stored in a version control system (there's no point in tracking any declaration's author and version in the source code, when the VCS gives you much more precise information — VCS's don't lie).

I can't think of any advantage right now. In case I would really need it, it's always possible to parse the XML documentation comments and transform the whole codebase into any attributed form.

like image 91
Ondrej Tucny Avatar answered Oct 13 '22 20:10

Ondrej Tucny


The question here seems to be 'what is documentation?'. If the 'stuff' your interested in needs to be accessible by reflection then your implied solution of attributes is a solution. But if the intent is to use standard documentation tools to build documentation then not so.

The need here begs the solution. What is the need for the 'documentation'. Perhaps the wrong question?

like image 23
Rob Smyth Avatar answered Oct 13 '22 22:10

Rob Smyth