Skip to content

PSProvideDefaultParameterValue is always wrong #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jaykul opened this issue Mar 7, 2016 · 3 comments
Closed

PSProvideDefaultParameterValue is always wrong #458

Jaykul opened this issue Mar 7, 2016 · 3 comments

Comments

@Jaykul
Copy link
Contributor

Jaykul commented Mar 7, 2016

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)

@raghushantha
Copy link
Member

Hi Joel.

This rule does not exist in v1.4 of ScriptAnalyzer. In fact, we have reworked the rule to 'AvoidDefaultValueForMandatoryParameter '.

Can you check the the version you are using?
https://www.powershellgallery.com/packages/PSScriptAnalyzer/1.4.0

-Raghu

@joeyaiello
Copy link
Contributor

Also not seeing it on 1.4.0:

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.

@raghushantha
Copy link
Member

Please activate if you can repro this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants