6

How many inputs are "too much" for a constructor in C#? For example, what if I create a Constructor with 110 inputs?

class File
{

public File(string name, string id, int comment, .... (+107))
{
}

 }

I have maybe 3000 text files (or more), which contain attributes like: name, ID, comment, speed, and 106 others. I wanted to create a list of objects, like below:

List<File> file = new List<File>();
file .Add(new File("name", "id", "comment", "speed" (+ 106 others attributes));

and then here in the constructor to save all these attributes for each file, and then should I save all these in an Excel file.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 4
    Encapsulate those parameters in a new class like `FileAttributes` and then pass only that instance to the constructor, 110 reduced to 1. – Tim Schmelter Nov 03 '21 at 07:33
  • looks like a single *collection* of attributes, say, an *array*, `string[] attributes` – Dmitry Bychenko Nov 03 '21 at 07:40
  • 3
    Marking as opinion based. Short answer, only you can your team knows this – TheGeneral Nov 03 '21 at 07:48
  • If this is opinion based, why isn't the duplicate question https://stackoverflow.com/questions/40264/how-many-constructor-arguments-is-too-many also marked as opinion based? – Pod Nov 09 '21 at 22:27

3 Answers3

13

A huge part of programming is making source code readable for humans. Would you like to read a list of 110 constructor parameters, making sure they are all correct? Probably not.

Some argue for 1-3 parameters maximum, some argue up to 7 might be ok, it depend a bit how strict you are and what book you are reading. I'm not very dogmatic, so I would be more concerned about if the number of parameters makes sense in the context.

If you have a huge amount of parameters it signals something is wrong. Perhaps the class is doing too much, and should be split into separate classes? Perhaps some parameters are related and should really be grouped into more logical classes, lists, or some other datastructure?

See also Are there guidelines on how many parameters a function should accept, since this applies just as well to constructors as methods, and there is really nothing c# specific about the number of parameters.

If you have a bunch of text-files with some attributes, and presumably a value, you are probably better of describing this with some sort of key-value container, like a dictionary or List<(string, object)>. Another alternative would be some kind of de-serialization solution to automatically map keys/values to properties. But this returns to the first point, is it reasonable to need 100-ish properties to describe a single thing? Are you sure it is not better described as composed of multiple things

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
JonasH
  • 28,608
  • 2
  • 10
  • 23
4

Generally, there is not magic number of parameters in a method or constructor, that simply makes code good or bad.

But going over 10-15 parameters begs for refactoring.

The simpliest you could do is to define some DTO (data transfer object) that could carry all this info and you could simplify then constructor:

public File(FileInitialization fileInit) {...}

and define the class like:

public class FileInitialization { ...props...}

Going further, you could group some information, like general info (file name, extension), metadata (comments, etc.), etc. Then you get more granular code and still reduce params in cosntructor:

public File(GeneralFileInfo fileInfo, FileMetadata meta, ...)

One more suggestion is when creation of object is so complex, you could define some factory method, that would save you from providing all this complex information and encapsulate object creation and it intricacies.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

I guess you should encapsulate the inputs into a class, and pass it as one object instead of making your parameters in such way, this way will also help if you have any changes in the future. However I think if you write your case here people can help in better way, I mean write the case why you need to pass around 100+ parameters into your contractor.

Hamdan Dabbas
  • 537
  • 1
  • 5
  • 11