C# Best Practices - Specify Clear Method Parameters

Improve parameters

parameter order

public OperationResult PlaceOrder(Product product, int quantity, bool includeAddress, bool sendCopy)

Acted opon or key to the operation (like product)

Required for the operation

Flags (like inclludeAddress)

Optional parameters

Best Practices

Do:

Define coherent parameters names

Defind an XML document comment for each parameter

Keep the number of parameters to a minimum

Order the parameters in a logical sequence

Use a consistent parameter order

Avoid:

Unused parameters

Named arguments

public OperationResult PlaceOrder(Product product, int quantity, bool includeAddress, bool sendCopy)

var result = Vendor.PlaceOrder(product, quantity:12, includeAddress:true, sendCopy:false);

Named arguments Best Practices

Do:

Use named arguments as needed for clarity when calling a method

Avoid:

Unnecessary named arguments (PlaceOrder(product:product...))

Optional Parameters

public OpertionalResult PlaceOrder(Product product, int quantity, DateTimeOffset? deliverBy = null, string instructions = "standard delivery")

Features:

Specify a default vallue

Are optional when the method is called

If argument is not provided, default is used

Can dramatically reduce the number of overloads

Notes:

Optional parameters must be defined after required parameters

When calling the method, if an argument is provided for any optional parameter, it must also provide arguments for all preceding parameters, or use named arguments.

Best Practices

Do:

Use optional parameters to minimize overload bloat

Avoid:

Optional parameters when the parameters are one or the other

Optional parameters if default could change and component versioning is important

Ref & Out

By Value or By Reference

public bool PlaceOrder(Product product, int quantity, ref string orderText)
public bool PlaceOrder(Product product, int quantity, out string orderText)

ref

Argument passed "by reference"

Argument variable must be initialized

Parameter values can be changed in the method

Changes are reflecting in the calling code

out

Argument passed "by reference"

Argument variable must be declared

Parameter values must be set in the method

Changes are reflecting in the calling code

Best Practices

Do:

Use ref when the method expects an incoming value

Use out when the method expects no incoming value

Avoid:

ref and out where feasible, return an object instead

FAQ

1.What is a named argument and when should it be used?

A named argument uses the parameter name when calling the method

Used to clarify the purpose of an argument and define arguments without concern for there position in the parameter list

2.How is an optional parameter defined?

By sepecify a default value

3.What is difference between passing an argument by value vs by reference?

When passed by value (which is default), the value of the argement is passed to the method.

When passed by reference (use ref or out), the variable is effectively passed to the method.

Because of this, passing by reference enables the method to change the value of the parameter and have that changed reflected in the calling code.

4.What is the difference between ref and out?

A ref parameter requires that the argument be initialized before it is passed.The method can modify the value for the ref parameter.

A out parameter must be declared,but not initialized before it is passed.The method must provide a value for the out parameter.

你可能感兴趣的:(C# Best Practices - Specify Clear Method Parameters)