Skip to main content
2 of 5
added 342 characters in body
Ben Cottrell
  • 12.1k
  • 4
  • 33
  • 44

The question does not really provide or link to any justification about why passing parameters as values might be a 'code smell' so it's impossible to address that particular claim without doing some wild guesswork about context, however in general there is absolutely nothing wrong with methods that accept plain simple primitive values/data as parameters.

Overall the two scenarios being compared are completely different. The first is focused on injecting a dependency into a constructor; the constructor parameters are also being used as part of a way of building the overall code structure. The second is just plain-simple argument passing into a method.

Dependency injection is generally focused around object creation and composition - the term 'dependency' used to imply that some kind of object graph which represents part of the overall code structure. Furthermore, the Dependency Injection Pattern is typically associated with having some kind of IoC Container or Framework performing all of this 'wiring'.

The example in the question of injecting a logger is a very common use of Dependency injection because loggers represent cross-cutting concerns, and logger objects themselves are probably going to be long-lived for most of the program's lifetime (sometimes known as 'singleton'). Loggers tend to be used in a lot of places, so passing a logger instance from the root of the program into every method would probably make the code feel rather bloated and cumbersome.

On the other hand, calling a method and passing a parameter is just that - there's nothing deeper behind it - such methods usually have a parameter list that only makes sense for that particular method, rather than being some cross-cutting list of parameters.

There's nothing inherently wrong with methods which accept individual values as arguments, though of course it could become cumbersome if the program starts to manifest a lot of different methods and behaviours which each use long lists of exactly the same parameters. But there's no hard-fast rule anywhere.

Some static code analysis tools may be configured to detect long parameter lists and flag this as a possible indicator of a problem (Usually a configurable threshold), but such warnings could equally apply to constructors accepting too many dependencies as well.

Ben Cottrell
  • 12.1k
  • 4
  • 33
  • 44