Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Even more unrelated, but equally pedantic, I've always thought it's weird when people write "null != val" instead of "val != null" for the same reason.

When said out loud, "null is not val" just feels wrong.



Comes from the null==val idiom, and that comes from avoiding the popular "if (val=null)" bug in C/C++.

Sure, we could just admit that assignments in conditions are a permanently stupid idea. Instead, an entire industry backwards the conditions wrote.


I find initializing condtions like `if(Type variable = ...)` to be very nice in C++ to avoid excessive nesting while still keeping the variable scoped to the block. Of course, I also enable -Wparentheses for things like `if(val=null)`, which you get e.g. when using -Wall with both GCC and Clang.


Isn't that just Yoda notation? https://en.wikipedia.org/wiki/Yoda_conditions


In PowerShell you'll get a linter warning because things like (look at the -):

if (@() -eq $null) { 'true' }else { 'false' } # false

if (@() -ne $null) { 'true' }else { 'false' } # false


And there also exist expressions for the left-hand side that make both expressions truthy, like [0]:

  $v = $null, $null, 1

  ($v -eq $null) -and ($v -ne $null) # True
In both these cases, this is caused by the language having built-in binary operators for when the left-hand side expression is a collection type that perform the operation elementwise and return an array.

Interestingly, it seems like PowerShell's operator overload resolution in general depends entirely on the type of the LHS. I say 'seems' because I couldn't find any sort of language specification when I looked into it a while ago like what C# and VB.NET have, and testing seemed to confirm that this was the case. Now, searching the PowerShell Core source, it seems from [1] that this is indeed the implementation.

This contrasts with C# [2] and VB.NET [3], where binary operator overload resolution is treated as if the candidate operator implementations were a two-parameter method group, making the resolution process 'commutative' (though not always commutative in practice as the operators themselves can still have different LHS and RHS types {Edit: example from the CLR [4]: +(Point, Size) but not +(Size, Point)}).

[0] https://blog.iisreset.me/schrodingers-argumentlist/

[1] https://github.com/PowerShell/PowerShell/blob/master/src/Sys...

[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...

[3] https://docs.microsoft.com/en-us/dotnet/visual-basic/referen...

[4] https://source.dot.net/#System.Drawing.Primitives/System/Dra...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: