I don’t think PowerShell syntax makes sense at all. It certainly doesn’t feel “more modern” to me.
param (
[Parameter(Mandatory = $true)]
[string]$FilePath
)
param()
sorta makes sense, but why are there two single element arrays inside it? Could each array have different contents? Presumably the first is a list of params and the second a list of types + variable names. Why not pair the parameter definitions with their types?
Are there other properties inside Parameter that you could set?
Why $true
instead of true
or True
? Doesn’t $
denote a variable name?
TitleCase keywords are annoying. Since you declared FilePath, could it at least be lowercase? Or is $FilePath
special in some way that makes tab completion work better, and specifically look for filenames?
Are you missing a comma between param’s parameters, or do PowerShell functions not use commas?
if (-not (Test-Path -Path $FilePath)) {
Write-Error "File '$FilePath' does not exist."
exit 1
}
Why -not
? I’d expect to see !
or even Not
, which would be consistent with the established language pattern. not
would make sense if lowercase were used for logic operators, like true
, but even it uses a $ prefix…
Title-Dash-Case is even more obnoxious than TitleCase.
Why is this version of a function (Test-Path) using -Path to set the path property, but the version in the previous block, Parameter, just set it inside the parentheses? For consistency, shouldn’t this also be Test-Path(Path = $FilePath)
? Or alternatively, above, why isn’t it Parameter -Mandatory True
? And why is it single dashes rather than the double dashes for named args that’s a common cli convention?
try {
$firstLine = Get-Content -Path $FilePath -TotalCount 1
}
At least the try catch syntax makes sense to me.
Why is this variable in camelCase when the previous one was in TitleCase?
Why is the function in Title-Dash-Case but one of its properties (TotalCount) is in TitleCase?
Why is the property name “TotalCount” when something like “LineCount” would make more sense? Or even to put it in a line-based mode with one property and then to set that value in another, e.g., $firstLine = Get-Content(Path = $filePath, Mode = Line, Count = 1)
? If Get-Content is always line-based, why isn’t it called Read-Lines?
catch {
Write-Error "Could not read from file '$FilePath'."
exit 1
}
Is Write-Error a dedicated function to log an error? Why isn’t it just Write
with an Error property? Does it log a stack trace?
Why is exit
lower case? Is it because lower case is used for control flow?
Write-Output "First line: $firstLine"
$lineLength = $firstLine.Length
Write-Output "Length of first line: $lineLength"
Feels like that should be String-Length $line
tbh
Worse than anything above, as far as I can tell, the official documentation doesn’t even explain the syntax or the design choices behind their weird choices - or maybe it does and I just can’t find it thanks to using a Microsoft site on mobile.
The bash script makes sense to me, and while there’s a ton I hate about bash, at least it doesn’t use Title-Dash-Case. Its syntax may use symbols instead of words, but most of those symbols are part of a well-documented standard (or for bash specific ones, they extend the standard in an intuitive, still documented way). Needing to read the documentation doesn’t make it arcane; not being well documented in the first place, on the other hand, does. (To be clear, I’m not saying that PowerShell isn’t well documented, but the way that Microsoft documents things does not make sense to me, so I can’t say that it’s well documented, either.)
I’ll put up with a semicolon or line break being needed between if
and then
if it means I don’t ever have to Do-This -In $powerShell
If I want to do something complicated, I’m probably going to use some other language instead of Bash, and as far as I can tell, I’d need to do the same with PowerShell, even if I were comfortable using it.