I’m a big fan of Bob Martin’s Clean Code, A Handbook of Agile Software Craftsmanship. I wrote about it (briefly) here: https://medium.com/@pagalvin/i-m-working-with-a-few-of-my-colleagues-on-an-official-set-of-programming-standards-47f79d46f183. He and co-authors lay out a set of principles among which are:
I’m not quoting the book here, just paraphrasing.
I try hard to keep my function parameters down to one or zero, but sometimes, it doesn’t feel right. Here’s an example:
The gist above shows a function that that uses jQuery to assign click handlers to icons. These icons are children to a parent element. I know the the ID of the parent and I know the jQuery selector required to find the appropriate icons.
The gist shows the same method implemented two ways. The first method takes three distinct parameters and would be invoked like:
In the second case, I’m passing an object:
It looks really similar to the three-parameter version and probably still constitutes a violation of the single-argument principle. That said, this approach feels pretty good for a couple of reasons:
In short, intellisense guides me through the process of filling in the parameters and helps reduces the risk that I make a mistake.
Here’s a video showing intellisense at work:
Is anyone else doing this? Tried it and stopped? Have any other objections? Please leave a comment and let me know.
</end>
Postscript: If you liked this article, you may also like my free book, Yet Another TypeScript Book. Please check it out.