0

This isn't really much of a problem in as much as it's a bit of a Code Smell IMHO.

I have a Method in my web service, which has multiple parameters (16 to be exact), I've had a look at This Question which is on the right lines, but doesn't really answer my question, which is "Is there a better way to assign multiple arguments to multiple parameters?", I'm looking for a way to avoid having to manually assign 16 values.

Community
  • 1
  • 1
Captain_Custard
  • 1,308
  • 6
  • 21
  • 35
  • 3
    I think the main problem here is that there is no fixed single universally correct answer. The answer in the question you linked has one way, a parameter object, but ultimately a refactoring and separation of concerns and responsibilities is probably the way to go. – Lasse V. Karlsen Oct 12 '15 at 07:40
  • Absolutely agree, I would refactor and seperate concerns but this is actually taking information from a form on the Web, and then creating a new user in my application, so all of that information is needed so I can insert it into the DB, which is why i need 16 parameters – Captain_Custard Oct 12 '15 at 07:44
  • 1
    If you require 16 distinct pieces of information to create the user I say go with the parameters. If you lift those parameters into a parameter object you have to choose between assigning them through the constructor or through properties (or a mix). If you absolutely require those 16 values every time, you end up just assigning them with the constructor which means you have just created an abstraction layer that doesn't really abstract anything away. If you assign them through properties you will always have to weigh this against the risk of forgetting to assign one (new) property. – Lasse V. Karlsen Oct 12 '15 at 07:49
  • 1
    If at some point you have several ways of creating a user, each requiring different values (numbers, types, meanings), then a parameter object with various constructors might abstract away the concern of working out what all the values should be from the ones provided, and you can have just one method that actually creates the user, but if you don't (right now) have multiple ways to create a user, don't write code you won't need yet. – Lasse V. Karlsen Oct 12 '15 at 07:50
  • It's an interesting concept but I think like you say, there's no point writing code I might not need, so I think the Parameters option is the best solution – Captain_Custard Oct 12 '15 at 07:55
  • IMHO, in your case I would probably create a simple class (or struct) that will hold all of these parameters as properties and pass it to the method. If the method is responsible to perform a single action, such as insert a new user to the database, and all of it's parameters are related to that user, I see no reason to refactor further then gathering all that information into a single parameter. – Zohar Peled Oct 12 '15 at 07:58

1 Answers1

0

According to me you can have maximum 3 to 5 parameters in your method. Since you mentioned you are using web services, the number of reduced parameter will give you the ease of use, when you call the methods.,

The way you can reduce the parameters instead of passing each you can club it and make it as objects.

Something like.,

Avoid:

public bool IsEverthingCorrect(string p1, string p2,string p3.......string p16)
{
   //...//
   return false;
}

Use:

public bool IsEverthingCorrect(User userObj)
{
   //...//
   return true;
}

public class User
{
   public string userName { get; set; }
   //...// 
   public DateTime CreatedDate { get; set; }
}

I hope this helps...

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
  • 1
    A parameter object is a fine solution if many/most of those parameters are optional, in which case you simply don't assign a value to the corresponding property. However, if all the values are required, you have just substituted a method call with N parameters that will be verified at compile-time with a runtime check (which you *really* should add) that will complain. This is why I voted to close this question as "Primarily opinion-based". – Lasse V. Karlsen Oct 12 '15 at 08:13
  • It seems parameter objects are the only solution to this problem, and I rather agree with the above comment as the values are most definitely all required, thanks for your help! – Captain_Custard Oct 12 '15 at 08:20