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.

Author: Robert Watkins

My name is Robert Watkins. I am a software developer and have been for over 20 years now. I currently work for people, but my opinions here are in no way endorsed by them (which is cool; their opinions aren’t endorsed by me either). My main professional interests are in Java development, using Agile methods, with a historical focus on building web based applications. I’m also a Mac-fan and love my iPhone, which I’m currently learning how to code for. I live and work in Brisbane, Australia, but I grew up in the Northern Territory, and still find Brisbane too cold (after 22 years here). I’m married, with two children and one cat. My politics are socialist in tendency, my religious affiliation is atheist (aka “none of the above”), my attitude is condescending and my moral standing is lying down.

Leave a Reply

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

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

Facebook photo

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

Connecting to %s

%d bloggers like this: