DEV Community

AnonymousITGuy FromUS
AnonymousITGuy FromUS

Posted on

Check out this Cool PowerShell Trick, I call it "Hygenic Bracing"

It's been a good while since I first started out learning PowerShell. Before then, I had learned & earned some chops, programming many years with statically-typed, compiled languages.

PowerShell was the sanity I needed to dive into and enjoy dynamic languages (enjoy much more than PHP, that is 😝)

So what is this "Hygienic Bracing" trick? -- let's get into it

Background

Back when first learning PowerShell, I remember learning about variable scoping and, at one point, thinking, "It would be really nice to be able to use those braces on local variables to explicitly represent my intention with variable scope!" -- Basically, I wanted to declare a variable inside an extra brace, use it to store some temporary result, and then make the variable go away with a closing brace -- I liked the idea of representing a variable's lifetime directly -- to the machine and to the programmer -- in a machine-processable way (and cut down on namespace clutter). Such a technique would make it easier when reading the code, to see where a variable's lifetime began and ended, especially for super-long scripts or procedures that have to make use of many variables. By limiting and "sub-scoping" variables, we would effectively reduce the number of variables the reader has to keep track of in their head!

Not long ago, it finally clicked -- PowerShell does let you do this, in-part! -- but it took a couple of years for it to dawn on me.

Let's give a working example.

Example

"Before"

# This is a super long script or routine

$X = 3 + 5*2

$Y = $X + 1

$X = 4 + $Y

# Many lines later...

$Z = $X * 2
Enter fullscreen mode Exit fullscreen mode

"After"

# This is a super long script or routine

$Global:X = 3 + 5*2

& {
    $Y = $Global:X + 1  #Obtain some result in $Y

    $Global:X = 4 + $Y   #Use $Y

}  <# And VOILA -- $We're done with $Y -- $Y goes away and
keeps the namespace clean!  The person reading the code can
be like "Oh, no $Y doesn't exist anymore!  I can forget about
$Y and move on!"
#>

# Many lines later...

$Z = $Global:X * 2
Enter fullscreen mode Exit fullscreen mode

Unfortunately, using dot-source-executed scriptblocks will not do anything for hygiene, but at least we have something going here with ampersands!

We can, of course, clean up our example further 😊

"After, v2.0"

# This is a super long script or routine

#Outer $X
$X = & {

    #Inner $X
    $X = 3 + 5*2

    #Uses Inner $X
    $Y = $X + 1  #Obtain some result in $Y

    (4 + $Y)   #Use $Y
}

# Many lines later...

#Uses Outer $X
$Z = $X * 2
Enter fullscreen mode Exit fullscreen mode

This is well and great, but my main point is how you can use & { } for improved namespace hygiene.

Application a.k.a. When to Use

Like I said, this comes in REAL handy when you have long scripts or large routines that go on for dozens of lines or more. Sometimes when your procedure does things in steps or builds+uses state in stages, this technique finds itself to be a helpful use-case!

+1 PowerShell Tricks

Top comments (0)