Private Methods or Local Functions? What to Look for When AI Coding Agent Refactors Your C#
Your AI coding agent will hand you local functions sooner or later. I found use for them while refactoring old code containing methods longer than a full page of scrolling, some leaning on private helpers that existed only to serve the calling methods. Some helpers sat far from the method they served and had names too generic, and were one copy-paste away from being misused.
I hold myself to the SRP when structuring code: a method does a single job. If I can't see the whole method on screen without scrolling, roughly half a screen, it is probably doing more than one job. Long methods need splitting, and local functions and private methods are the tools I reach for.
This isn't a syntax tutorial. It's a design question: when a helper belongs to one method, should it be a private method or a local function? I'll also argue that knowing features like this matters more now that AI writes so much of our code, because you can't properly review a feature you don't know.