mardi 18 juin 2019

Design solution to varargin in C#

Matlab has a feature, varargin (https://www.mathworks.com/help/matlab/ref/varargin.html), that allows for:

  • Variable input argument list
  • Text, value pairs
  • Parsing of the value pairs into a structure

I have been using this feature to set parameters, such as:

  • Choosing to provide verbose output (similar to logging)
  • Open graphics windows to describe the results of a computation (sort of like graphics logging)
  • Set parameters such as thresholds, search ranges, polynomial order for fitting, methods of interpolation (e.g. cubic spline, nearest neighbor).

A quick search on StackExchange for a similar feature in C# results in a number of opinions that perhaps this is not a good coding style or design.

Given a class with methods that I want to control parameters / features for computation and execution, what is the appropriate design that implements this functionality?

Seems to me, the intuitive answer is to have a property for each of the parameters, with values set by default in the constructor. Then, call property setting methods to sets these properties before calling the method.

The varargin offers a convenient way to do this without having to create properties for every single parameter.

Is varargin a good approach or will it result in unmanageable code / design?

Aucun commentaire:

Enregistrer un commentaire