| name | code-comments |
| description | Use when adding, editing, or reviewing source code comments. Guides Codex to write useful comments that explain why code is shaped a certain way, especially around non-obvious constraints, concurrency hazards, Orleans lifecycle rules, stream/outbox ordering, compatibility requirements, parsing loosely structured input, external standards, and workarounds. |
Code Comments
- Err on the side of over-commenting when the reasoning is not obvious. Comments should explain WHY code is written a particular way; the WHY is the most important part.
- Do comment non-obvious implementation details: concurrency hazards, Orleans grain lifecycle constraints, stream/outbox ordering guarantees, compatibility requirements (public HTTP APIs, Event Hub payloads, Contracts/Client NuGet -- see compatibility-governance), upstream workarounds, and intentional deviations from the obvious helper or API.
- When parsing strings, logs, Event Hub payloads, CPMS/WAAS messages, or other loosely structured data, include a comment with an example of the raw format being parsed. Show edge cases, optional fields, or malformed-but-observed inputs when they affect the parser.
- When code follows an external standard or protocol (OCPI, OCPP, ISO-15118, ISO-3166, etc.), include a link to the relevant source so future readers can verify the rule.
- Do not add comments that simply narrate clear code, such as
// set the timeout immediately before assigning a timeout.
- Keep workaround comments next to the workaround. Add a
TODO: describing the condition for removing it, and link an issue when the workaround is tied to an upstream bug.
Good comments explain the constraint or tradeoff:
var subscriptionSearchPeriod = Schedule.TryCreate(sessionRef.SessionStarted, sessionRef.SessionCompleted ?? sessionRef.TariffEnd);
if (!subscriptionSearchPeriod.HasValue)
{
continue;
}
if (reason.ReasonCode is DeactivationReasonCode.ApplicationRequested or DeactivationReasonCode.ActivationIdle)
{
await WriteStateAsync(State, nameof(OnDeactivateAsync), forceWrite: false, cancellationToken);
}
state = AddSessionUpdatesToOutbox(state, [@event.SessionReference], timeProvider, sourceEventInfo: $"{nameof(SessionUpdatedEvent)} with timestamp {@event.Timestamp:O}");
elements.Add(BuildUnrestrictedFallbackElement(consumptionPriceGroup));
LogFailedToRegisterOutboxReminder(ex, this.GetGrainId(), State.Outbox.Length);
Parsing comments should show the raw shape and important edge cases:
var session = JsonSerializer.Deserialize<CpmsSessionMessage>(payload, SerializerOptions);
if (quartersInHour.Count != 4)
{
continue;
}
Avoid comments that restate the code:
var timeout = TimeSpan.FromSeconds(2);
var subscriptions = new List<EvseSubscription>();