Required fields in a class

I'm working with a request my boss just gave me, and was looking for some outside input.

His aim is to consolidate all committing of parts on a work order to go through one consolidated method… this, in essence, is fine.

So we start listing off each of the required parameters for this method, and it grows to a fairly sizable list… easily over 15 parameters would be needed.

An alarm goes off in my head for any parameter list whose length is greater than 3 or 4 … and usually I will opt for passing an object to the method (because OOP)… He is fine with this if we can have "Required" fields in the passed class… or some way to say to the programmer that field X is required upon building… if it's not populated, throw an exception.

In his mind, including all required parameters in the method declaration is preferred versus passing an object with possibly null/default values… I point out that it could easily have the wrong order of parameters, or duplicated same type parameters… but to him this is still the preferred route.

The avenue we typically take is to use a custom validator upon "save" that will validate the payload and check for any blank/improper fields… but this is post-build, and if there is already a better way of accomplishing this before we start in-house testing the code, it feels like we are constantly reinventing the wheel.

I've done some research this morning, but nothing I am finding is answering the core question… I fear there is no way to supplement a programmer's due diligence here.

Any thoughts would be greatly appreciated.

by Elementally via /r/csharp

Leave a Reply