Skip to content

Switch parameter standardization#26150

Open
logiclrd wants to merge 2 commits intoPowerShell:masterfrom
logiclrd:switch-parameter-standardization
Open

Switch parameter standardization#26150
logiclrd wants to merge 2 commits intoPowerShell:masterfrom
logiclrd:switch-parameter-standardization

Conversation

@logiclrd
Copy link
Copy Markdown
Contributor

@logiclrd logiclrd commented Oct 5, 2025

PR Summary

Replace SwitchParameter.IsPresent and SwitchParameter.ToBool() with SwitchParameter.IsSpecified and standardize the codebase to it.

PR Context

In discussion on #26140, it has come up that there are in fact three ways to do the identical conversion of a SwitchParameter to a bool value that indicates whether it is specified in the invocation:

  • A SwitchParameter can be implicitly converted to bool. This is documented as the recommended way to process switches from PowerShell code: The SwitchParameter type implicitly converts to Boolean
  • A property .IsPresent, which is used in 135 places across the current codebase.
  • A method .ToBool(), which is used in 11 places across the current codebase.

The C# codebase does not use the implicit conversion anywhere.

The .ToBool() method is descriptive only of the what but not the why; where it is used, it's not describing the meaning of the bool value.

The .IsPresent property is poorly-named, because it suggests that it is return a value indicating whether the switch is, in fact, present when a switch can be present but explicitly $false, in which case .IsPresent == false:

New-Guid -Empty
New-Guid -Empty:$false

It has been deemed a bug that these two commands return identical output, and PR #26140 fixes this. There is a similar issue with Get-Uptime -Since, fixed by PR #26141.

This PR has two aims:

  • Replace .IsPresent and .ToBool() with a more semantically-accurate property .IsSpecified. (The old ones cannot be deleted, per se, since they're part of the public API, but they can be marked [Obsolete].)
  • Standardize the codebase on this one method of determining the semantic value of a SwitchParameter.

I searched for existing documentation of .IsPresent and .ToBool(), and as far as I can tell, they are only explicitly documented in the XmlDoc for their members in the source code, and .IsSpecified is presented here with equivalent XmlDoc documentation. The implicit convention for the use of the new property .IsSpecified is internal to the PowerShell codebase. I have thus checked Not Applicable for the documentation section of the PR checklist. If this is in error, let me know and I'll amend as appropriate. 🙂

Closes: #26149

PR Checklist

Marked SwitchParameter.IsPresent and SwitchParameter.ToBool() as [Obsolete] and [Browsable(false)]. Added a property IsPresent for the same purpose that is more semantically-accurate.
@xtqqczze
Copy link
Copy Markdown
Contributor

xtqqczze commented Oct 5, 2025

The changes to public APIs require an RFC. I suggest we change the codebase to the implicit conversion internally: https://github.com/xtqqczze/PowerShell-PowerShell/tree/rm-SwitchParameterIsPresent

@logiclrd
Copy link
Copy Markdown
Contributor Author

logiclrd commented Oct 5, 2025

Shucks, I thought I might get a Hacktoberfest contribution out of this 😛

@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Oct 6, 2025

This new parameter doesn't look any more convenient/clear than the old one. Considering that SwitchParameter has been around for many years, I would prefer to simply improve its description in the documentation. Perhaps ScriptAnalyzer can also help too.

@jborean93
Copy link
Copy Markdown
Collaborator

I would agree, good to standardise the internal codebase onto either IsPresent or ToBool but adding a 3rd option seems like just another way to confuse people and make things even more fragmented.

@logiclrd
Copy link
Copy Markdown
Contributor Author

logiclrd commented Oct 6, 2025

What about marking .IsPresent and .ToBool() with [Obsolete] in conjunction with documenting that the implicit conversion should be preferred everywhere, no other changes? Existing code keeps working, and a warning is generated so awareness isn't limited to people who happen to actually read the documentation. 😛

@logiclrd
Copy link
Copy Markdown
Contributor Author

logiclrd commented Oct 6, 2025

The thought process I'm basing that on is the fact that in the context of PowerShell code, the implicit conversion is already documented in a way that implies it is the preferred way to interact with SwitchParameters.

@jborean93
Copy link
Copy Markdown
Collaborator

I think relying on the implicit conversion is fine but would personally rather just 1 official method or property to get the bool value. While the obsolete attribute might help from a binary module implementation to try and unify things on the PowerShell side would require a rule in PSScriptAnalyzer and even then that can only do so much due to the dynamic nature of PowerShell.

@xtqqczze
Copy link
Copy Markdown
Contributor

xtqqczze commented Oct 7, 2025

While the obsolete attribute might help from a binary module implementation to try and unify things on the PowerShell side would require a rule in PSScriptAnalyzer and even then that can only do so much due to the dynamic nature of PowerShell.

There is discussion about the right way to implement deprecation in #11674.

@xtqqczze
Copy link
Copy Markdown
Contributor

xtqqczze commented Oct 7, 2025

I would agree, good to standardise the internal codebase onto either IsPresent or ToBool but adding a 3rd option seems like just another way to confuse people and make things even more fragmented.

I have a PR to use the implicit conversion internally: #26152

@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SwitchParameter.IsPresent documentation does not match actual behavior when parameter is explicitly set to false

4 participants