You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This rule should not exist at all. You agreed with me about this, but it's still in there?
Function parameters are always automatically defined in the local scope and initialized by PowerShell to their default value (the equivalent of null -- which comes out as an empty string or a zero for numerical values, or a default struct, etc)
This happens even when they are part of an unused parameter set.
There is absolutely ZERO value in setting them yourself.
I repeat. There is nothing to be gained by setting them yourself *unless you actually need them to default to something other than $null.
If anything, setting a default value to $null, 0 or an empty string should cause a performance and readability warning. Initializing a variable to it's default value is just extra work; extra code that accomplishes nothing. It might make your script (infinitesimally) slower, and make future editors of your script pause to understand what you're doing ... whilst changing nothing.
NOTE: I ALREADY FILED THIS BUG ONCE. You even agreed with me and supposedly fixed it (to warn when default values are assigned to mandatory parameters), but it is still wrong in v1.4 (See #362)
The text was updated successfully, but these errors were encountered:
C:\WINDOWS\system32> gmo psscriptanalyzer
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.4.0 psscriptanalyzer {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer}
C:\WINDOWS\system32> Get-ScriptAnalyzerRule -Name *default*
RuleName Severity Description
-------- -------- -----------
PSAvoidDefaultValueSwitchParameter Warning Switch parameter should not default to true.
PSAvoidDefaultValueForMandatoryPara Warning Mandatory parameter should not be initialized with a
meter default value in the param block because this value will
be ignored.. To fix a violation of this rule, please
avoid initializing a value for the mandatory parameter in
the param block.
This rule should not exist at all. You agreed with me about this, but it's still in there?
Function parameters are always automatically defined in the local scope and initialized by PowerShell to their default value (the equivalent of null -- which comes out as an empty string or a zero for numerical values, or a default struct, etc)
This happens even when they are part of an unused parameter set.
There is absolutely ZERO value in setting them yourself.
I repeat. There is nothing to be gained by setting them yourself *unless you actually need them to default to something other than $null.
If anything, setting a default value to
$null
,0
or an empty string should cause a performance and readability warning. Initializing a variable to it's default value is just extra work; extra code that accomplishes nothing. It might make your script (infinitesimally) slower, and make future editors of your script pause to understand what you're doing ... whilst changing nothing.NOTE: I ALREADY FILED THIS BUG ONCE. You even agreed with me and supposedly fixed it (to warn when default values are assigned to mandatory parameters), but it is still wrong in v1.4 (See #362)
The text was updated successfully, but these errors were encountered: