From 8d64856e3e69f47abff81ac1660272ac61ae115d Mon Sep 17 00:00:00 2001 From: Dennis Doomen Date: Sat, 21 Nov 2026 20:08:27 +0100 Subject: [PATCH] Add a writing-csharp skill with the C# coding conventions The repo only wrote down its test conventions. For everything else, agents copied the style of the surrounding code, so reviews kept asking for the same changes. - Add .agents/skills/writing-csharp, based on the C# Coding Guidelines with Fallout's exceptions: project rules, test rules, the rule tables and a review checklist. - Add critical rule 10 and a routing row to AGENTS.md, list the skill in CLAUDE.md, and link it from CONTRIBUTING.md. Co-Authored-By: Claude Opus 5.5 --- .agents/skills/writing-csharp/SKILL.md | 34 ++++ .../references/coding-guidelines.md | 192 ++++++++++++++++++ .../references/project-rules.md | 55 +++++ .../references/review-checklist.md | 42 ++++ .../writing-csharp/references/testing.md | 58 ++++++ AGENTS.md | 2 + CLAUDE.md | 1 + CONTRIBUTING.md | 2 +- 8 files changed, 385 insertions(+), 1 deletion(-) create mode 100644 .agents/skills/writing-csharp/SKILL.md create mode 100644 .agents/skills/writing-csharp/references/coding-guidelines.md create mode 100644 .agents/skills/writing-csharp/references/project-rules.md create mode 100644 .agents/skills/writing-csharp/references/review-checklist.md create mode 100644 .agents/skills/writing-csharp/references/testing.md diff --git a/.agents/skills/writing-csharp/SKILL.md b/.agents/skills/writing-csharp/SKILL.md new file mode 100644 index 000000000..a0a4e7802 --- /dev/null +++ b/.agents/skills/writing-csharp/SKILL.md @@ -0,0 +1,34 @@ +--- +name: writing-csharp +description: Coding conventions for C# in this repo — design, naming, layout, XML documentation and tests — based on the C# Coding Guidelines (csharpcodingguidelines.com) with Fallout's exceptions. Trigger whenever you write, change or review C# code under src/, tests/ or build/, including test specs. +--- + +These rules apply to code you add or change. Where existing code does +something else, follow these rules anyway. Don't reformat code you don't +otherwise touch. There is no `.editorconfig` to enforce them, so review does. + +## Rules that are missed most often + +Check these before you finish. Each one came up in a real review. + +- A `Get…` method never returns `null`. If it can, name it `Find…` or `TryGet…`. +- Check for null with `is null` and `is not null`. +- Use `var` only when the right side shows the type. Never use it for `bool`, `int`, `string` and other built-in types. Test code is exempt. +- Keep a method under 15 statements. Put a method before the methods it calls. +- Put one type in each file. Keep lines at 130 characters or less. +- Document every `public` and `internal` member. Say what the signature doesn't: when the result is `null`, which input wins, what gets rejected. +- Put `` and `` on their own lines. +- Use `//` only for why. Use `` for what a member does. +- No constructors or shared fields in a `Specs` class. +- Give every new rule a test. When it depends on the environment, move the decision into a method that takes those values as parameters. + +## References + +Read the file that matches your task: + +| File | Read it when | +| --- | --- | +| [references/project-rules.md](references/project-rules.md) | Writing any C#. It has Fallout's exceptions, such as static `*Tasks` classes and `bool?` on tool options. | +| [references/testing.md](references/testing.md) | Writing or changing a `*Specs.cs` file. | +| [references/coding-guidelines.md](references/coding-guidelines.md) | You need the full rule behind an `AV` rule ID. | +| [references/review-checklist.md](references/review-checklist.md) | Before opening a PR, or when reviewing one. | diff --git a/.agents/skills/writing-csharp/references/coding-guidelines.md b/.agents/skills/writing-csharp/references/coding-guidelines.md new file mode 100644 index 000000000..83b7ef803 --- /dev/null +++ b/.agents/skills/writing-csharp/references/coding-guidelines.md @@ -0,0 +1,192 @@ +# C# Coding Guidelines + +The rule tables of the [C# Coding Guidelines](https://csharpcodingguidelines.com). Each rule ID links to its full text. Fallout-specific exceptions are in [project-rules.md](project-rules.md). + +**Severity:** **Must** = always · **Should** = unless there is a clear reason not to · **May** = optional good practice. + +## General Guidelines (AV0100) + +| Rule | Guideline | +|------|-----------| +| [AV0100](https://csharpcodingguidelines.com/general-guidelines/#AV0100) | Understand the logical and physical boundaries of your codebase; cross-boundary communication must go through well-defined contracts. | +| [AV0105](https://csharpcodingguidelines.com/general-guidelines/#AV0105) | Use design patterns to communicate intent (*Repository*, *Factory*, *Strategy*, etc.). | +| [AV0110](https://csharpcodingguidelines.com/general-guidelines/#AV0110) | Prefer composition over inheritance; reserve inheritance for true "is-a" relationships. | +| [AV0112](https://csharpcodingguidelines.com/general-guidelines/#AV0112) | Apply the Principle of Least Surprise: name things predictably, avoid hidden side effects, follow platform conventions. | +| [AV0115](https://csharpcodingguidelines.com/general-guidelines/#AV0115) | Keep It Simple (KISS): the simplest solution that fully solves the problem is almost always the best. | +| [AV0120](https://csharpcodingguidelines.com/general-guidelines/#AV0120) | You Ain't Gonna Need It (YAGNI): build what is needed now, not what might be needed someday. | +| [AV0125](https://csharpcodingguidelines.com/general-guidelines/#AV0125) | Don't Repeat Yourself (DRY) within a bounded context; allow limited duplication across boundaries to avoid coupling. | +| [AV0130](https://csharpcodingguidelines.com/general-guidelines/#AV0130) | Apply the four pillars of OOP (encapsulation, abstraction, inheritance, polymorphism) as tools, not goals. | +| [AV0135](https://csharpcodingguidelines.com/general-guidelines/#AV0135) | Treat AI-generated code as your own: review, understand, and verify every line before committing it. | + +## Class Design (AV1000) + +| Rule | Guideline | +|------|-----------| +| [AV1000](https://csharpcodingguidelines.com/class-design-guidelines/#AV1000) | A class or interface should have a single, well-defined purpose (Single Responsibility Principle). | +| [AV1001](https://csharpcodingguidelines.com/class-design-guidelines/#AV1001) | Constructors should return a fully initialized, usable object without requiring extra property-setting afterward. | +| [AV1002](https://csharpcodingguidelines.com/class-design-guidelines/#AV1002) | Only pass to a constructor the dependencies that most or all methods need. | +| [AV1003](https://csharpcodingguidelines.com/class-design-guidelines/#AV1003) | Interfaces should be small, focused, and named to explain their role (Interface Segregation Principle). | +| [AV1004](https://csharpcodingguidelines.com/class-design-guidelines/#AV1004) | Expose extension points as interfaces, not base classes. | +| [AV1005](https://csharpcodingguidelines.com/class-design-guidelines/#AV1005) | Use interfaces to decouple classes, prevent bidirectional associations, and enable dependency injection. | +| [AV1008](https://csharpcodingguidelines.com/class-design-guidelines/#AV1008) | Avoid static classes except for extension method containers; they are hard to test and violate DI. | +| [AV1010](https://csharpcodingguidelines.com/class-design-guidelines/#AV1010) | Don't suppress compiler warning CS0114 using `new`; fix the design instead (breaks polymorphism). | +| [AV1011](https://csharpcodingguidelines.com/class-design-guidelines/#AV1011) | Honor the Liskov Substitution Principle: a derived type must be usable wherever its base type is expected. | +| [AV1013](https://csharpcodingguidelines.com/class-design-guidelines/#AV1013) | Don't refer to derived classes from a base class. | +| [AV1014](https://csharpcodingguidelines.com/class-design-guidelines/#AV1014) | Avoid exposing the objects a class depends on (Law of Demeter / "Don't talk to strangers"). | +| [AV1020](https://csharpcodingguidelines.com/class-design-guidelines/#AV1020) | Avoid bidirectional dependencies between classes or assemblies. | +| [AV1025](https://csharpcodingguidelines.com/class-design-guidelines/#AV1025) | Classes should have both state and behavior; avoid data-only or behavior-only splits. | +| [AV1026](https://csharpcodingguidelines.com/class-design-guidelines/#AV1026) | Protect internal state consistency: validate public inputs and guard invariants at construction and mutation. | + +## Member Design (AV1100) + +| Rule | Guideline | +|------|-----------| +| [AV1100](https://csharpcodingguidelines.com/member-design-guidelines/#AV1100) | Properties should be stateless with respect to each other; property access order must not matter. | +| [AV1105](https://csharpcodingguidelines.com/member-design-guidelines/#AV1105) | Use a method instead of a property when the operation is expensive, conversion-like, non-deterministic, or has observable side effects. | +| [AV1110](https://csharpcodingguidelines.com/member-design-guidelines/#AV1110) | Don't use mutually exclusive properties; they signal two conflicting concepts in one type. | +| [AV1115](https://csharpcodingguidelines.com/member-design-guidelines/#AV1115) | A property, method, or local function should do exactly one thing. | +| [AV1125](https://csharpcodingguidelines.com/member-design-guidelines/#AV1125) | Don't hide dependencies behind static members; inject them explicitly. | +| [AV1130](https://csharpcodingguidelines.com/member-design-guidelines/#AV1130) | Return `IEnumerable`, `IReadOnlyCollection`, or `IReadOnlyList` instead of concrete mutable collections. | +| [AV1135](https://csharpcodingguidelines.com/member-design-guidelines/#AV1135) | Strings, collections, and tasks should never be `null`; return empty equivalents instead. | +| [AV1137](https://csharpcodingguidelines.com/member-design-guidelines/#AV1137) | Define parameters as specific and narrow as possible; don't require callers to pass more than you need. | +| [AV1140](https://csharpcodingguidelines.com/member-design-guidelines/#AV1140) | Consider creating domain-specific value types rather than passing primitives everywhere. | +| [AV1145](https://csharpcodingguidelines.com/member-design-guidelines/#AV1145) | Use C# 14 extension members to add behavior to types you don't own or to keep domain logic close to its type. | +| [AV1150](https://csharpcodingguidelines.com/member-design-guidelines/#AV1150) | Avoid local functions; prefer extracting to a named private method. Exceptions: recursion, iterators, or many captured variables. | +| [AV1155](https://csharpcodingguidelines.com/member-design-guidelines/#AV1155) | Use the C# 14 `field` keyword in auto-properties when you need validation or transformation on get/set without a full backing field. | + +## Miscellaneous Design (AV1200) + +| Rule | Guideline | +|------|-----------| +| [AV1200](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1200) | Throw exceptions rather than returning status values or error codes. | +| [AV1202](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1202) | Provide a rich, meaningful exception message that explains the cause and how to avoid it. | +| [AV1205](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1205) | Throw the most specific exception type appropriate (e.g. `ArgumentNullException` over plain `ArgumentException`). | +| [AV1210](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1210) | Don't swallow errors by catching `Exception`; only do so at top-level handlers for logging or graceful shutdown. | +| [AV1215](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1215) | Handle exceptions correctly in asynchronous code; understand how `async`/`await` propagates exceptions. | +| [AV1225](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1225) | Use a `protected virtual OnXxx` method to raise each event so derived classes can intercept or suppress it. | +| [AV1235](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1235) | Never pass `null` as the `sender` argument when raising an event; pass `EventArgs.Empty` instead of `null` args. | +| [AV1240](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1240) | Use generic constraints (`where`) instead of casting to/from `object` in generic code. | +| [AV1250](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1250) | Materialize LINQ results before returning them from a method (call `ToList()` or `ToArray()`). | +| [AV1251](https://csharpcodingguidelines.com/misc-design-guidelines/#AV1251) | Don't use `this.` or `base.` prefixes unless required to disambiguate. | + +## Maintainability (AV1500) + +| Rule | Guideline | +|------|-----------| +| [AV1500](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1500) | Keep methods under 15 statements and at a single level of abstraction. | +| [AV1501](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1501) | Default all members to `private` and types to `internal sealed`. | +| [AV1502](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1502) | Avoid conditions with double negatives. | +| [AV1505](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1505) | Name assemblies after their contained namespace (`Company.Component.dll`). | +| [AV1506](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1506) | Name each source file after the type it contains, in PascalCase with no underscores. | +| [AV1507](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1507) | Limit a source file to one type (exceptions: nested types, same-type-different-generic-arity). | +| [AV1508](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1508) | Name partial type files after the logical role they play (e.g. `MyClass.Designer.cs`). | +| [AV1510](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1510) | Use `using` directives instead of fully qualified type names; use aliases to resolve conflicts. | +| [AV1515](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1515) | Replace literal numeric and string values with named constants or enum members. | +| [AV1520](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1520) | Only use `var` when the type is evident from the right-hand side; never use `var` for built-in types. | +| [AV1521](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1521) | Declare and initialize each variable at the point of first use, not at the top of the block. | +| [AV1522](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1522) | Assign each variable in a separate statement; don't chain assignments. | +| [AV1523](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1523) | Favor object and collection initializers over separate property-assignment statements. | +| [AV1525](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1525) | Don't compare `bool` expressions to `true` or `false` explicitly. | +| [AV1530](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1530) | Don't modify a `for` loop variable inside the loop body; use `break` or `continue` instead. | +| [AV1532](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1532) | Avoid nested loops; prefer LINQ joins or extracted methods. | +| [AV1535](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1535) | Always add curly braces after `if`, `else`, `do`, `while`, `for`, `foreach`, and `case`. | +| [AV1536](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1536) | Always include a `default` block in `switch`; add a comment or throw if it should never be reached. | +| [AV1537](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1537) | Finish every `if`-`else if` chain with a final `else` block. | +| [AV1540](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1540) | Be reluctant with multiple `return` statements; one entry / one exit improves readability in most cases. | +| [AV1545](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1545) | Use direct assignment over `if`-`else`; leverage `??`, `??=`, `?.`, and ternary operators. | +| [AV1546](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1546) | Prefer interpolated strings (`$"…"`) over `string.Format` or concatenation. | +| [AV1547](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1547) | Encapsulate complex inline expressions in a clearly named method, property, or local function. | +| [AV1551](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1551) | Chain overloads so simpler ones call the most-complete overload; make the most-complete overload `virtual`. | +| [AV1553](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1553) | Only use optional parameters as a shorthand for overloads; never use `null` defaults for strings, collections, or tasks. | +| [AV1554](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1554) | Don't add optional parameters to interface methods. | +| [AV1555](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1555) | Avoid named arguments; exception: use them to clarify a `bool` parameter you don't control. | +| [AV1561](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1561) | Limit signatures to 3 parameters; don't use tuple parameters; don't return tuples with more than 2 elements. | +| [AV1562](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1562) | Avoid `ref` and `out` parameters; return a tuple or value type instead. Exception: `TryParse`-style patterns. | +| [AV1564](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1564) | Avoid `bool` parameters that switch behavior; use two methods or an enum instead. | +| [AV1570](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1570) | Use `is`-pattern matching instead of `as`-then-null-check casts. | +| [AV1575](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1575) | Never commit commented-out code; use an issue tracker for deferred work. | +| [AV1580](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1580) | Write code that is easy to debug; break long call chains into named intermediate variables. | +| [AV1582](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1582) | Use raw string literals (`"""…"""`) for multi-line or escape-heavy strings (C# 11+). | +| [AV1585](https://csharpcodingguidelines.com/maintainability-guidelines/#AV1585) | Mark properties `required` when they must be set during object initialization (C# 11+). | + +## Testability (AV1600) + +| Rule | Guideline | +|------|-----------| +| [AV1600](https://csharpcodingguidelines.com/testability-guidelines/#AV1600) | Write test names as short, present-tense sentences describing observable behavior, not implementation details. | +| [AV1602](https://csharpcodingguidelines.com/testability-guidelines/#AV1602) | Suffix test classes with `Specs` instead of `Tests` to encourage a behavior-driven mindset. | +| [AV1605](https://csharpcodingguidelines.com/testability-guidelines/#AV1605) | Test observable behavior through public APIs only; never test private methods or internal state directly. | +| [AV1608](https://csharpcodingguidelines.com/testability-guidelines/#AV1608) | Make the important parts of a test visible in the test body; hide irrelevant setup in helper methods or builders. | +| [AV1610](https://csharpcodingguidelines.com/testability-guidelines/#AV1610) | Use Test Data Builders for flexible construction of complex test objects; use Object Mothers for canonical fixed fixtures. | +| [AV1615](https://csharpcodingguidelines.com/testability-guidelines/#AV1615) | Prefer inline literal values in assertions when the value itself communicates the intent of the test. | +| [AV1618](https://csharpcodingguidelines.com/testability-guidelines/#AV1618) | Assert against independently known expected values; never derive an expected value from the production code under test. | +| [AV1620](https://csharpcodingguidelines.com/testability-guidelines/#AV1620) | Test reusable components (validators, serializers, domain services) in isolation, independently of their consumers. | +| [AV1622](https://csharpcodingguidelines.com/testability-guidelines/#AV1622) | Access internal implementation details through the public API of the component that uses them, not directly. | + +## Naming Conventions (AV1700) + +| Rule | Guideline | +|------|-----------| +| [AV1701](https://csharpcodingguidelines.com/naming-guidelines/#AV1701) | Use US English for all identifiers; favor readability and correctness over brevity. | +| [AV1702](https://csharpcodingguidelines.com/naming-guidelines/#AV1702) | Use PascalCase for types, members, namespaces, and constants; camelCase for parameters, local variables, and private fields. | +| [AV1704](https://csharpcodingguidelines.com/naming-guidelines/#AV1704) | Don't include numbers in variable, parameter, or member names. | +| [AV1705](https://csharpcodingguidelines.com/naming-guidelines/#AV1705) | Don't prefix fields with `_`, `m_`, `g_`, or `s_`. | +| [AV1706](https://csharpcodingguidelines.com/naming-guidelines/#AV1706) | Spell out names in full; avoid abbreviations except for widely accepted acronyms (e.g. `UI`, `Id`). | +| [AV1707](https://csharpcodingguidelines.com/naming-guidelines/#AV1707) | Name identifiers after their meaning, not their type. | +| [AV1708](https://csharpcodingguidelines.com/naming-guidelines/#AV1708) | Name types with nouns or noun phrases; don't include `Utility`, `Helper`, or `Manager` in a type name. | +| [AV1709](https://csharpcodingguidelines.com/naming-guidelines/#AV1709) | Prefix all generic type parameters with `T`; use a descriptive name unless a single `T` is completely unambiguous. | +| [AV1710](https://csharpcodingguidelines.com/naming-guidelines/#AV1710) | Don't repeat the enclosing class or enum name in its members. | +| [AV1711](https://csharpcodingguidelines.com/naming-guidelines/#AV1711) | Name collection/service members to match the conventions of the equivalent .NET Framework type. | +| [AV1712](https://csharpcodingguidelines.com/naming-guidelines/#AV1712) | Avoid names that are visually similar to other names (e.g. `l` vs `1`, `O` vs `0`). | +| [AV1715](https://csharpcodingguidelines.com/naming-guidelines/#AV1715) | Name boolean properties with affirmative phrases; consider prefixes `Is`, `Has`, `Can`, `Allows`, or `Supports`. | +| [AV1720](https://csharpcodingguidelines.com/naming-guidelines/#AV1720) | Name methods and local functions using a verb or verb-object pair; don't use `And` in a name. | +| [AV1725](https://csharpcodingguidelines.com/naming-guidelines/#AV1725) | Name namespaces using nouns, layers, or features; never embed a type name in a namespace name. | +| [AV1735](https://csharpcodingguidelines.com/naming-guidelines/#AV1735) | Name events with a verb or verb phrase (`Click`, `Deleted`, `Closing`). | +| [AV1737](https://csharpcodingguidelines.com/naming-guidelines/#AV1737) | Use `-ing`/`-ed` suffixes for pre/post events instead of `Before`/`After` prefixes. | +| [AV1738](https://csharpcodingguidelines.com/naming-guidelines/#AV1738) | Prefix event handler methods with `On` (e.g. `OnClosing`, `OkButtonOnClick`). | +| [AV1739](https://csharpcodingguidelines.com/naming-guidelines/#AV1739) | Use `_` (single discard in C# 9+) for irrelevant lambda parameters. | +| [AV1745](https://csharpcodingguidelines.com/naming-guidelines/#AV1745) | Group extension methods in a class suffixed with `Extensions`. | +| [AV1755](https://csharpcodingguidelines.com/naming-guidelines/#AV1755) | Only suffix methods with Async or TaskAsync when both synchronous and asynchronous variants exist | + +## Performance (AV1800) + +| Rule | Guideline | +|------|-----------| +| [AV1800](https://csharpcodingguidelines.com/performance-guidelines/#AV1800) | Use `Any()` instead of `Count()` to check whether an `IEnumerable` is empty. | +| [AV1820](https://csharpcodingguidelines.com/performance-guidelines/#AV1820) | Use `async`/`await` only for I/O-bound work; it does not run code on a thread-pool thread by itself. | +| [AV1825](https://csharpcodingguidelines.com/performance-guidelines/#AV1825) | Use `Task.Run` for CPU-bound work; use `TaskCreationOptions.LongRunning` for long-running background operations. | +| [AV1830](https://csharpcodingguidelines.com/performance-guidelines/#AV1830) | Don't block on async code with `.Wait()` or `.Result`; it can deadlock. | +| [AV1835](https://csharpcodingguidelines.com/performance-guidelines/#AV1835) | Blocking on a `Task` in UI frameworks (WPF, WinForms) causes deadlocks because of the single-threaded sync context. | +| [AV1840](https://csharpcodingguidelines.com/performance-guidelines/#AV1840) | Await `ValueTask`/`ValueTask` directly and exactly once, or call `.AsTask()` to get a reusable `Task`. | + +## Framework Usage (AV2200) + +| Rule | Guideline | +|------|-----------| +| [AV2202](https://csharpcodingguidelines.com/framework-guidelines/#AV2202) | Prefer idiomatic C# syntax over verbose .NET API calls: tuple literals, `is null`, `is not null`, `??=`, `[]` collection expressions, and C# 14 null-coalescing assignment. | +| [AV2210](https://csharpcodingguidelines.com/framework-guidelines/#AV2210) | Build with the highest warning level and treat warnings as errors. | +| [AV2220](https://csharpcodingguidelines.com/framework-guidelines/#AV2220) | Prefer LINQ extension method syntax over query syntax for simple expressions. | +| [AV2225](https://csharpcodingguidelines.com/framework-guidelines/#AV2225) | Use tuple/record deconstruction to avoid intermediate variables and simplify assignments. | +| [AV2230](https://csharpcodingguidelines.com/framework-guidelines/#AV2230) | Only use the `dynamic` keyword for interop with dynamic languages or APIs; avoid it in hot code paths. | + +## Documentation & Comments (AV2300) + +| Rule | Guideline | +|------|-----------| +| [AV2301](https://csharpcodingguidelines.com/documentation-guidelines/#AV2301) | Write all comments and XML documentation in US English. | +| [AV2305](https://csharpcodingguidelines.com/documentation-guidelines/#AV2305) | Document every `public`, `protected`, and `internal` type and member with XML doc comments. | +| [AV2306](https://csharpcodingguidelines.com/documentation-guidelines/#AV2306) | Write documentation for other developers who don't have access to the source code. | +| [AV2308](https://csharpcodingguidelines.com/documentation-guidelines/#AV2308) | Document the purpose of a member (the *why* and *what*), not its implementation (the *how*). | +| [AV2310](https://csharpcodingguidelines.com/documentation-guidelines/#AV2310) | Avoid inline comments; replace complex blocks with well-named methods instead. | +| [AV2316](https://csharpcodingguidelines.com/documentation-guidelines/#AV2316) | Only write comments to explain non-obvious algorithms or significant design decisions. | +| [AV2318](https://csharpcodingguidelines.com/documentation-guidelines/#AV2318) | Don't use `// TODO` comments as a task tracker; use a proper issue tracking system. | + +## Layout (AV2400) + +| Rule | Guideline | +|------|-----------| +| [AV2400](https://csharpcodingguidelines.com/layout-guidelines/#AV2400) | Max 130 chars/line; 4-space indent (no tabs); space between keyword and `(`; spaces around operators; opening and closing braces on their own lines; empty lines between unrelated blocks. | +| [AV2402](https://csharpcodingguidelines.com/layout-guidelines/#AV2402) | Order `using` directives: System namespaces first, then third-party/own in alphabetical order; place `using static` and alias directives last. | +| [AV2406](https://csharpcodingguidelines.com/layout-guidelines/#AV2406) | Order members: private fields/constants → public constants → public static readonly fields → factory methods → constructors → events → public properties → other methods; local functions at the bottom of their method. | +| [AV2407](https://csharpcodingguidelines.com/layout-guidelines/#AV2407) | Don't use `#region`; they hide code and add friction without improving readability. | +| [AV2410](https://csharpcodingguidelines.com/layout-guidelines/#AV2410) | Use expression-bodied member syntax only when the body is a single statement that fits on one line. | diff --git a/.agents/skills/writing-csharp/references/project-rules.md b/.agents/skills/writing-csharp/references/project-rules.md new file mode 100644 index 000000000..847347b3b --- /dev/null +++ b/.agents/skills/writing-csharp/references/project-rules.md @@ -0,0 +1,55 @@ +# Project rules + +Rules that add to the [C# Coding Guidelines](coding-guidelines.md), and the places where Fallout makes an exception to them. + +## Exceptions for Fallout + +- **Static task classes are fine.** The `*Tasks` classes (`DotNetTasks`, `GitTasks`, ...) and their extension methods are the public API of a build script. AV1008 and AV1125 don't apply to them. A new static class that is not part of that API still needs a reason. +- **`bool?` is fine on tool options.** In a `ToolOptions` class, `null` means "don't pass the argument", which differs from `false`. Everywhere else, use `bool` when no value means `false`. +- **Public API is a promise.** A public interface or extension point is fine with one implementation when consumers implement it. Internal interfaces still need a reason (see below). +- **Check the target framework.** Many projects also target `netstandard2.0`. A new C# feature (AV1145, AV1155, `required`, `init`) must compile for every target framework of the project. +- **Generated code is exempt.** Don't apply these rules to `*.Generated.cs` files. Change the tool's `.json` spec or the generator instead. + +## Design + +- Make a type `internal` unless it is meant to be public API. Its members may be `public`: they can't be more visible than the type. Don't flag a `public` member on an `internal` type. +- Add an abstraction only when at least three places use it. +- Don't add an interface only for dependency injection or tests. Add one when it separates two parts of the system that must not know each other. +- Make nullability match the contract. Mark a property that must have a value as `required`, or make it non-nullable, instead of declaring it nullable and rejecting `null` at run time. A property that may be absent must be nullable, not just documented as optional. +- When you validate arguments, check each one on its own and throw an exception that names it. One message for several fields doesn't tell the caller which one is wrong. +- Don't add an early-return guard before code that already handles empty input. It adds noise and can skip logging that should still run. + +## Names + +- A method whose name starts with `Get` never returns `null`. If it can, name it `Find…` or `TryGet…`. +- Never abbreviate a word in a folder, type or namespace name: `Configuration`, not `Config`. Widely used acronyms such as `Id`, `Uri` and `Xml` are fine. +- The namespace follows the folder structure. + +## Code + +- Use `var` only when the right side shows the type, as in `new Foo()` or a cast. Never use it for built-in types such as `bool`, `int` and `string`. `var` is required for anonymous types. Test code is exempt. +- Check for null with `is null` and `is not null`, not `== null` and `!= null`. +- Never compare a `bool` to `true` or `false`. Write `if (isActive)`, not `if (isActive == true)`. +- Don't write `(await FooAsync()).ToList()`. Use an async LINQ method, or split it over two lines. +- Never use `Thread.Sleep` in async code. Use `await Task.Delay`. +- Never put `!` after a deserialization call, such as `JsonSerializer.Deserialize(...)!`. A `null` result then turns into a `NullReferenceException` far from the cause. Check for `null` and throw a clear exception. +- Put `[UsedImplicitly]` (JetBrains.Annotations) on a member that only reflection or a serializer uses, such as a `[Parameter]` field. Use `[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]` on the type when this applies to all of its members. + +## Size and order + +- Keep a method under 15 statements, at one level of abstraction. Split a longer method into well-named private methods. A flat list of independent guard clauses is fine. +- Order methods so that a method comes before the methods it calls. The file then reads from top to bottom. +- Put one type in each file, and name the file after the type. +- Keep lines at 130 characters or less. + +## Documentation and comments + +- Document every `public` and `internal` type and member with XML comments. Skip it only when the comment would repeat the name. +- Say what the reader can't see from the signature: when the result is `null`, which input wins, what gets rejected. "Gets the test runner" adds nothing to `GetTestRunner`. +- Put `` and `` on their own lines, also for one-line summaries. +- Use a `//` comment only to explain why the code does something unexpected. To describe what a member does, use ``. +- `// REFACTOR` and `// SMELL` comments are fine to point the next person at a known problem. Don't leave a `// TODO` without an issue. + +## Packages + +- Every new package needs a maintainer's approval. Add it to the right group in `Directory.Packages.props`, never with `Version=` in a project file, and record it in [docs/dependencies.md](../../../../docs/dependencies.md). diff --git a/.agents/skills/writing-csharp/references/review-checklist.md b/.agents/skills/writing-csharp/references/review-checklist.md new file mode 100644 index 000000000..2dffc2ae7 --- /dev/null +++ b/.agents/skills/writing-csharp/references/review-checklist.md @@ -0,0 +1,42 @@ +# Review checklist + +Use this before you open a PR, or when you review one. Walk the block that matches each changed file. Every changed method also gets block M, whatever file it is in. + +Each item has an ID so a review can say which items it checked. The rule behind each item is in [project-rules.md](project-rules.md), [testing.md](testing.md) or [coding-guidelines.md](coding-guidelines.md). + +## T. A new or changed `*Specs.cs` file + +- **T1.** Is the class suffixed `Specs`, and do its file and namespace mirror the project under test? +- **T2.** Does each test name state a fact in plain English, with no method or type names and no `Can_` for behavior that always happens? +- **T3.** Is every statement under the right Arrange, Act or Assert heading? +- **T4.** Is everything the scenario doesn't depend on left out: no restated defaults, no assertions the setup already guarantees? +- **T5.** Is there no constructor and no shared field in the class? +- **T6.** Are the values that matter visible in the test body, including in `[InlineData]` rows? +- **T7.** Is the expected value known independently, not computed with the code under test? +- **T8.** Does every new rule in the production code have a test, including rules that depend on the environment? + +## M. Any changed method, whatever file it is in + +- **M1.** Is it under 15 statements, at one level of abstraction? +- **M2.** Can a reader follow every branch without guessing the author's intent? +- **M3.** Does an existing helper already do what this code does by hand? +- **M4.** Is `var` used only where the right side shows the type, and never for built-in types? +- **M5.** Is a method that only its own type uses `private`? +- **M6.** Does a `Get…` method never return `null`? +- **M7.** Are null checks written as `is null` and `is not null`? +- **M8.** Does it come before the methods it calls? + +## D. A new or changed `public` or `internal` type or member + +- **D1.** Is the type `internal` unless it is meant to be public API? If it is public and not yet stable, is it marked `[Experimental]`? +- **D2.** Does it have an XML comment that says what the signature doesn't? +- **D3.** Are `` and `` on their own lines? +- **D4.** Is there one type per file, with the file named after the type? +- **D5.** Is a new abstraction or interface used in at least three places, or does it separate parts that must not know each other? +- **D6.** Does nullability match the contract, with no `!` needed downstream? +- **D7.** Are all lines 130 characters or less? + +## P. A changed `*.csproj` or `Directory.Packages.props` + +- **P1.** Does it add a package? Then it needs a maintainer's approval, an entry in the right group of `Directory.Packages.props` and a line in `docs/dependencies.md`. +- **P2.** Does a `PackageReference` have a `Version=`? Move the version to `Directory.Packages.props`. diff --git a/.agents/skills/writing-csharp/references/testing.md b/.agents/skills/writing-csharp/references/testing.md new file mode 100644 index 000000000..ed972d9fb --- /dev/null +++ b/.agents/skills/writing-csharp/references/testing.md @@ -0,0 +1,58 @@ +# Tests + +Fallout tests use xUnit v3, FluentAssertions and Verify. Don't add another test or mocking framework. + +## Structure + +- Use Arrange, Act and Assert, marked with `// Arrange`, `// Act` and `// Assert` comments. Leave out a phase that has nothing in it. +- Put each statement under the phase it belongs to. A value that feeds the call under test is Arrange, however late it appears. Reading the result of the call is Act. Every check is Assert, even a one-line sanity check. +- Don't invent other headings, such as `// Assign / Act`. The only allowed combination is `// Act/Assert`, for a test where the act is the check, such as "does not throw". +- Don't use constructors or shared fields in a `Specs` class. Set up what a test needs inside the test, or in a helper that the test calls. +- Set up state through the public API where you can. A test that sets up through the internals no longer proves that the public API works. + +## What to leave out + +- Leave out anything the scenario doesn't depend on. When everything is shown, nothing stands out. +- Don't check something the setup already guarantees, and don't restate defaults you rely on. +- Ask of each assertion: would the test prove less without it? If not, remove it. + +## Assertions + +- Use the single most specific FluentAssertions method. A row of checks per property or key almost always becomes one `BeEquivalentTo` or one collection assertion, with a better failure message. +- Avoid `AssertionScope`. Normal tests rarely need every failure reported at once, and a `null` inside the scope causes a `NullReferenceException` instead of a clear failure. +- Compare with values you know independently. Never compute the expected value with the production code under test. +- Build dates with the FluentAssertions extensions, such as `1.September(2026)`. Never use `DateTime.Parse` in a test. +- Never take a value from one fixed input and apply it to a different input that is read at run time. For example, a time-zone offset taken from a fixed date is wrong for a date in another DST period. + +## Test data + +- Make the values that matter visible in the test body. +- Make test data rows describe themselves. A row of bare values in `[InlineData]` or `TheoryData` doesn't tell the reader which value is input and which is expected. Use a named record or named arguments. +- Prefer a Test Data Builder over small private helper methods. Name the builder after the type it builds, and don't add methods that no test uses yet. +- Hundreds of theory rows are a finding. Keep a few rows that each show a different rule, and move the bulk into a data file. + +## Names + +A test name is read in a failure report, usually without the code. It must say what is true of the system. + +- Use snake case, and state a fact. Never use "should" or "when". +- State the rule, not the mechanism that enforces it. "An empty project path is not valid" is better than "Validation rejects the request". +- Write a real English sentence, with articles: `A_project_path_is_required`, not `Project_path_required`. +- Never use the name of the method under test, a type name or any other code element. +- Don't use `Can_` for behavior that always happens. `Can_throw_for_a_missing_project` suggests it sometimes doesn't. +- Don't repeat the purpose of the test class in the method name. +- Suffix test classes with `Specs`, never `Tests`. + +| Instead of | Write | +| --- | --- | +| `GetNuGetReleaseNotes_WithMissingChangelog_DoesNotThrow` | `Missing_changelog_does_not_add_a_full_changelog_link_to_release_notes` | +| `Throws_error_if_project_path_is_empty` | `An_empty_project_path_is_not_valid` | +| `Can_throw_when_target_is_not_found` | `An_unknown_target_is_rejected` | +| `ParseTestRunner_returns_null_for_unknown` | `Unknown_test_runner_names_are_ignored` | + +## Coverage + +- Give every new rule its own test. +- When a rule depends on the environment, such as an environment variable or the installed SDK, move the decision into a method that takes those values as parameters. Test that method. +- Test reusable parts, such as a parser or an argument builder, on their own and not only through the code that uses them. +- Test files and namespaces mirror the project under test. diff --git a/AGENTS.md b/AGENTS.md index 3cd4aaa32..c2f48f9e7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -74,6 +74,7 @@ Do commit code generated by `GenerateTools` — the `.Generated.cs` files are ch Full rules and examples: the `plain-english` skill, including issue/PR shape (Problem → Outcome → Acceptance criteria, anti-patterns). 9. **Never ping the former NUKE maintainer, Matthias Koch (GitHub handle `matkoch`).** He no longer maintains Fallout and does not want the notifications. Do not `@`-mention him (never write `@` before his handle, in any file or GitHub surface), add him as a reviewer/assignee, request his review, tag him in issue/PR/commit text, or add him as a commit co-author/`Co-authored-by:` trailer — from any AI tool. Credit NUKE's origin by name or a plain profile link — just never with a leading `@` (a bare `@handle` is what fires a mention). +10. **Follow the `writing-csharp` skill for every C# change**, in `src/`, `tests/` and `build/`. Its rules win over the style of the code around your change. Don't reformat code you don't otherwise touch. Before you open a PR, walk its review checklist. ## Repository map @@ -96,6 +97,7 @@ Production code lives under `src/`, tests under `tests/` — don't reintroduce a | --- | --- | | Opening a PR, writing commits, picking a base branch | `creating-a-pr` skill | | Writing any PR, commit, issue, or doc text | `plain-english` skill | +| Writing, changing, or reviewing C# code, including tests | `writing-csharp` skill | | Adding/extending a `Tools//.json` wrapper | `adding-a-tool-wrapper` skill | | Adding public API that isn't stable yet, or deprecating one | `marking-experimental-apis` skill | | Proposing a new public API before writing code | [CONTRIBUTING.md § Proposing new public API](CONTRIBUTING.md#proposing-new-public-api) | diff --git a/CLAUDE.md b/CLAUDE.md index 2e1b2f71e..65b6d7874 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -10,6 +10,7 @@ Skills live under `.agents/skills/` (tool-neutral — Copilot CLI auto-discovers - [.agents/skills/creating-a-pr/SKILL.md](.agents/skills/creating-a-pr/SKILL.md) — opening a PR, writing commits, picking a base branch, writing plain terse PR/issue descriptions. - [.agents/skills/plain-english/SKILL.md](.agents/skills/plain-english/SKILL.md) — writing any PR, commit, issue, or doc text so non-native English readers follow it on the first pass. +- [.agents/skills/writing-csharp/SKILL.md](.agents/skills/writing-csharp/SKILL.md) — writing, changing, or reviewing C# code, including tests. - [.agents/skills/adding-a-tool-wrapper/SKILL.md](.agents/skills/adding-a-tool-wrapper/SKILL.md) — adding or extending a `Tools//.json` wrapper. - [.agents/skills/marking-experimental-apis/SKILL.md](.agents/skills/marking-experimental-apis/SKILL.md) — adding public API that isn't stable yet, or deprecating one. - [.agents/skills/editing-ci-workflows/SKILL.md](.agents/skills/editing-ci-workflows/SKILL.md) — touching `.github/workflows/**` or `build/Build.CI.GitHubActions.cs`. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 40d91981a..49a05c600 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -61,7 +61,7 @@ This applies to a new public type or member, not a small additive overload or a ### When writing the PR - **Write functional commit and PR titles** — describe what the change accomplishes, not how it's categorised. Do not use conventional-commit prefixes (`feat:`, `fix:`, `chore:`, `refactor:`, etc.). Good examples: "Add retry logic to the HTTP tool wrapper", "Fix null-reference in target dependency resolution". The `!` suffix (e.g. `fix(security)!: …`) is recognised only as a breaking-change detection signal, not a general style requirement. -- Aim for qualitative, readable code that matches the surrounding style. +- Aim for qualitative, readable code. Follow the [C# conventions](.agents/skills/writing-csharp/SKILL.md). Where they say nothing, match the surrounding style. - There's no committed `.editorconfig` or ReSharper/`*.DotSettings` file — they were removed during the takeover. Rely on `dotnet format` defaults and review; don't reintroduce them without a maintainer-level decision. - Add tests when meaningful — every `Foo` project has a sibling `Foo.Tests`. A few conventions to follow: - **Test names read as behavior, not method calls** (AV1600-style): `Missing_changelog_does_not_add_a_full_changelog_link_to_release_notes`, not `GetNuGetReleaseNotes_WithMissingChangelog_AndGitHubRepository_DoesNotThrow`.