SSW Foursquare

Do you put optional parameters at the end?

Last updated by Brook Jeynes [SSW] 8 months ago.See history

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: 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: Good example - All the optional parameters are the end

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

We open source. Powered by GitHub