Named parameters (aka Let’s Join A Bandwagon)

I saw a post on Dion Almaer’s blog on named parameters, and I thought I’d add my 2 cents worth.

Named parameters are certainly nice, and I wish Java had them. But readability isn’t the reason. Consider this example:

Window w = new Window(x:0, y:0, color:blue);

If what I’m concerned about is readability, I can solve that with decent variable names. The bulk of the time, you’d be passing variable references in anyway, rather than primitives or consts, so that’s okay.

No, what named parameters saves you on is when you misremember the order of the parameters (or when they get changed on you). Assume that in the Window example, all the arguments are ints or longs. If someone decides to change the order of the parameters (maybe they decide colour belongs first), your code breaks.

You can solve this some of the time via decent types for parameters, but not always. Named parameters help a lot here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s