Friday, January 4, 2013

A data transfer object may be the cure for a smelly method signature.

I thought of Robert C. Martin's words as I had retyped them here from his Clean Code treatise...

The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification - and then shouldn't be used anyway.

...as I was abstracting a method in a web form's code behind out to a standalone class. The method which took no parameters at its signature now needs to be handed the string values kept in numerous web form controls. At first I started writing a method with twenty string variables in the signature as I've seen this sort of thing elsewhere in a sister codebase. I then thought of Uncle Bob's words and considered handing in a string array instead. This would introduce an encoding however and just be more confusing. The thing to do here is to roll a DTO (data transfer object). I should populate the DTO before calling the method and then just hand the method the DTO which will be the only thing in the method's signature.

No comments:

Post a Comment