Keyword Arguments

Brian Carper blogs about keyword arguments in Ruby, Clojure and Common Lisp:

Why are keyword arguments good?

  • You can omit arguments.
  • You can supply arguments in an arbitrary order.
  • Arguments are labeled, so you know what argument means what.

Positional arguments require mentally lining up the 7th argument in your function call with the 7th argument in the function signature, and so on. Keyword arguments become more and more attractive the more arguments you have in your function signature.

Keyword arguments trade a bit of verbosity for added explicitness, clarity and reduced mental burden. (Kind of like Lisps do overall. Fancy that.)

More mainstream languages should natively support keyword/named arguments (and increasingly, they do). Generally it is a good trade-off between clarity and conciseness.

The prototypical example (not mentioned in the article) is Smalltalk and derivatives languages such as Objective-C and Objective-J, where every argument is prefixed with the argument name.

Objective-C example:

[self presentModalViewController: controller animated: YES];

Which, in a C-style language using the less verbose naming convention (that C-style languages tend to have):

this.presentModal(controller, true);

Advantage? More concise. Disadvantage: what is that true argument for again?