Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

String Comparisons #32

Open
FMCalisto opened this issue Mar 1, 2016 · 0 comments
Open

String Comparisons #32

FMCalisto opened this issue Mar 1, 2016 · 0 comments

Comments

@FMCalisto
Copy link

Position literals first in String comparisons.

42
43     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
44         context = callbackContext;
45         if(action.equals("getPermission"))
46         {
47             if(hasPermisssion())

Why is this an issue?

Position literals first in comparisons, if the second argument is null then NullPointerExceptions can be avoided, they will just return false.

Ex:

class Foo {
  boolean bar(String x) { //it will throw a nullPointer if x is null
    return x.equals("2");
  }

  boolean bar2(String x) { //this way it will return false even if x is null
    return "2".equals(x);
  }
}

Source

Related code pattern

Enforces literals first in comparisons

Enforces literals to be positioned first in comparisons.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant