Monday, 1 June 2015

Named Parameters

Named parameters, let you write more comprehensible code, buy letting the reader know explicitly what parameters correspond with the values being passed to the method, function or constructor.

Generally when we pass arguments to functions, methods, or constructors; we pass them in the sequence that they appear in the signature.

For example if we had a printName() function that took in a persons information and printed their full name we might have something like this.


namespace pav.namedArguments;

class Program
{
    public static void printName(string first, string middle, string last,
bool isMale, bool isMarried, bool isDoctor){
        var prefix = isDoctor ? "Dr." : isMale ? "Mr." : isMarried ? "Mrs." : "Ms.";
        middle = String.IsNullOrEmpty(middle) ? " " : $" {middle} ";
       
        Console.WriteLine($"{prefix} {first}{middle}{last}");
    }

    static void Main(string[] args)
    {
        printName("Robert", "Mike", "Smith", true, true, false);
        printName("Jessica", "Lyne", "Johnson", false, true, true);
        printName("Megan", "Eli", "Jones", false, true, false);
        printName("Tom", "", "Williams", false, true, true);
        printName("Kristy", "Grace", "Gray", false, false, false);
        printName("Amanda", "", "Harding", false, false, false);
    }
}


Now what you might not know, is the fact that you do not have to  pass the parameters in the specific order, you can leverage, named parameters and pass the values to your function in any order you please. To do so, you simply append the parameter name with it's corresponding value.

namespace pav.namedArguments;
class Program
{
    public static void printName(string first, string middle, string last, bool isMale, bool isMarried, bool isDoctor)
    {
        var prefix = isDoctor ? "Dr." : isMale ? "Mr." : isMarried ? "Mrs." : "Ms.";
        middle = String.IsNullOrEmpty(middle) ? " " : $" {middle} ";

        Console.WriteLine($"{prefix} {first}{middle}{last}");
    }

    static void Main(string[] args)
    {
        // no named parameters
        printName("Robert", "Mike", "Smith", true, true, false);

        // named parameters, for obscure values
        printName("Jessica", "Lyne", "Johnson",
                  isMale: false, isMarried: true, isDoctor: true);

        // named parameters for everything
        printName(first: "Megan", middle: "Eli", last: "Jones",
                  isMale: false, isMarried: true, isDoctor: false);

        //named parameters not needed, but position matters
        printName("Tom", last: "Williams",
                  isMale: false, isMarried: true, isDoctor: true, middle: "");

        // jumbled parameter input
        printName(last: "Gray", first: "Kristy", isDoctor: false,
                  isMale: false, middle: "Grace", isMarried: false);

        // some sort of logic behind named parameters
        printName("Amanda", last: "Harding", isDoctor: true,
                  isMale: false, isMarried: false, middle: "");
    }
}


Named parameters have one main use, they make can make it easier to understand your code, take a look at the second and third print name method calls. The named parameters make it much easier to understand what the meanings behind the values are. 

However if named parameters are abused like in the last two examples, they can make your code painful to read.