IC Improve Code Recipes

Chapter 15

Strings, Dates and Everyday Utilities

Every codebase I have opened has a junk drawer. It is usually a class called Utility, Utils, Common or Helper, it is usually static, and it usually holds forty methods that have nothing to do with each other. There is a FormatPhone, a GetConnectionString, a SendErrorEmail, an IsNumeric built on a try/catch, and a GetBusinessDays that someone wrote at 6 p.m. on a Friday and nobody has checked since.

The archive behind this book makes the point with numbers. String, date and conversion utilities show up in 28 of the 45 engagements, across 7,638 hand-written classes. The single most common helper class name in the whole archive is Utility, with 463 copies. Utils adds another 195 and StringHelper another 161. A class name with that many copies is a class that traveled from project to project, bugs included.

These helpers are not glamorous, but they sit under everything else. A date helper that uses server local time quietly shifts every timestamp when the application moves to a cloud region. A parsing helper that swallows exceptions turns bad input into zeros that end up on an invoice. A business-day function that forgets holidays sends a payment a day early.

This chapter keeps the helpers worth having and writes them properly: a short list of extension methods, UTC and DateTimeOffset with a testable clock, time zone conversion that survives daylight saving changes, business-day arithmetic, safe parsing, a few string helpers, and a way to organize all of it so it doesn't turn back into a drawer.

Recipes

  1. 15.1Extension Methods Worth KeepingExtension methods are easy to write and hard to remove. Teams collect dozens of them, many duplicating what the base class library already does. A…
  2. 15.2UTC, DateTimeOffset and a Testable ClockCode calls DateTime.Now in hundreds of places. In the archive, roughly 20,000 hand-written C# files (19,999, or 4.6%) use DateTime.Now, in 33 of the…
  3. 15.3Time Zones That Survive Daylight SavingUsers in several time zones need to see times in their own zone, and reports need "orders placed on March 8" to mean March 8 where the business is,…
  4. 15.4Business-Day CalculationsDue dates, SLAs, settlement dates and "respond within five business days" all need date arithmetic that skips weekends and holidays. The typical…
  5. 15.5Safe Parsing and ConversionValues arrive as strings from query strings, CSV files, config and old database columns. The code converts them with int.Parse, Convert.ToDecimal or…
  6. 15.6String Helpers: Truncate, Slugify, MaskThree small string jobs come up in almost every business app. Text must be cut to fit a column or a grid cell. Titles must become URL-friendly slugs.…
  7. 15.7A Lean Utility Library Instead of a Junk DrawerThe Utility class has grown to two thousand lines. It references System.Web, System.Data.SqlClient and the SMTP client, so any project that wants…

Chapter checklist

  • Search the solution for DateTime.Now and replace it with an injected TimeProvider, or at least DateTime.UtcNow, starting with anything that is stored or compared.
  • Store instants as UTC (datetime2 named ...Utc, or datetimeoffset) and default them with SYSUTCDATETIME().
  • Store users' time zones as IANA IDs and convert for display with TimeZoneInfo, never with fixed offsets or ToLocalTime() on the server.
  • Put business-day arithmetic in one place, on DateOnly, with a holiday calendar the business can maintain and tests for the Friday and holiday edges.
  • Replace try/catch parse helpers with TryParse methods that return null, and pass the culture explicitly.
  • Make Truncate, Slugify and Mask safe for short, accented and empty input, and test them with exactly that input.
  • Split the Utility class: pure helpers into a dependency-free library shipped as a package, everything with I/O into injected services.