Let's say I have the following method:
public Stream GetMusic(string songTitle, string albumName) { ... }
A colleague of mine is convinced that this a bad method signature. He would like me to use a Request object, which would transform the method signature into this:
public Stream GetMusic(SongRequest request) { ... }
I really don't see the point of doing that. The only benefit I see is that it will be easier to add parameters in the future. We won't have to change the method signature, but the Request object still have to change.
Personally, I don't think it is a good idea. Using parameters makes it explicit what the method requires to run. In addition, this force us to create another object for not much.
What are the pros and cons of using Request objects? Are you using it in your projects and why?
You are getting data using GetMusic(...)
method. If so, it whould probably too much effort to use an additional entity without really need.
Indeed, in a situation, where is only one input parameter, you can use a custom class. But if that class is the only place to use, so if SongSignature
as the class name says, have to be used specifically for this class, that is a bad practice of using "parameter bags", because it lucks readability.
Additionally, if someone stupid says that SongSignature
must be a structure, and in that structure there is a pointer to some data to be changed inside method, that pointer would never really changes because every time GetMusic
is called, it will take a copy of a property bag.
Even if it is a class, you have to change the accessor for that class to public
, and in general this is not the best method to pass an arguments forward to a function and getting results from a function, because you have already getting a stream from that method.
Let's assume the following situation:
If in a team one programmer replaces the parameters with a class SongRequest
, second programmer did not find that it is used as a parameter to a functions (because it lucks info in a name of a class), and changed it to a structure on next iteration, third programmer used this method in such a way, that it have to be a class (for example have used class references inside SongRequest
)... As a result no one did really knowns why something is not working because each of them have dome right thing... There is no excuse to use a class for a local usage instead of implicit declaration of parameters.
Generally you have a good chances to get such a situation in a future, because:
GetMusic
)SongReuest
class can add an additional dependencies for you method (is someone changes this class, most likely you founction will not compile)SongRequest
as a property bag
locks it usage only as as a class, as mentioned before.SongRequest
class only for passing parameters for a specific function, gives additional memory overhead footprint, because if this method is called often, at one hand, it will create a lot of unnecessary objects in memory have to be garbage collected, in the other hand, if such a method is used rarely, it will be simply not practical to create a class to pass several variables to a single call
There is only one real reason to use class instead of a two string arguments: you programmer likes such calls and wants to make all code "more beautiful than before", more monadic, despite the fact that this is not very practical and useful.
I would never advice you to make a code looks like this until you want to make it looks better.
Generally, I suppose that using a custom class for passing an arguments for a function is a bad practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With