Hacker News new | ask | show | jobs
by j_baker 5877 days ago
Named parameters make code more readable when used properly. For example, which is more readable?

    var c = new Cube(1, 2, 3)
or:

    var c = new Cube(height: 1, width: 2, depth: 3);
1 comments

The real advantage of named parameters is the ability to have optional parameters as well. Which means that instead of:

   var c = new X(3, "blue", null, null, null, null, false, true, null, 1, null);
(which, sadly, is an all too common implementation pattern), you can have:

   var c = new X(foo: 3, bar: "blue", baz: false, quux: true, wibble: 1);
Which seems quite a bit better.

Another advantage of named/optional parameters is that you can use them in the construction of immutable objects, whereas you don't get that advantage if you use settable properties.