API Design: Method parameter objects

When defining a constructor or method with more than three parameters it is considered a best practice to create a parameters object for holding the values which are required. This especially holds true when some of the parameters are optional (as they will contain default values) and also when two or more consecutive parameters are of the same type (as developers may accidentally transpose these parameters – which will not result in compile time or runtime errors, making debugging a nightmare!)

The most appropriate solution for such cases is to break up the method into separate methods, however when this is not feasible creating a parameters object will improve both code readability and provide a cleaner design which leaves less room for unexpected errors.

Consider the following method:

The parameters defined in this method are typical of what you will often see, however it is much cleaner and reliable to create a parameters object for holding these parameters. In addition, a parameters object allows for a much easier client implementation as default values need not be reassigned for all parameters which proceed a parameter which you need to specify a value other than the default value. For instance, if a method accepts 5 parameters and the first two parameters are required but the last three are optional, if you you need to specify a value for the last parameter you will need to re-assign the default values for the proceeding parameters, when in fact you really only need to specify three parameters.

The following example demonstrates creating a parameter object which can be passed to “someMethod”:

When invoking “someMethod” clients can now simply instantiate an instance of the parameter object, set values only for what is needed and pass it in as follows:

So if you would like to improved code readability and provide proactive exception precautions, consider utilizing parameter objects for methods which require more than three parameters.

{ 2 comments to read ... please submit one more! }

  1. William from Lagos

    But how do we account for type checking at compile time. Suppose z in the params object is meant to be a string and not a number as passed, how do we guard against that?

  2. William: the parameters object is an instance of an actual parameters class you created specifically for the method your creating it for. Your class will have strong typed properties so your values will still be type checked.

    Eric: A great solution! I’ve seen plenty of this going on with anonymous objects passed as parameters (AS3 Tweener class for example) but never as a concrete parameters class. I think i’ll finally update some of my more unwieldy methods, thanks 🙂

{ 0 Pingbacks/Trackbacks }