Is there a preferred practice around initialising variables which are first defined within a branch of code, or is this purely subjective / personal preference?
i.e. there are multiple ways this can be done; the most obvious of which are below:
#option 1
[string]$path = ''
if ($ENV:PROCESSOR_ARCHITECTURE -like '*64') {
$path = ${env:ProgramFiles(x86)}
} else {
$path = $env:ProgramFiles
}
Get-ChildItem $path
#option 2
[string]$path = $env:ProgramFiles
if ($ENV:PROCESSOR_ARCHITECTURE -like '*64') {
$path = ${env:ProgramFiles(x86)}
}
Get-ChildItem $path
#option 3
if ($ENV:PROCESSOR_ARCHITECTURE -like '*64') {
[string]$path = ${env:ProgramFiles(x86)}
} else {
[string]$path = $env:ProgramFiles
}
Get-ChildItem $path
In most programming languages you’d go with option 1
; i.e. have the variable declared and defaulted at the top, then assign it in a later branch. You may select option 2
instead if the cost of fetching the value is insignificant (e.g. in the above we’re just pulling back an environment variable, so it’s a low cost operation; but if we were pulling back a value from an external resource / using an expensive query or operation we may prefer option 1
to option 2
). Equally some people may prefer option 1 to option 2 because it better illustrates the conditions under which each value should be used.
Option 3
is only permissible in languages which don’t require variable declaration; so is allowed in PowerShell / is the lowest cost solution (i.e. minimum number of assignments).
NB: I realize that the [string]
qualifier is superfluous / that PowerShell will automatically determine this; this is just something I include to help illustrate the idea of variable declaration (i.e. first assignment).
I feel an aversion to option 3, probably because of my background in other languages; so don’t know if others would have the same aversion, or if most people just think “this is the right thing to do in this language”.
Are there any guidelines on this sort of thing (i.e. when there are no considerable performance considerations or additional factors to better inform the decision)?
4
This is very much opinionated, and every professional programmer should have no problems to read and understand each of the variants. However, here is a version which avoids the superfluous repetition of the $path
variable, thus being more DRY than any of your 3 alternatives:
[string]$path = if ($ENV:PROCESSOR_ARCHITECTURE -like '*64') {
${env:ProgramFiles(x86)
} else {
$env:ProgramFiles
}
To make this a little bit more readable, I would probably introduce an explaining variable for the expression $ENV:PROCESSOR_ARCHITECTURE -like '*64'
(but this is something I would do for any of the four variants, and so orthogonal to your question). i.e.
[bool]$isX64Processor = $ENV:PROCESSOR_ARCHITECTURE -like '*64'
[string]$path = if ($isX64Processor) {
${env:ProgramFiles(x86)
} else {
$env:ProgramFiles
}
I do not think there is a universally preferrable way. I would use option 1 if the determination of the ultimate value were a complex process. If one value would be the most likely and another an exceptional case, I would go with option 2.