Skip to content

Code smells & their fixes

Classic C# code smells from real codebases — events, simulation flags, out parameters, null checks, global state — and the modern patterns that replace them.

Design & architecture
12 posts1 h 39 min reading time
Start with part 1 →
  1. Code smells and their fixes — a cheat sheet for .NET teamsOut parameters, simulation flags, switch chains, global state, async void, swallowed exceptions and 3,000-line classes. A compact smell → symptom → fix table for the problems I see most in .NET code, with links to a detailed post for each.7 min read
  2. From events and callbacks to Tasks with TaskCompletionSourceEvent-based and callback APIs scatter one operation across three methods and a flag. TaskCompletionSource turns them into a single awaitable call. Here's how to wrap them correctly, with unsubscription, cancellation, timeouts and request correlation.8 min read
  3. Producer/consumer in 2026 — Channels instead of BlockingCollection and BufferBlockBlockingCollection blocks threads, BufferBlock pulls in a whole dataflow library for a queue, and a hand-rolled Queue plus lock is a race condition waiting to happen. System.Threading.Channels is the async-native producer/consumer queue, with backpressure and completion built in.8 min read
  4. Taming high-frequency events — Rx and Channels for 1 kHz sourcesA machine controller calls you a thousand times per second, and the UI needs thirty updates. Handling every event as it arrives floods dispatchers, logs and databases. How to sample, batch and drop deliberately with Rx and System.Threading.Channels, and the Throttle operator that doesn't do what its name says.9 min read
  5. No simulation flags in production codeAn `if (Simulation)` in every method of a device driver looks harmless, until real and fake behaviour are tangled together in one class. Choose the implementation once at the composition root, keep fakes in their own assembly, and keep test code out of what you ship.9 min read
  6. Out parameters vs. result typesA method with four `out` parameters is hard to call, easy to misuse and impossible to extend. Records, tuples and small result types say the same thing more clearly, and the Try pattern is the one place where `out` still earns its keep.8 min read
  7. The null object pattern meets nullable reference types`notifier?.Send()` in twenty places is a design decision nobody made on purpose. When a missing collaborator should simply do nothing, a null object removes the checks, and nullable reference types make sure the remaining nulls are the ones that mean something.7 min read
  8. Replace switch chains with strategiesThe same `switch` over a shipping method in five places, each case a small algorithm, and every new option means touching all of them. How to turn it into strategies, resolve them with a dictionary or .NET 8 keyed services, and when a plain switch is still the better choice.8 min read
  9. Repository or query object? Giving your data access a homeSQL in view models, a DbContext in every class and validation rules hidden in stored procedures. Why a data access layer matters, when a repository fits, when query objects are the better choice, and the one rule both share — no business logic below the line.8 min read
  10. Lazy<T> and singletons done rightThe static Instance property, the hand-rolled double-checked lock and the Lazy<Task<T>> that caches a failure forever. What Lazy<T> really guarantees, which thread-safety mode to pick, how to cache async initialization without caching errors, and why a DI singleton beats a static one.10 min read
  11. State is the enemy — global state, mutable members and the 3,000-line classA static "current machine state", fields that every method changes, and a lock that guards half of a huge class. Why shared mutable state makes code untestable and unsafe, and how records, pure functions, small classes and the right concurrent types get you out.9 min read
  12. Code noise — comments, regions, prefixes and loops that hide what your code doesGenerated XML docs that repeat the property name,8 min read