Request object, what are the pros and cons?

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:

  • you are not the one who changes your code (ie GetMusic )
  • someone can review the code and find the class 'SongReqest' useful (so situation goes even worse - from a local usage to a global usage of a class)
  • adding the SongReuest class can add an additional dependencies for you method (is someone changes this class, most likely you founction will not compile)
  • using SongRequest as a property bag locks it usage only as as a class, as mentioned before.
  • using this class, you method would probably never share it parameters with other function calls (for what reason?)
  • finally, using 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.


    There is one major advantage to passing an object -

    If you have an object used as the parameter, such as SongRequest , the object can be responsible for its own validation. This allows you to dramatically simplify the validation in each method that uses the object, as you pretty much only need to check for null, instead of checking each and every parameter.

    In addition, if you have many parameters, it can often be simpler to generate a single object. This is especially true if you find you need multiple overloads to manage different combinations of parameters.

    That being said, each situation is unique. I would not recommend one approach over the other for every situation.

    链接地址: http://www.djcxy.com/p/55054.html

    上一篇: 如果默认为Chrome,如何在Python中打开一个新的默认浏览器窗口

    下一篇: 请求对象,有什么优点和缺点?