Returning input arguments

I have finally laid my eyes on the book Clean Code by Robert C. Martin. I am just quickly browsing it because I think I am already familiar with most of the practices but sometimes something catches my eye.

In the chapter about monadic functions (functions with one argument) author mentions this function:

He says:

Using an output argument instead of a return value for a transformation is confusing. If a function is going to transform its input argument, the transformation should appear as the return value.

And suggests to refactor to something like this (where you can return the input argument):

I am not quite sure about happily returning the input argument. From the signature of the function it is not obvious to me that the returned instance is the same as the input one. It seems more like it is returning a different object (maybe a copy?).

Another thing is that if we return the  argument and change the object, then the function still uses the argument as an output argument. That doesn’t seem too DRY to me because then the function returns the same object through 2 different channels.

My conclussion is that I think that if a function transforms an input argument then the clearer way to tell that is not to mask it by the return value that is somewhat redundant.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">