Skip to main content
Share Your Experience: Take the 2024 Developer Survey
226 votes
Accepted

Is there a name for the (anti- ) pattern of passing parameters that will only be used several levels deep in the call chain?

The data itself is called "tramp data". It is a "code smell", indicating that one piece of code is communicating with another piece of code at a distance, through intermediaries. Increases rigidity ...
BobDalgleish's user avatar
  • 4,724
138 votes
Accepted

Should we avoid custom objects as parameters?

Using a custom object to group related parameters is actually a recommended pattern. As a refactoring, it is called Introduce Parameter Object. Your problem lies elsewhere. First, generic Window ...
Euphoric's user avatar
  • 37.5k
108 votes

Is there a name for the (anti- ) pattern of passing parameters that will only be used several levels deep in the call chain?

I don't think this, in itself, is an anti-pattern. I think the problem is that you are thinking of the functions as a chain when really you should think of each one as an independent black box (NOTE: ...
JimmyJames's user avatar
  • 27.5k
100 votes
Accepted

Is using parameter names that differ from type names only by casing considered a bad practice in C#?

Don't overthink this, Range range is fine. I use such kind of naming for more than 15 years in C#, and probably much longer in C++, and have never experienced any real drawbacks from it, quite the ...
Doc Brown's user avatar
  • 209k
94 votes
Accepted

Is it wrong to use a boolean parameter to determine values?

API design should focus on what is most useable for a client of the API, from the calling side. For example, if this new API requires the caller to write regularly code like this if(flag) foo....
Doc Brown's user avatar
  • 209k
72 votes

Is there a name for the (anti- ) pattern of passing parameters that will only be used several levels deep in the call chain?

BobDalgleish has already noted that this (anti-)pattern is called "tramp data". In my experience, the most common cause of excessive tramp data is having a bunch of linked state variables that should ...
Ilmari Karonen's user avatar
42 votes

Should I raise an exception/error when an optional argument is used but is not necessary?

Exceptions should be thrown in exceptional situations. If an exception is thrown that could be avoided, that’s a bug on the caller side. So you force the caller to change their code from Fetch_person (...
gnasher729's user avatar
  • 45.7k
40 votes

Is it wrong to use a boolean parameter to determine values?

Martin Fowler quotes Kent Beck in recommending separate setOn() setOff() methods, but also says that this should not be considered inviolable: If you pulling[sic] data from a boolean source, such ...
Graham Lee's user avatar
39 votes
Accepted

Pass object twice to same method or consolidate with combined interface?

No, this is perfectly fine. It merely means that the API is over-engineered with regards to your current application. But that doesn't prove that there will never a use case in which the data source ...
Kilian Foth's user avatar
38 votes

Is there a name for the (anti- ) pattern of passing parameters that will only be used several levels deep in the call chain?

I am not aware of a specific name for this, but I guess it is worth to mention that the problem you describe is just the problem of finding the best compromise for the scope of such a parameter: as a ...
Doc Brown's user avatar
  • 209k
27 votes

Should we avoid custom objects as parameters?

You say it's not quite easy to test individually, because it needs a real Student object to call the function But you can just create a student object to pass to your window: showInfo(new Student(...
Tom Bowen's user avatar
  • 370
26 votes

Should I raise an exception/error when an optional argument is used but is not necessary?

If the function caller supplies the right kind of person_id the country_code is not necessary. This implies that a country_code is somehow embedded in a fully-qualified person_id. In the case of a ...
David Hammen's user avatar
  • 8,321
25 votes
Accepted

Is "too many parameters" a visual or a logical issue?

It is first and foremost a logical issue (which often comes with visual issues, too). The wrong solution to this is by trying only to improve the visual problem by encapsulate parameters in a ...
Doc Brown's user avatar
  • 209k
24 votes

Should we avoid custom objects as parameters?

In layman's terms: What you call a "custom object" is usually simply called an object. You cannot avoid passing objects as parameters when designing any non-trivial program or API, or using any non-...
Tulains Córdova's user avatar
24 votes
Accepted

Optional parameters or overloaded constructors

I prefer multiple constructors over default values and personally I don't like your two constructor example, it should be implemented differently. The reason for using multiple constructors is that ...
t3chb0t's user avatar
  • 2,554
22 votes

Is there a name for the (anti- ) pattern of passing parameters that will only be used several levels deep in the call chain?

I believe the pattern you're describing is exactly dependency injection. Several commenters have argued that this is a pattern, not an anti-pattern, and I would tend to agree. I also concur with @...
Quuxplusone's user avatar
19 votes

Is there a use case for boolean parameters?

I have one assert(a == 1); Why is this OK? Because the boolean is the point of this interface. No one has trouble remembering what it means. That's not always the case so the advice to consider ...
candied_orange's user avatar
19 votes

What is the purpose of enclosing all return values and arguments of a method in separate classes?

Whether it's a widespread convention (I hope it's not), I don't know, but I agree with @Telastyn. Those are the same reason why many people keep naming classes with DTO. I will ignore the DTO thing on ...
Laiv's user avatar
  • 14.7k
