Description
I have a non-mandatory parameter that is defined like so:
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$NuGetVersion,
This is generating a warning:
Variable 'NuGetVersion' is not initialized.
Non-global variables must be initialized. To fix a
violation of this rule, please initialize non-global
variables.
While parameter may not be global variables, their initial value is typically provided outside the scope of the function. I've used parameter checks against $null to test if the parameter was supplied. I guess I could initialize these parameters to some value (say $null). I'm just not convinced that all non-mandatory parameters should be initialized with a default value.
Here's an example where initialization gets tricky due to value types:
function foo {param([Parameter()][DateTime]$d) "d is $($d -eq $null)"}
Parameter $d is not initialized and this would gen a warning. However, if I try to set $d to $null that will error saying that null can't be converted to DateTime. However if no argument is passed for $d, the function above will return True - $d is equal to $null in this scenario. So to eliminate this warning I have to initialize to some valid DateTime value - blech. I guess I could resort to checking $PSBoundParameters but that seems clunky when a simple $null check would suffice.