1

I'm fairly new to C# and programming in general, hence why I'm asking this question.

I recently got my code reviewed by a colleague, and he stated that for the following method, I should use a class for some parameters.

public List<Items> DoSomething(List<OtherItems> someItems, int amountOfItems, 
    DbGeopgrahpy Location)
{ //More stuff, irrelevant to the question.

He pointed out that I should put the amountOfItems and Location in a class which would contain those values instead.

This method will be called from a controller which returns this data to the client.

Now my question is why? I understand that it would improve readability (arguably imo), but is there any benefits to using classes as parameters? I think I can assume this would even impact performance negatively when you have to declare a class each time you want to call this method.

I also found that structs can be used this way, if this is a good practice, when should I use structs and when should I use classes?

  • 2
    *Now my question is why* - why didn't you asked him? There are many existing topics "struct vs class", e.g. [here](http://stackoverflow.com/q/3942721/1997232). – Sinatr Jan 03 '17 at 12:48
  • @Sinatr yeah I realize my mistake there, this person isn't at the company at the moment, so I can't ask questions anymore. Also, I'm aware about the use of structs vs classes, but when should I use them as parameters? –  Jan 03 '17 at 12:49
  • 1
    Ask yourself if that potential class/struct consisting of those two parameters has more usages than just in that call? Do you have an idea for a meaningfull name for such a class besides "MyLovelyClassUsedToCallDoSomething". If not forget it. – Ralf Jan 03 '17 at 12:53
  • @Ralf I see, so it wouldn't make sense to put those parameters in a class when only making that one call. Thank you! –  Jan 03 '17 at 13:03

4 Answers4

6

One of the benefit of using DTO class as method argument instead of collection of parameters is a preventing signature modification of method in future. This may be useful for API public methods, for example, as allow you to modify API without breaking changes / method versioning.

Let's say you need to update/modify method and so you need to pass additional argument. If your method expect only one argument, that is class/struct, then you do not need to change the signature of method. Instead you just add additional property to your DTO class.

Set
  • 47,577
  • 22
  • 132
  • 150
  • Ah that makes sense. I was mostly worried about it being confusing, since someone who wants to call this function has to look for the class/struct instead of reading the parameters. –  Jan 03 '17 at 12:58
2

It´s more a design awareness I think. If the method expects too many parameters that can be encapsulated in another object then that is spotting that you might haven´t done a good object definition. Also, as your software begin to grows you will find that it is much better to try to group everything in classes so you and the people that works with you will find much easier to mantain that way.

//avoid
public void Checkout(string shippingName, string shippingCity, 
       string shippingSate, string shippingZip, string billingName, 
       string billingCity, string billingSate, string billingZip)
{

}  

//DO
public void Checkout(ShippingAddress shippingAddress,BillingAddress billingAddress)
{
}

As you see, the common properties have been grouped to a common class that groups them. You have to apply your criteria and do a good grouping.

NicoRiff
  • 4,803
  • 3
  • 25
  • 54
  • But isn't it more confusing to create a class each time you want to call a method? Also, won't it impact performance negatively? –  Jan 03 '17 at 12:53
  • No, not every time. Don´t misunderstand. The idea is that you can probably group the parameters that will probably respond to a same object. I´ll add an example so you can see the point. – NicoRiff Jan 03 '17 at 12:55
  • I see, so it's mainly when you want to make it easier to read and when you want to possibly reuse the parameters? –  Jan 03 '17 at 13:01
  • Yes, you will find more advantages apart from this. @Set showed you some other advantages on having your code easier to mantain and to refactor. – NicoRiff Jan 03 '17 at 13:06
2

Check this => Choosing Between Class and Struct

ihsan
  • 59
  • 9
0

Personally, I find that if I have more than 3 or 4 parameters that are logically grouped (belong to the same sort of object or info), then I'll use a class.

This is very arbitrary like you are finding. Each person has their own set of rules as to when they want to use a class/struct.

Eric Burdo
  • 812
  • 1
  • 10
  • 24