XML documentation standard¶
This page sets the rules for XML documentation comments in ConnectSoft C# code. It is written for engineers who add or review code. ConnectSoft libraries ship their XML documentation with the package, and generated services are read by people who did not write them, so the comments are part of the product.
The Definition of Done item CS-DOD-006 requires XML comments on public and internal members. This page says what those comments contain.
What a good summary says¶
This is guidance, because it cannot be checked mechanically.
- State what the member does or represents, and the contract a caller relies on: units, ranges, whether
nullis allowed, what happens on failure. - Do not repeat the name.
/// <summary>Gets the order id.</summary>onOrderIdadds nothing; "Gets the identifier of the order this line belongs to" does. - Write a full sentence. Start a method summary with a verb ("Creates", "Validates"), a property summary with "Gets" or "Gets or sets" (SA1623), and a constructor summary with "Initializes a new instance of the
class." (SA1642). - Start a type summary with what the type is or does ("Represents an order line.", "Provides access to stored orders."). A field, constant or enum value summary may be a noun phrase ("Customer identifier used when a test does not set one.").
- Use
<see cref="..."/>to link types and members instead of writing their names as plain text. - Describe the exceptions a caller should handle with
<exception cref="...">.
Rules¶
CS-XML-001: Document every public and internal type and member, with its parameters and return value, test code included¶
Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: analyzer SA1600, SA1611, SA1615, SA1618, CS1573, CS1591 · Status: active
Why¶
Most service code is internal, so a rule for public members only would leave most of a service undocumented. Test helpers and test doubles are read by everyone who writes the next test. A summary on the class alone does not explain its constants, constructor parameters or properties, and a method summary alone does not say what each argument means, which values are allowed or what comes back. This rule is the detail behind CS-DOD-006.
Do¶
- Add
<summary>to every public and internal type, constructor, method, property, indexer, field, constant, event, delegate and enum value. - Add
<param>for every parameter of a method, constructor, indexer or delegate, in signature order. Say what the value means and what is allowed: units, range, whethernullor an empty value is accepted. - Add
<typeparam>for every type parameter, saying what the type stands for. - Add
<returns>for every member that returns a value,TaskandValueTaskincluded:- For a method that returns
TaskorValueTask, write exactlyA <see cref="Task"/> representing the asynchronous operation.(orValueTask). - For
Task<T>orValueTask<T>, describe the result:A task whose result is the created order. - For any other return type, describe the value and its special cases, such as
nullwhen nothing is found.
- For a method that returns
- Describe a
CancellationTokenparameter asA token to cancel the operation. - Refer to a parameter inside other comment text with
<paramref name="..."/>, and to a type parameter with<typeparamref name="..."/>. - Rename the
<param>tag when you rename the parameter. - Document test helpers, fixtures, fakes, stubs and builders in test projects the same way.
- Before review, scan new files for a
publicorinternaldeclaration whose previous non-attribute line is not///.
Don't¶
- Document only the class and leave its members bare.
- Leave an empty or placeholder summary such as
/// <summary></summary>or/// TODO. - Leave out
<param>or<returns>because the summary already mentions the value. - Repeat the parameter name or type as its description, such as
<param name="input">The input.</param>or<param name="token">Cancellation token.</param>. - Write
<returns>A task.</returns>or<returns>The result.</returns>.
Example¶
A domain service interface:
/// <summary>
/// Provides the operations that create and delete orders.
/// </summary>
public interface IOrdersProcessor
{
/// <summary>
/// Validates the input, then creates and stores a new order.
/// </summary>
/// <param name="input">Customer and lines of the new order; must not be <see langword="null"/>.</param>
/// <param name="token">A token to cancel the operation.</param>
/// <returns>A task whose result is the created order.</returns>
/// <exception cref="ValidationException"><paramref name="input"/> breaks a rule of the create-order validator.</exception>
Task<IOrder> CreateOrder(CreateOrderInput input, CancellationToken token = default);
/// <summary>
/// Deletes an existing order.
/// </summary>
/// <param name="input">Identifier of the order to delete.</param>
/// <param name="token">A token to cancel the operation.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task DeleteOrder(DeleteOrderInput input, CancellationToken token = default);
}
A test helper:
/// <summary>
/// Builds valid <see cref="CreateOrderInput"/> instances for order tests.
/// </summary>
internal static class CreateOrderInputBuilder
{
/// <summary>
/// Customer identifier used when a test does not set one.
/// </summary>
internal static readonly Guid DefaultCustomerId = new("6f1c2a4e-0000-4000-8000-000000000001");
/// <summary>
/// Creates an input for the given customer.
/// </summary>
/// <param name="customerId">Identifier of the customer who places the order; must not be <see cref="Guid.Empty"/>.</param>
/// <returns>A new input for <paramref name="customerId"/>.</returns>
internal static CreateOrderInput ForCustomer(Guid customerId)
{
return new CreateOrderInput { CustomerId = customerId };
}
}
How to check¶
The analyzers report these only when the documentation file is generated (CS-XML-003):
- SA1600 reports a missing comment on a public or internal element. CS1591 covers only publicly visible elements, so it does not replace SA1600.
- SA1611 reports a missing
<param>, SA1618 a missing<typeparam>, and SA1615 a missing<returns>, including on methods that returnTask. - CS1572 and CS1573 report a
<param>tag that does not match a parameter, for example after a rename.
By hand:
- Read each new
<param>and<returns>and confirm it says more than the name or the type. - In test projects, check new helpers when the warnings are suppressed there (ENF-032).
CS-XML-002: Use <inheritdoc/> on implementations and overrides¶
Severity: nit · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active
Why¶
An interface member and its implementation describe the same contract. Copying the text means the two drift apart when one of them is edited.
Do¶
- Document the contract on the interface or base member.
- Write
/// <inheritdoc/>on the implementing or overriding member. - Add your own summary instead when the implementation has behaviour a caller must know, such as a different exception or a side effect.
Don't¶
- Copy the interface summary into the implementation.
Example¶
The implementation of the IOrdersProcessor interface from CS-XML-001 inherits the summary, the parameters, the return value and the exception from it. The full shape of the method body is in CS-SVC-001.
/// <inheritdoc/>
public async Task<IOrder> CreateOrder(CreateOrderInput input, CancellationToken token = default)
{
await this.createOrderInputValidator.ValidateAndThrowAsync(input, token).ConfigureAwait(false);
return await this.SaveNewOrder(input, token).ConfigureAwait(false);
}
How to check¶
Compare new implementation summaries with the interface; an identical text is replaced by <inheritdoc/>.
CS-XML-003: Generate the documentation file and keep its warnings visible¶
Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: manual (target: ENF-032) · Status: active
Why¶
The compiler checks XML comments only when it generates the documentation file. A suppressed CS1591 hides every missing comment, which is how test and sample projects drift.
Do¶
- Set
<GenerateDocumentationFile>true</GenerateDocumentationFile>once, inDirectory.Build.props. - Keep CS1591, CS1572, CS1573 and the SA16xx documentation rules at warning level, in test projects too.
Don't¶
- Add CS1591 or SA1600 to
NoWarn, or set their severity tonone, in a new project.
How to check¶
- Check
Directory.Build.propsforGenerateDocumentationFile. - Search project files and
.editorconfigforCS1591andSA1600. Existing suppressions are listed under ENF-032.