A ten-year-old C# customer-support backend turns on <Nullable>enable</Nullable> and gets two thousand warnings; how would you plan the migration and decide when nullable warnings become errors?
answer
- two flags, two phases
- leaves of the dependency graph first
- directives as a shrinking backlog
- WarningsAsErrors accepts nullable
basics
~20 sTurn the warnings into a sequenced backlog: pick a project default and #nullable directives, migrate bottom-up from leaf types, fix the APIs that really return null, and make nullable warnings errors once migrated code is clean, so the backlog can only shrink.
solid answer
~40 sI would not fix two thousand warnings in one branch. First choose the default: in an active codebase set `<Nullable>enable</Nullable>` so new files start migrated, and put `#nullable disable` on the old files; in a stable one keep `disable` and add `#nullable enable` file by file. Migrate bottom-up, starting with leaf types like the repository, because annotating `Find` as returning `Customer?` moves warnings to callers, and those are often the real bugs. The two flags allow two phases: `warnings` first to harden code, or `annotations` first to settle public contracts. Fix with checks, honest `?`, constructors or `required` members, and treat each `!` as debt. Once the project is clean, add `<WarningsAsErrors>nullable</WarningsAsErrors>` so a regression breaks the build; the end state has no directives left.
go deeper
Recall that a large codebase migrates gradually with the Nullable property and #nullable directives rather than in one change.
Explain the opt-in and opt-out directions, the warnings and annotations modes, and why leaf types are migrated before their callers.
Fix warnings for the right reasons: honest return types on lookups, constructor or required members for CS8618, and guards that stay at the edges.
Own the plan: which default fits the codebase's pace, how progress is measured, when WarningsAsErrors=nullable is switched on, and what counts as done.
## Frame the problem Two thousand warnings after enabling nullable reference types is normal for a codebase that predates C# 8. Most of them come from a few roots: a repository whose `Find` returns null on a miss while declaring a non-nullable return type, DTOs whose properties are set by a serializer (CS8618 by the hundred), and code that was always one null away from a `NullReferenceException`. The goal is not "zero warnings by Friday"; it is reaching `<Nullable>enable</Nullable>` with no `#nullable` directives, no leftover suppressions and annotations that tell the truth. That is a judgment call about sequencing, risk and team habits, and there is more than one defensible plan. ## Choose the default and the direction The nullable context has two flags (annotations and warnings), the project property sets the default, and `#nullable` directives override it per file. That gives two directions: | Strategy | Project setting | Per-file directive | Fits | |---|---|---|---| | Opt in | `disable` | `#nullable enable` on migrated files | Stable code with little new work | | Opt out | `enable` | `#nullable disable` on files not yet migrated | Active code: every new file starts migrated, so the backlog only shrinks | | Warnings first | `warnings` | none needed | Hardening running code before changing any declared types | | Annotations first | `annotations` | none needed | Publishing accurate contracts before fixing internals | For a support backend under active development the opt-out direction is usually the better default: put `#nullable disable` at the top of every existing file mechanically, then delete directives as files are migrated. The count of remaining directives becomes the progress metric. ## Order the work 1. **Start at the leaves of the dependency graph.** Annotating a type creates warnings in its callers, so migrating bottom-up avoids revisiting files. 2. **Fix the repository first.** If `Find` can miss, declare it as returning `Customer?`. Every caller that dereferences the result now warns CS8602, and many of those are real bugs. Where a miss is exceptional, a second method that throws on a miss, or a Try-style method annotated with the attributes covered in the lesson on flow analysis attributes, keeps call sites honest. 3. **Fix constructors.** Answer each CS8618 by asking whether the member is truly required: a constructor parameter or a `required` member (C# 11) if so, a `string?` if not. Use `= null!` only for types a framework fills, and log it as debt. 4. **Annotate the public surface deliberately.** Annotations ship in metadata and become part of the contract other projects compile against. Changing one is not a binary break, but it changes the warnings every caller sees. 5. **Keep run-time guards at the edges.** Callers with nullable disabled and deserializers bypass the analysis, so public entry points still validate. ## Team rules during the migration - A `!` in review needs a reason; blanket `!` to reach zero warnings is rejected, because it hides exactly the bugs the migration exists to find. - Each pull request migrates whole files and removes their directive, so partial states do not linger. - Generated files are compiled with nullable disabled regardless of the project; generators that should participate must emit `#nullable enable` themselves. - Track three numbers: remaining `#nullable disable` directives, `null!` and `default!` occurrences, and nullable warnings in migrated files (which should be zero). ## When warnings become errors Turning everything into errors on day one blocks all work. Never turning them into errors lets warnings creep back. A middle path: ```xml <PropertyGroup> <Nullable>enable</Nullable> <WarningsAsErrors>nullable</WarningsAsErrors> </PropertyGroup> ``` `WarningsAsErrors` accepts the word `nullable` as shorthand for every nullability warning, so other warnings stay warnings. Because files still under `#nullable disable` report no nullable warnings, this can be switched on early in the opt-out strategy: migrated files are held to zero from the moment they are migrated, and unmigrated files are unaffected. `TreatWarningsAsErrors` is the broader switch for every warning, including unrelated ones, and `WarningsNotAsErrors` can exempt specific codes from it. Fine-grained per-rule severities in `.editorconfig` belong to the lesson on warnings and analyzers. ## Trade-offs a lead owns - **Speed versus truth.** A fast migration reaches zero warnings with `!`; a truthful one finds bugs and takes longer. Pick per area: hot paths get the truthful treatment. - **One pass or two.** Separating warnings from annotations gives smaller, reviewable diffs but touches each file twice. - **Library consumers.** For packages others consume, annotate the API early, since consumers start relying on it immediately. - **Done means removed.** The migration ends when the directives, the temporary `null!` initializers and any blanket suppressions are gone, not when the warning count first hits zero.
- Why migrate the repository's Find method before its callers?Annotating `Find` as returning `Customer?` is what makes the compiler warn at every caller that dereferences the result without a check. Migrating callers first would mean visiting them again after the annotation changed, and the warnings at callers are where the real missing checks show up.
- Can WarningsAsErrors=nullable be enabled before every file is migrated?Yes, with the opt-out strategy. Files under `#nullable disable` report no nullable warnings, so only migrated files and new files are held to zero. Each migrated file then stays clean, and the remaining directives measure what is left.
- Fix all two thousand warnings in one long-lived branch.
- Add ! wherever a warning appears to reach zero quickly.
- Turn on TreatWarningsAsErrors on day one of the migration.
- Once annotated, public methods can drop their null guards.
- Migrate the callers first and the repository last.