# The curious case of $null should be on the left side of equality comparisons PSScriptAnalyzer

If you’re using VSCode with Powershell extension, you probably got used to PSScriptAnalyzer giving you all kind of tips on optimizing things. It makes your code better. Chris Bergmeister does a great job working on it. One of the tips PSScriptAnalyzer gives you when you use $null on the right side of the comparison.

blog

24 Apr 2019 en

![The curious case of $null should be on the left side of equality comparisons PSScriptAnalyzer](/wp-content/uploads/2019/04/img_5cc0645e15f23.png)

[View images (4)](#pf-content-image-1-0)

If you're using **VSCode with Powershell** extension, you probably got used to **PSScriptAnalyzer** giving you all kind of tips on optimizing things. It makes your code better. [Chris Bergmeister](https://twitter.com/CBergmeister) does a great job working on it. One of the tips **PSScriptAnalyzer** gives you when you use **$null** on the right side of the comparison.

```powershell
$Process = Get-Process
if ($Process -eq $null) {
    #DoSomething
}
```

For a long time, this was my standard code until I started using VSCode and got the tip from PSScriptAnalyzer to use it differently. Like that:

```powershell
$Process = Get-Process
if ($null -eq $Process) {
    #DoSomething
}
```

From the english perspective, this should be the same, but well it really isn't.

### 💡 Explanation for $null comparison

When I asked Chris about it, he explained the reason with following code

```powershell
if (@() -eq $null) { 'true' }else { 'false' }
if (@() -ne $null) { 'true' }else { 'false' }
```

[!\[PSScriptAnalyzer documentation excerpt describing the PossibleIncorrectComparisonWithNull rule\](/wp-content/uploads/2019/04/img_5cc05a6cb17eb.png)](/wp-content/uploads/2019/04/img_5cc05a6cb17eb.png)

Fair enough right? Full rule [documentation is available here](https://github.com/PowerShell/PSScriptAnalyzer/blob/development/RuleDocumentation/PossibleIncorrectComparisonWithNull.md). As documentation describe it there are reasons why this should occur:

- 💡 `$null` is a scalar. When the input (left side) to an operator is a scalar value, comparison operators return a Boolean value. When the input is a collection of values, the comparison operators return any matching values, or an empty array if there are no matches in the collection. The only way to reliably check if a value is `$null` is to place `$null` on the left side of the operator so that a scalar comparison is performed.
- 💡 **PowerShell** will perform type casting left to right, resulting in incorrect comparisons when `$null` is cast to other scalar types.

Accepting this truth made me start using** $null** religiously on the left side of the comparison.

### 💡 When it is not a good idea to have $null on the left side

I could end the article here, saying “**and they lived ever after**” if I wouldn't hit a problem today because of that rule. You see I started to trust that rule without even thinking about it. The moment **VSCode** would tell me to fix it, I would go and fix it. So any time I would see this rule popup I would fix it and go my way:

[!\[VS Code warning recommending that null be placed on the left side of the comparison\](/wp-content/uploads/2019/04/img_5cc05cd14e030.png)](/wp-content/uploads/2019/04/img_5cc05cd14e030.png)

The problem is I wasn't really testing after I changed this, every time I did it. I even started to do it for some old code and simply fixed it for peace of mind. Until I noticed that one of my scripts using RunSpaces stopped working today. Let's take a look at the code that is simplified version of it:

```powershell
$Object1 = @{
    Name   = 'Test1'
    Status = 1
}
$Object2 = @{
    Name   = 'Tests2'
    Status = $null
}

$Array = @($Object1, $Object2)

$Test1 = While ($Array.Status -ne $null) {
    $true
    foreach ($A in $Array) {
        $A.Status = $null
    }
}
```

[!\[PowerShell example with two while loops comparing object status values against null\](/wp-content/uploads/2019/04/img_5cc05d95c1799.png)](/wp-content/uploads/2019/04/img_5cc05d95c1799.png)

This code merely is supposed to set **$null** value in Status property of two objects: **$Object1** and **$Object2**. While of course you usually wouldn't use **While **for such case, but in my code I did. When you run the code above you will get **$true** as an output of **$Test1** variable. Everything according to plan, but with one problem – **$Array.Status comparison** complaining that I'm using **$null** on the wrong side. Let's fix it. For comparison purposes, I left both loops in the code.

```powershell
$Object1 = @{
    Name   = 'Test1'
    Status = 1
}
$Object2 = @{
    Name   = 'Tests2'
    Status = $null
}

$Array = @($Object1, $Object2)

$Test1 = While ($Array.Status -ne $null) {
    $true
    foreach ($A in $Array) {
        $A.Status = $null
    }
}

$Test1

$Test2 = While ($null -ne $Array.Status) {
    $true
    foreach ($A in $Array) {
        $A.Status = $null
    }
}

$Test2
```

Let me know when it's done? Well, actually, it will never finish. It seems that changing order around makes it an infinite loop. **Totally not what I expected**. I trusted PSScriptAnalyzer a bit too much on this front.

[!\[PowerShell run demonstrating the infinite loop caused by moving null to the left side of the comparison\](/wp-content/uploads/2019/04/img_5cc0645e15f23.png)](/wp-content/uploads/2019/04/img_5cc0645e15f23.png)

### 💡 Moral

Moral of this story? **Test your code** after making changes, even those that seem like a minor change. I'll, of course, ask [Chris](https://twitter.com/CBergmeister) to clarify this for me, and maybe he can add some workaround for **PSScriptAnalyzer** to not show this suggestion if it's used on **Array**. Make sure to follow him on **Twitter**. He makes it better for us, users of **VSCode and PowerShell Extension.**

## Related Projects

Projects that align with this post's tags, categories, and content.

[All projects](/projects/)

Matching projects...

[Back to Blog](/blog)

## Related posts

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)
- [ImagePlayground: image automation that stays in PowerShell](/blog/imageplayground-powershell-image-automation/)

<!-- PowerForge markdown artifact sha256:293B53B0ABC03978E9F752E413C8AC395F5BD476784F31B242B58DACAF842661 -->
