Day 18 - Investigate long parameter lists

Since @ben seems to have forgotten to unlock or create today’s exercise I create it myself. Ben feel free to delete/merge this topic if it has been hidden somewhere else.

######## EXERCISE AS SENT OUT BY BEN ##########

Do a search through your project for long parameter lists, where long is defined as more than three parameters.

Due to long parameter lists often being line-wrapped, you’ll likely have to do some manual searching, but here’s a naive regex that will find single-line method calls (in Ruby, at least) with at least four parameters:

(.*,.*,.*,.*)

You’ll probably find that many of your long parameter lists appear when calling library or framework code.

However, you might find some in your own code. If you do, it doesn’t necessarily mean there’s anything wrong, but it’s worth asking yourself a few questions about them:

  1. Should any of the data that you’re passing in be instance data instead? A clear indication this is true is if other methods on this object also require the same parameter.

  2. Do you frequently pass several of these parameters together? If so, it’s possible you have a Data Clump and could benefit from extracting a value object to contain them.

  3. Are any of these parameters booleans? If so, you probably have a case of control coupling, and would do well to remove it.

  4. Can any of these parameters be removed outright? You’d be surprised how easy it is to continue passing something into a method that no longer requires it.

By the way, the above questions are worth asking about just about any list of parameters, so consider them even if you tend to keep your parameter lists short.

Good luck!

1 Like

Loved the two provided links!

Used Netbeans Project Regex Search to quickly find all functions within my own bundles.

(.*,.*,.*,.*)\{

Surprisingly I only had one function in my php(sendMail) files and only some addListener functions in my js files which I both kept as they were. I also only had a few with 3 parameters. So a quick one for me :grinning:.

Thanks so much for posting this, @trufflepiggy!

Sorry it wasn’t up in time.

1 Like

This article explaining the types of Coupling is awesome. Thank you for it.

Difficult exercise in C, but I found a data clump that had already been collected into a struct. So, just needed to change the function signature to pass in the struct instead of breaking it up in the call.

We have a lot of these - most of them with 4 params. I also noticed that most of them are in small helpers that aren’t really used as objects at all… I spent the 20 minutes going through all the cases and working a bit on one of them. The links are great, thanks :+1:

1 Like

This is another free for me. But I don’t have the concept of data clump. Even in this project I have not long parameters I’ll use this 20 minutes in others projects that I know this could help a lot. Thanks

While looking for long parameters lists I’ve noticed data clump of a couple of parameters and was able to extract them into a struct. this turned out to be a major cleanup since these parameters appeared all over the code! Awesome win! loved every minute of it :smiley: