Search code examples
salesforceapex-code

Is the use of <> as a not-equal operator documented somewhere?


looking at existing APEX code I've seen quite a few places where not equal is tested using "<>" instead of "!=". I can't find an official documentation where this behaviour is described. Does anyone know if this has any potential side effects? Should that be changed to use != wherever possible?

I'd be thankful for any hints / references.


Solution

  • I personally always use != for clarity, unless I'm looking for a number that is either greater than or less than another number. Since <> implies greater than or less than as opposed to not equals, it doesn't read well with strings - unless alpha-sorting/ranking is what's desired.

    For example:

    String myVar = 'apple';
    system.assert(myVar <> 'orange'); // Pass
    system.assert(myVar < 'orange'); // Pass
    system.assert(myVar > 'orange'); // Fail
    

    So in the spirit of creating self-describing code, != is my choice for all "not-equal" scenarios that don't involve ranking or ordering of any kind. Otherwise <> will work the same, but with incompatible types you can't perform a < OR a >. In other words:

    system.assert(myVar <> null); // Pass
    system.assert(myVar > null); // Error: Comparison arguments must be compatible types: String, NULL)
    

    So whatever more accurately describes the comparison is my 2-cents.