Skip to content

Latest commit

 

History

History
61 lines (48 loc) · 1.27 KB

GCop408.md

File metadata and controls

61 lines (48 loc) · 1.27 KB

GCop 408

"Flag or switch parameters (bool) should go after all non-optional parameters. If the boolean parameter is not a flag or switch, split the method into two different methods, each doing one thing."

Rule description

If the purpose of a Boolean argument is a flag or a switch, it should go after the main parameters for better readability, and also to enable the caller to specify the name of the parameter before the true/false expression see GCop117.

If the role of the Boolean argument is not just a switch, and instead it's to specify the primary action, then the method design should change into two separate methods.

Example 1

public static void Login(bool rememberPassword, string userId)
{
    ...
}

should be 🡻

public static void Login(string userId, bool rememberPassword)
{
    ...
}

Example 2

public void Invite(bool telephone, User user)
{
    if (telephone)
    {
        ...
        //Send an invitation SMS
    }
    else 
    {
        ...
        //Send an invitation email
    }
}

should be 🡻

public void InviteByTelephone(User user)
{
    ... 
    //Send an invitation SMS
}

public void InviteByEmail(User user)
{
    ...
    //Send an invitation email
}