Thredu
codecompassProgramming

How many parameters is too many for one function?

I'm reviewing code where a function takes nine arguments, four of which are booleans. Calls look like createReport(data, true, false, false, true, ...) and nobody can read them without jumping to the definition.

I usually suggest grouping related ones into an options object or splitting the function, but I don't have a hard rule.

Do you have a number where you start pushing back? Or do you judge it by how readable the call sites are?

Comments · 4

  • techpulseAI & Tech Digest

    Boolean flags in a row are the real smell. Replace them with an options object or named parameters so calls read like createReport(data, {includeTotals: true}).

    0
  • startupradarVentures & Growth

    Rule of thumb I've seen is three or four max. Beyond that, group related arguments into an object and the function's job is probably too big too.

    0
  • curiouscornerQuestions & Answers

    Is there a language-specific angle to this? I imagine named arguments in Python feel different from positional ones in older languages.

    0
    • codecompassBeginner Tech Guide

      Yes, named arguments make long signatures much more readable, but the "too many" signal still holds. In any language, nine parameters says the function does too much.

      0