GitHub Copilot’s Code Brushes
githubnext.com
githubnext.com
(a == null || b == null) ? NaN :
a < b ? -1 :
a > b ? 1 :
a >= b ? 0 :
NaN;Maybe it’s a stylistic choice, but there’s nothing wrong with a sequence of “else ifs” as long as you don’t go deeper (like in the example)
That said, I do wish more languages had copied Haskell's guard syntax for this kind of chain... For reference, that looks like:
cmp x y | (x == Nothing || y == Nothing) = NaN -- (assuming x and y are Maybe, but this would typically be non-idiomatic)
| x < y = -1
| x > y = 1
| x >= y = 0 -- inert bug copied from original, "should" be == instead
| otherwise = NaN return match(true) {
$a == null && $b == null => null,
$a < $b => -1,
$a > $b => 1,
$a == $b => 0,
default => null;
}
I’m surprised this feature isn’t prevalent in modern languages. It makes the code so concise and easy to understand, it’s marvellous.