Optional parameters should not be used

Hello-

This is regarding the issues reported by the C# rule "Optional parameters should not be used". As per rule description:

The overloading mechanism should be used in place of optional parameters for several reasons:

  • Optional parameter values are baked into the method call site code, thus, if a default value has been changed, all referencing assemblies need to be rebuilt, otherwise the original values will be used.

-This reason is not applicable in our project since its not public API.

  • The Common Language Specification (CLS) allows compilers to ignore default parameter values, and thus require the caller to explicitly specify the values.

- Since we are using MS JIT compiler this reason is also not applicable.

  • The concept of optional argument exists only in VB.Net and C#. In all other languages like C++ or Java, the overloading mechanism is the only way to get the same behavior.

- Since programming language for our solution is C#, this reason is not applicable.

  • Optional parameters prevent muddying the definition of the function contract. Here is a simple example: if there are two optional parameters, when one is defined, is the second one still optional or mandatory?

- In our project only one optional parameter is being used, so this reason is also not applicable.

Also, creating overloads for a method with same implementation to avoid optional param will result extra lines of code so probability of more defects. It also avoids the redundancy of the code.

So do you think its appropriate the mark these issues as false +ve ?

If you are not creating a library that is going to be used by other applications written using languages different from VB.NET and C#, I think it is safe to even disable this rule. Optional parameters is a nice language feature that is worth using because it makes the code simpler and often more readable.

Hi @sontakke.vishal,

Does @Val post answered all your questions?

Cheers,
Amaury

Yes, this answers helped us. Thank you!