Menu

#42 NAB_NEEDLESS_BOOLEAN_CONSTANT_CONVERSION helpful?

closed-wont-fix
Detector (52)
5
2019-06-29
2008-04-08
No

Hi!

to start with: We find fbcontrib very useful, use it a lot and reported some bugs before.
I understand, that people have different opinions about what findbugs/fbcontrib reports are useful and what aren't.

But I don't understand the motivation behind NAB_NEEDLESS_BOOLEAN_CONSTANT_CONVERSION when used on the following code:

void doit(Boolean b) {
...
}

doit(true);

1. The code is short (true instead of Boolean.TRUE)
2. No memory is wasted (javac should autobox to Boolean.TRUE and Boolean.FALSE)
3. Nothing can go wrong (no null pointer risk or such).

I find it useful for situations as

void doit(boolean b) {
}

doit(Boolean.TRUE)

but I think it does not make sense for the first situation.

Thanks for a great program!

Greatings,
alph

Discussion

  • Dave Brosius

    Dave Brosius - 2008-04-15
    • labels: --> Detector
    • assigned_to: nobody --> dbrosius
     
  • Dave Brosius

    Dave Brosius - 2008-05-29

    Logged In: YES
    user_id=66596
    Originator: NO

    I agree the problem is not that big of a deal. However, in my experience, people often don't pay attention to the boxing level of their variables, and pass them around all over and constantly switch between boxed and unboxed versions for no reason other than not paying attention.

    I'm not sure why you would do
    void doit(Boolean b) {
    ...
    }

    doit(true);

    over

    void doit(Boolean b) {
    ...
    }

    doit(Boolean.TRUE);

    even if you consider the extra Boolean.valueOf method call irrelevant.

     
  • Dave Brosius

    Dave Brosius - 2008-05-29

    Logged In: YES
    user_id=66596
    Originator: NO

    I agree the problem is not that big of a deal. However, in my experience, people often don't pay attention to the boxing level of their variables, and pass them around all over and constantly switch between boxed and unboxed versions for no reason other than not paying attention.

    I'm not sure why you would do
    void doit(Boolean b) {
    ...
    }

    doit(true);

    over

    void doit(Boolean b) {
    ...
    }

    doit(Boolean.TRUE);

    even if you consider the extra Boolean.valueOf method call irrelevant.

     
  • Dave Brosius

    Dave Brosius - 2009-01-26
    • status: open --> closed-wont-fix
     
  • Pavel Roskin

    Pavel Roskin - 2019-06-29

    Why? Maybe the caller and the callee are in different packages developed by different people. Just because the API accepts null, it doesn't mean I want to emphasize that fact when calling it with true or false. "Be conservative in what you do, be liberal in what you accept from others"

    Or maybe the function accepts Object, as it's the case with JSONObject#put(). Surely I can pass Boolean.TRUE, but then I'm doing another thing needlessy, specifically accessing a public static member of Boolean. Between those two "needless" alternatives, I prefer the one that is short and idiomatic.

    All the rest of the suggestions from fb-contrib are very useful, but this one just doesn't belong with the rest.

     

Log in to post a comment.