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

Improved the script #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Improved the script #73

wants to merge 1 commit into from

Conversation

HotCakeX
Copy link

@HotCakeX HotCakeX commented May 23, 2024

  • Changed the function name to PowerShell valid function name syntax.
  • Added proper error types to the throw statements. It's best practice and also allows for catching specific errors later on when needed.
  • Changed some variable types to full types instead of using type accelerators for better clarity.
  • Removed all the unnecessary trailing whitespaces.
    • Used \s+$ in VS Code's search as regex.
  • Changed double quotes to single quotes wherever possible for improved security, speed and to prevent string interpolation.

Changed the function name to PowerShell valid function name syntax

Added proper error types to the throw statements

Changed some variable types to full types instead of using type accelerators
@HotCakeX
Copy link
Author

@microsoft-github-policy-service agree

scripts/sudo.ps1 Show resolved Hide resolved
$bytes = [System.Text.Encoding]::Unicode.GetBytes($cmdLine)
[Convert]::ToBase64String($bytes)
}
function ConvertTo-Base64EncodedString([System.String]$cmdLine) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use [string], the builtin type?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it eliminates confusion when we explicitly type out the fully qualified type name instead of using type accelerator/alias.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the other argument (in fact it's a fantastic one!), but this one seems to be just an opinion. (It's easily countered with "How does it eliminate confusion?", etc., for instance.) I'm not really meaning to press this further, however.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confusion elimination is by being explicit instead of implicit

like in this example

$CSharpCode = @'
namespace MyNamespace
{
    public class String
    {
        public string Value { get; set; }
    }
}
'@
Add-Type -TypeDefinition $CSharpCode
Set-Alias -Name String -Value MyNamespace.String
$myStringObject = [String]::new()
$myStringObject.Value = 'Hello'
$myStringObject.Value

That doesn't work unless i disambiguate it by using this

$myStringObject = [MyNamespace.String]::new()

since both System and my custom namespace have the same String class.

@HotCakeX HotCakeX requested a review from lhecker June 4, 2024 20:59
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

Successfully merging this pull request may close these issues.

2 participants