Tuesday 23 September 2014

OK, there's a precedent contradicting my PHP "Null" gripe from the other day

G'day:
What would I do without my guardian angel Sean? ;-)

Over the weekend I wrote an article "PHP: a fractal of [etc]... yeah, I'm now getting where you're coming from", which I - in passing - derided PHP's choice of null being a boolean false.



In a comment, Sean pointed out this is a well-established conceit. He specifically mentions low-level languages like C and C++, but once I engaged my brain, it also occurred to me that the same thing exists quite happily on high-level languages that I have at least a passing familiarity with, such as JavaScript and Ruby and (having checked, because I wasn't sure) Groovy and Python. My Python skills is very much "passing familiarity", and not even "G'day World" level.

So I did better investigation as to what other languages have to say.

JavaScript


if (!null) console.log("null is false")
if (null) console.log("null is true")

console.log("0 == false: " + (0 == false))
console.log("1 == true: " + (1 == true))
console.log("null == false: " + (null == false))
console.log("null == true: " + (null == true))
console.log("null == 0: " + (null == 0))
console.log("null == 1: " + (null == 1))

Output:


null is false
0 == false: true
1 == true: true
null == false: false
null == true: false
null == 0: false
null == 1: false


Ruby


puts "nil is false" if not nil
puts "nil is true" if nil

puts "0 == false: " + (0 == false).to_s
puts "1 == true: " + (1 == true).to_s
puts "nil == false: " + (nil == false).to_s
puts "nil == true: " + (nil == true).to_s
puts "nil == 0: " + (nil == 0).to_s
puts "nil == 1: " + (nil == 1).to_s

Output:

nil is false
0 == false: false
1 == true: false
nil == false: false
nil == true: false
nil == 0: false
nil == 1: false


Groovy


if (!null) println "null is false"
if (null) println "null is true"

println("0 == false: " + (0 == false))
println("1 == true: " + (1 == true))
println("null == false: " + (null == false))
println("null == true: " + (null == true))
println("null == 0: " + (null == 0))
println("null == 1: " + (null == 1))

Output:

null is false
0 == false: false
1 == true: false
null == false: false
null == true: false
null == 0: false
null == 1: false


Python


if not None:
    print "None is false"
if None:
    print "None is true"

print "0 == False: " + str(0 == False)
print "1 == True: " + str(1 == True)
print "None == False: " + str(None == False)
print "None == True: " + str(None == True)
print "None == 0: " + str(None == 0)
print "None == 1: " + str(None == 1)

Output:

None is false
0 == False: True
1 == True: True
None == False: False
None == True: False
None == 0: False
None == 1: False


So across the board here null (etc) is a falsey value. But, equally, across the board null is not equal to 0 or false.

So the PHP people can be spared the sack/snake/ape/dog/cock treatment for making null false. But perhaps still deserve it for making null actually equal to false.

Oh... the most interesting thing about this article? All that code above was written and tested using the various language's REPLs / consoles (Ruby & Python via repl.it, Groovy via Groovy++ Web Console, and JavaScript just via Chrome's console). Not saving a file and running it like one would have to do with CFML. All the more demonstration Railo & ColdFusion ought to have CLI/REPLs!

--
Adam