Too Many Parameters: How to Refactor Long Parameter Lists (video)
Long parameter lists cause swapped arguments and hard-to-read calls. Learn how many parameters is too many, and refactor with parameter objects, options objects, builders, and splitting responsibilities.
Transcript
Too Many Parameters: How to Refactor Long Parameter Lists. Here's the short version.
A function with seven positional parameters is easy to call wrongly: swap two strings or two booleans and the code still compiles. Long parameter lists also hint that a function does too much or that related values belong together.
How many is too many?. Zero to three parameters is easy to read. Watch especially for several parameters of the same type next to each other, and for boolean flags.
Here's an example: Which true is which?.
Fix 2: group values that travel together. If the same three or four values are passed through several functions (street, city, postcode, country). Introduce an Address type or record and pass that instead.
Fix 3: replace boolean flags with separate functions. A flag that switches between two behaviours usually means two functions. sendInvoice is clearer as sendInvoiceByEmail and…
Want to find these issues in your own code? Connect your GitHub repository to CodeInspector. You'll get findings with the file, line, severity, and a recommended fix, plus HTML and PDF reports for your team. Start free at codeinspector.app.
More videos
- CodeInspector product demo (0:54)
- CodeInspector Now Works Inside Claude: Scan and Fix Code in One Conversation (video) (1:20)
- throw vs throw ex in C#: Why throw ex Destroys Your Stack Trace (video) (1:22)
- Thread.Sleep vs Task.Delay in C#: Which to Use and Why (video) (1:38)
- Task.Result and .Wait() Deadlocks in C#: Why Sync-over-Async Hangs (video) (1:47)
- ILogger Best Practices in .NET: Structured Logging That Helps You Debug (video) (1:20)