8 ms·
I've seen this in code I've reviewed before, where people will check for `== "empty"` instead of just using types like `null` which were designed for this (in j
by gtf21 10y ago
I've seen this in code I've reviewed before, where people will check for `== "empty"` instead of just using types like `null` which were designed for this (in javascript).
- kqr 10y agoA practise that may come from some shell scripting languages where `[ $x == "" ]` is an invalid command because empty strings aren't strings.
- _kst_ 10y agoThat may be true in older shells, but in modern shells [ $x == "" ] is valid, and "" is a single token whose value is the empty string. But I've seen old code that does things like if [ x$x == "x" ] ; then ... apparently because it was necessary at one time.
- kqr 10y agoIt depends on what you mean by "older" shells. More modern shells tend to support this properly, but the baseline for most shell scripts is /bin/sh which is not guaranteed to be modern – it is often a new release of Sh.
- astrodust 10y agoAnyone who's done even a trivial amount of programming in the shell knows to do: [ "$x" == "" ]
- kqr 10y agoThat does not help, as far as POSIX is concerned. $ [ "$x" == "" ] sh: 2: [: unexpected operator