Do you put optional parameters at the end?

Updated by Brook Jeynes [SSW] 1 year ago. See history

123

Optional parameters should be placed at the end of the method signature as optional ones tend to be less important. You should put the important parameters first.

public void SaveUserProfile(
[Optional] string username,
[Optional] string password,
string firstName,
string lastName,
[Optional] DateTime? birthDate
) {}

❌ Figure: Figure: Bad example - Username and Password are optional and first - they are less important than firstName and lastName and should be put at the end

public void SaveUserProfile(
string firstName,
string lastName,
[Optional] string username,
[Optional] string password,
[Optional] DateTime? birthDate
) {}

✅ Figure: Figure: Good example - All the optional parameters are the end

Note: When using optional parameters, please be sure to use named para meters

acknowledgements
related rules