Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should $null be on the left side of the equality comparison? (-eq with arrays)

Tags:

powershell

Discussion with a colleague, should $null be on the left or right side of the check? Any examples of why this is important?

$abc = $null
$null -eq $abc
True
$abc -eq $null
True

All ok

$abc = 6,7,$null,8,9
$null -eq $abc
False
$abc -eq $null
*No output*

Can someone explain what is happening when an array is compared?

like image 907
user9924807 Avatar asked Oct 03 '19 11:10

user9924807


People also ask

What is $null in PowerShell?

$null is an automatic variable in PowerShell used to represent NULL. You can assign it to variables, use it in comparisons and use it as a place holder for NULL in a collection. PowerShell treats $null as an object with a value of NULL. This is different than what you may expect if you come from another language.

What is EQ in PowerShell?

Eq is a comparison operator that will test if one numeric or string expression is equal to another, Syntax expression -eq expression. Eq is a case-insensitive match and will ignore wildcards.

What is LT in PowerShell?

PowerShell's Less Than Comparison Operator -lt Just as -eq has an opposite in -ne, so -gt (greater than) has a mirror image in -lt (less than). In other scripting languages these would be represented by > and < symbols, however PowerShell uses the dash then an abbreviation of the comparison operator, thus -lt.


2 Answers

When performing comparison operations, PowerShell will evaluate the first operand the operation to determine if it is a collection or a scalar value. If it is a scalar, it will compare the operands to each other as whole objects.

2 -eq 1,2,$null,3
False

$null -eq 1,2,$null,3
False

4 -eq 1,2,$null,3
False

If it is a collection, it will iterate through the collection comparing each element to the second operand and return the value of the second operand if it finds a match.

1,2,$null,3 -eq 2
2

1,2,$null,3 -eq $null
<nothing appears, indicating returned value is $null>

1,2,3 -eq 4
<nothing appears, indicating returned value is $null>

The key difference between the second and third examples. Unless you know absolutely whether the second operand is always or never $null, you can't trust the output of the comparison operation.

On a side note, if both operands are collections, -eq returns $null in every case, because you can't use -eq to compare collections.

So to answer the question, it is generally good practice to put $null on the left side because when using $null in a comparison operation, it is assumed that you want to compare scalar values. If that assumption is wrong, then $null may be better placed on the opposite side, but it's not likely.

Contrarily, I always put $null on the right side because I write my code under the aforementioned assumption - that I am always comparing scalar values when $null is explicitly mentioned in the comparison operation. That way, when I get a non-boolean value returned, I know that I have not accessed my collection object properly in the equation. I have gotten caught in the trap, however, so my coding practices may be better served by changing my ways.

In conclusion, like any coding practice, it's a matter of opinion. Were I to teach PowerShell, I would teach this practice. However, I find it unlikely I will change my seditious ways.

like image 101
Slogmeister Extraordinaire Avatar answered Oct 01 '22 18:10

Slogmeister Extraordinaire


-eq is different with arrays on the left. A lot of operators are.

1,2,3 -eq 2

Output:

2

So instead of 2, in your case it's returning $null, because $null is in the array.

like image 44
js2010 Avatar answered Oct 01 '22 20:10

js2010