Friday afternoon #coding #goodie

After our code retreat a couple of months ago I got some inspiration for improving code and specifically number of parameters. So instead of passing parameters all over the place and polluting methods creating long long long long long long long lines of unreadable stuff, there are now just 2 params for this particular method.

Before:

_contentRepository.List(request, marketFilter, showExtended, paging);
...
List(ReadMarketContents request, IEnumerable marketFilter, bool showExtended, IPage paging) {
  //do stuff
}

After:

_contentRepository.List(request, options);
 
List(ReadMarketContents request, dynamic options) {
  //do stuff
}

Since “options” is a dynamic expandoobject I can assign and read data from it as I please with the usual dot-notation without the reflection-crap making things unreadable, since the compiler handles it for me.

dynamic options = new ExpandoObject();
options.MarketFilter = base.Request.GetMarketFilter();
...
var marketFilter = options.MarketFilter as IEnumerable;

And all this goodness without getting a class explosion for each and every request case like in this example. Think it might be a good fit for these kind of optional parameters where scope is limited, since knowledge is required.

Tagged , ,