[webkit-dev] !!Tests for equality comparison

Geoffrey Garen ggaren at apple.com
Fri Apr 28 08:10:43 PDT 2017


> but == should be used for testing things where 0 is just another number, like indexes:
> 
> if (index == 0)
>
The index case is especially annoying because we use -1 to indicate notFound, so !index means “the first valid index” rather than “no index”. It’s pretty odd to think of false as the first valid array index. I wouldn’t want someone to write “x = array[false]”.

Also:

If (point.x == 0)
    …

If (gps.latitude == 0)
   …

etc.

These are the cases where ! really grinds my gears.

Perhaps we could relax the rule to say:

Tests for true / false and null / non-null should use if (x) / if (!x) instead of equality comparisons:

    If (ptr)
        ptr->func()

Tests for numeric values where 0 indicates falsiness or emptiness should also use if (x) / if (!x).

    If (!collection.size())
        return;

Other comparisons to numeric values should use equality comparisons:

    if (index == 0)
        ...

Geoff


More information about the webkit-dev mailing list