18 votes
Accepted

Are "in" parameter modifiers considered a code smell?

You've taken the line from MSDN out of context. The most important part of the page is the first sentence: "The in keyword causes arguments to be passed by reference." It is primarily in this respect ...
Sebastian Redl's user avatar
18 votes

Should I raise an exception/error when an optional argument is used but is not necessary?

Note the caveat mentioned below. No, you shouldn't raise an exception. If you take this approach, this inherently means that which data source you use is an internal implementation detail of the ...
Flater's user avatar
  • 51.7k
17 votes

Optional parameters or overloaded constructors

Considering the only thing you're doing in the constructor is simple assignment, the single constructor solution may be a better choice in your case. The other constructor provides no extra ...
Andy's user avatar
  • 10.3k
16 votes

Is using parameter names that differ from type names only by casing considered a bad practice in C#?

I do this all the time, it is giving me great piece of mind. If it is an argument passed to a constructor that needs to be assigned to a member, my member would be named range as well and the ...
Martin Maat's user avatar
  • 18.4k
16 votes

What is the purpose of enclosing all return values and arguments of a method in separate classes?

What is the purpose of doing so? Potentially, it allows you to violate the open closed principle and add things to these DTOs without impacting the rest of the code as much. It also potentially ...
Telastyn's user avatar
  • 109k
15 votes

Is there a name for the (anti- ) pattern of passing parameters that will only be used several levels deep in the call chain?

This is pretty much the textbook definition of coupling, one module having a dependency that deeply affects another, and that creates a ripple effect when changed. The other comments and answers are ...
Karl Bielefeldt's user avatar
13 votes
Accepted

Is it bad to create classes whose sole purpose is to be converted to another class implicitly?

While it isn't always bad, it is pretty rare that implicit conversions are your best option. There are issues. Implicit conversions are not very readable. Because you're making a new object, any ...
Telastyn's user avatar
  • 109k
12 votes

How to eliminate a if-else block which is used to determine behaviour according to a boolean input?

You say, "... this implementation only moves the if-else from inside to outside" as if that's no big deal but in fact the second example moves the if check closer to the code that knows what the ...
Daniel T.'s user avatar
  • 3,043
11 votes
Accepted

Are all boolean arguments flag arguments, and thus a code smell?

No rule is absolute in software development and everything depends on context. That being said, I would not consider your example to be necessarily a good case for boolean arguments. An API like show(...
simoraman's user avatar
  • 2,338
11 votes
Accepted

Parametrize methods vs global variables

Don't use global variables. Also, don't pass parameters down chains of functions! Its difficult because you don't use an actual example. But normally there will be a better approach. Lets say we ...
Ewan's user avatar
  • 76.4k
11 votes

How to avoid changing the signature on a well-used method?

That a design is hard to change in a particular way is not always a problem, but in this case I think there is a superior design that makes this change easier. If the method instead of taking half a ...
walpen's user avatar
  • 3,231

Only top scored, non community-wiki answers of a minimum length are eligible