7

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
  • 2
    Preemptive note: I don't think this is a duplicate of any of the obviousdupeoptions that came up when searching, despite this being a recurring topic on this site. This question is asking about initialization, not declaration.
    – user22815
    CommentedAug 16, 2016 at 14:18
  • 1
    I know the question is not about the processor architecture values but it is a bit distracting that you compare to the non-existent AMT64 instead of AMD64 and that you are ignoring the other possible values. msdn.microsoft.com/en-us/library/aa384274.aspxCommentedApr 29, 2017 at 7:28
  • @MartinMaat Good point; corrected (sorry it took so long, only just realised I'd thumbed you up but not taken any corrective action!)CommentedJul 6, 2017 at 9:37
  • 1
    I may add: [string]$path = Get-ProcArchitectureDependentProgFilesPath ... simple helper function allow to avoid the very question and make the code more readable too. (I'll note that I find the accepted answer acceptable too, if the if condition is easier.)
    – Martin Ba
    CommentedJul 6, 2017 at 9:42

2 Answers 2

12

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 } 
    1

    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.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.