Skip to content
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

PSProvideDefaultParameterValue is always wrong. #362

Closed
Jaykul opened this issue Nov 17, 2015 · 6 comments
Closed

PSProvideDefaultParameterValue is always wrong. #362

Jaykul opened this issue Nov 17, 2015 · 6 comments

Comments

@Jaykul
Copy link

Jaykul commented Nov 17, 2015

This rule should not exist at all

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, and nothing to be gained in setting them yourself -- unless you actually need them to default to something other than $null.

If anything, this should be warned against for performance reasons. Initializing a variable to it's default value is just extra work that accomplishes nothing. It might even make your script (infinitesimally) slower, whilst changing nothing.

@DWSR
Copy link

DWSR commented Nov 17, 2015

Signed.

@raghushantha
Copy link
Member

Agree. Will change to rule to warn users if they provide a default value for a Mandatory parameter.

@KirkMunro
Copy link
Contributor

Be sure to check all parameter sets that parameter belongs to when creating
this rule. Some parameters are mandatory in some parameter sets yet remain
optional in others. In that scenario, it shouldn't warn if a default value
is provided.

The rule should only warn users when a parameter that is mandatory in every
parameter set where it is included is assigned a default value.

*Kirk Munro *
Poshoholic, Microsoft MVP
Blog http://poshoholic.com/ | Twitter http://twitter.com/poshoholic |
LinkedIn http://ca.linkedin.com/in/kirkmunro | GitHub
http://github.com/KirkMunro | Facebook
http://www.facebook.com/#%21/kirk.munro

Need a PowerShell SME for a project at work? Contact me
http://poshoholic.com/contact-me/!

On Wed, Nov 18, 2015 at 3:53 PM, Raghu Shantha [MSFT] <
notifications@github.com> wrote:

Agree. Will change to rule to warn users if they provide a default value
for a Mandatory parameter.


Reply to this email directly or view it on GitHub
#362 (comment)
.

@Jaykul Jaykul changed the title PSProvideDefaultParameterValue is wrong. PSProvideDefaultParameterValue is always wrong. Nov 21, 2015
@Jaykul
Copy link
Author

Jaykul commented Nov 21, 2015

Hang on, you're confusing me with #360

My point is that PSProvideDefaultParameterValue is always wrong.
It's not just wrong when the parameter is mandatory.

There's nothing wrong with providing default values if the parameter is (sometimes) not mandatory, but any rule that makes default values required is wrong.

@KirkMunro
Copy link
Contributor

Raghu said they would change this rule such that it only warns when default parameter values are assigned to mandatory parameters. I read that as saying that is all this rule would do, and suggested they only do that for parameters that are mandatory in every parameter set in which they are used. Nothing else. No default value checking for optional parameters. Is that not what you meant Raghu?

@raghushantha
Copy link
Member

Correct. Here is the PR with the change:

#382

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

No branches or pull requests

4 participants