You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@accumulo.apache.org by David Medinets <da...@gmail.com> on 2012/09/12 18:29:08 UTC

Non Short Circuit Logic

The if statement belows uses a single & instead of &&. Therefore it is
not using short-circut logic. This seems like a typo. Does anyone
object if I change the '&' to '&&'?

    String delim = "";
    shellState.getReader().printString("System permissions: ");
    for (SystemPermission p : SystemPermission.values()) {
      if (shellState.getConnector().securityOperations().hasSystemPermission(user,
p) & p != null) {
        shellState.getReader().printString(delim + "System." + p.name());
        delim = ", ";
      }
    }
    shellState.getReader().printNewline();

Re: Non Short Circuit Logic

Posted by John Vines <vi...@apache.org>.
SystemPermission is an enum, and calling values spits back a Collection of
those enums. If we have a null in our enum, we have a MUCH larger issue at
hand.

John

On Wed, Sep 12, 2012 at 4:54 PM, David Medinets <da...@gmail.com>wrote:

> I didn't know for sure how the for loop acts when a null is the list.
> So I wrote some code:
>
>   List<String> strings = new ArrayList<String>();
>   strings.add("a");
>   strings.add(null);
>   strings.add("b");
>   for (String s : strings) {
>     System.out.println(s);
>   }
>
> The result is:
>
> a
> null
> b
>
> Therefore, testing for null within a for loop seems like a good idea.
>

Re: Non Short Circuit Logic

Posted by David Medinets <da...@gmail.com>.
I didn't know for sure how the for loop acts when a null is the list.
So I wrote some code:

  List<String> strings = new ArrayList<String>();
  strings.add("a");
  strings.add(null);
  strings.add("b");
  for (String s : strings) {
    System.out.println(s);
  }

The result is:

a
null
b

Therefore, testing for null within a for loop seems like a good idea.

Re: Non Short Circuit Logic

Posted by Jim Klucar <kl...@gmail.com>.
Without looking at SystemPermisison.values(), I don't know if John is
correct. If he's not, to get a speed improvement via short-circuiting,
you'd want to make the null check the first check in the conditional
because its faster than the method call.

On Wed, Sep 12, 2012 at 1:22 PM, John Vines <jv...@gmail.com> wrote:
> Judging from that code, p is never null so that second case is unnecessary.
>
> Sent from my phone, so pardon the typos and brevity.
> On Sep 12, 2012 12:29 PM, "David Medinets" <da...@gmail.com> wrote:
>
>> The if statement belows uses a single & instead of &&. Therefore it is
>> not using short-circut logic. This seems like a typo. Does anyone
>> object if I change the '&' to '&&'?
>>
>>     String delim = "";
>>     shellState.getReader().printString("System permissions: ");
>>     for (SystemPermission p : SystemPermission.values()) {
>>       if
>> (shellState.getConnector().securityOperations().hasSystemPermission(user,
>> p) & p != null) {
>>         shellState.getReader().printString(delim + "System." + p.name());
>>         delim = ", ";
>>       }
>>     }
>>     shellState.getReader().printNewline();
>>

Re: Non Short Circuit Logic

Posted by John Vines <jv...@gmail.com>.
Judging from that code, p is never null so that second case is unnecessary.

Sent from my phone, so pardon the typos and brevity.
On Sep 12, 2012 12:29 PM, "David Medinets" <da...@gmail.com> wrote:

> The if statement belows uses a single & instead of &&. Therefore it is
> not using short-circut logic. This seems like a typo. Does anyone
> object if I change the '&' to '&&'?
>
>     String delim = "";
>     shellState.getReader().printString("System permissions: ");
>     for (SystemPermission p : SystemPermission.values()) {
>       if
> (shellState.getConnector().securityOperations().hasSystemPermission(user,
> p) & p != null) {
>         shellState.getReader().printString(delim + "System." + p.name());
>         delim = ", ";
>       }
>     }
>     shellState.getReader().printNewline();
>