Yonatan Karp-Rudin
Yonatan Karp-Rudin

Follow

Yonatan Karp-Rudin

Follow
Kotlin Code Smell 008 - Too Many Arguments

Kotlin Code Smell 008 - Too Many Arguments

many arguments causing confusing and assigning wrong arguments

Yonatan Karp-Rudin's photo
Yonatan Karp-Rudin
·Dec 11, 2022·

2 min read

Play this article

Table of contents

  • Problems
  • Solutions
  • Exceptions
  • Sample Code
  • Conclusion
  • Credits

TL;DR: Don't pass more than three arguments to your functions.

Problems

  • Low maintainability

  • Low Reuse

  • Coupling

  • Readability

Solutions

  • Find cohesive relations among arguments

  • Create a "context".

  • Consider using a Method Object Pattern.

  • Avoid "basic" Types: strings, arrays, integers, etc, and think in terms of objects.

Exceptions

  • Operations in the real-world needing no cohesive collaborators.

Sample Code

Wrong

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()
}

Right

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()
}

Conclusion

Relate arguments and group them. Always favor real-world mappings. Find in the real world how to group the arguments into cohesive objects.

If a function gets too many arguments, some of them might be related to class construction. This is a design smell too.

Credits

Did you find this article valuable?

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

Learn more about Hashnode Sponsors
 
Share this