Kotlin Code Smell 8 - Too Many Arguments

Kotlin Code Smell 8 - Too Many Arguments

Optimizing Function Design: The Art of Grouping and Simplifying Arguments

Play this article

TL;DR: Avoid passing more than three arguments to your functions.


  • Low maintainability.

  • Low reusability.

  • Coupling.

  • Readability.


  • Identify cohesive relationships among arguments.

  • Create a "context" or related group.

  • Consider using the Method Object Pattern.

  • Avoid using "basic" types such as strings, arrays, and integers, and instead think in terms of objects.


  • Operations in the real world that do not require cohesive collaborators.

Sample Code


class Printer {
    fun print(
        documentToPrint: String,
        paperSize: String,
        orientation: String,
        grayScales: Boolean,
        pageFrom: Int,
        pageTo: Int,
        copies: Int,
        marginLeft: Float,
        marginRight: Float,
        marginTop: Float,
        marginBottom: Float
    ): Unit = TODO()


class PaperSize {

class Document {

class PrintMargins {

class PrintRange {

class ColorConfiguration {

class PrintOrientation {

class PrintSetup(
    paperSize: PaperSize,
    orientation: PrintOrientation,
    color: ColorConfiguration,
    range: PrintRange,
    copiesCount: Int,
    margins: PrintMargins

class Printer {
    fun print(
        documentToPrint: Document,
        setup: PrintSetup
    ): Unit = TODO()


To improve code quality, identify and group related arguments. Aim for real-world mappings and cohesive objects.

If a function requires too many arguments, some of them might be better suited for class construction. This is also a design issue.

More Info


Did you find this article valuable?

Support Yonatan Karp-Rudin by becoming a sponsor. Any amount is appreciated!