You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Gary Gregory <ga...@gmail.com> on 2011/07/07 18:56:44 UTC

[lang] NullArgumentException usage

Hi All:

I do like using NullArgumentException, but I find writing this over and over
tedious:

if (arg == null) {
 thrown new NullArgumentException(argName);
}
something(arg);

How about this instead:

NullArgumentException.check(arg, argName);
something(arg);

or:

something(NullArgumentException.check(arg, argName));

Depending on the style you like.

Where check is:

    public static <T> T check(T arg, String argName) {
        if (arg == null) {
            throw new NullArgumentException(argName);
        }
        return arg;
    }

Yes, you are pushing the argName on the stack (or passing it in a register)
and that is extra work, but you do not have to use the new method then ;)

?

-- 
Thank you,
Gary

http://garygregory.wordpress.com/
http://garygregory.com/
http://people.apache.org/~ggregory/
http://twitter.com/GaryGregory

Re: [lang] NullArgumentException usage

Posted by Gary Gregory <ga...@gmail.com>.
On Thu, Jul 7, 2011 at 1:17 PM, Gary Gregory <ga...@gmail.com> wrote:

> On Thu, Jul 7, 2011 at 1:11 PM, Matt Benson <gu...@gmail.com> wrote:
>
>> On Thu, Jul 7, 2011 at 11:56 AM, Gary Gregory <ga...@gmail.com>
>> wrote:
>> > Hi All:
>> >
>> > I do like using NullArgumentException, but I find writing this over and
>> over
>> > tedious:
>> >
>> > if (arg == null) {
>> >  thrown new NullArgumentException(argName);
>> > }
>> > something(arg);
>> >
>> > How about this instead:
>> >
>> > NullArgumentException.check(arg, argName);
>> > something(arg);
>> >
>> > or:
>> >
>> > something(NullArgumentException.check(arg, argName));
>> >
>> > Depending on the style you like.
>> >
>> > Where check is:
>> >
>> >    public static <T> T check(T arg, String argName) {
>> >        if (arg == null) {
>> >            throw new NullArgumentException(argName);
>> >        }
>> >        return arg;
>> >    }
>> >
>> > Yes, you are pushing the argName on the stack (or passing it in a
>> register)
>> > and that is extra work, but you do not have to use the new method then
>> ;)
>> >
>> > ?
>>
>> Notice that NullArgumentException doesn't live in [lang] v3.  :|
>>
>
>
> Hm... Clearly, I missed that day.
>
> What happened? Did we decide it was not useful or redundant with something
> else?
>

Never mind, I see Validate now... ;)

Gary


>
> Gary
>
>>
>> Matt
>>
>> >
>> > --
>> > Thank you,
>> > Gary
>> >
>> > http://garygregory.wordpress.com/
>> > http://garygregory.com/
>> > http://people.apache.org/~ggregory/
>> > http://twitter.com/GaryGregory
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>
>
>
> --
> Thank you,
> Gary
>
> http://garygregory.wordpress.com/
> http://garygregory.com/
> http://people.apache.org/~ggregory/
> http://twitter.com/GaryGregory
>



-- 
Thank you,
Gary

http://garygregory.wordpress.com/
http://garygregory.com/
http://people.apache.org/~ggregory/
http://twitter.com/GaryGregory

Re: [lang] NullArgumentException usage

Posted by Gary Gregory <ga...@gmail.com>.
On Thu, Jul 7, 2011 at 1:11 PM, Matt Benson <gu...@gmail.com> wrote:

> On Thu, Jul 7, 2011 at 11:56 AM, Gary Gregory <ga...@gmail.com>
> wrote:
> > Hi All:
> >
> > I do like using NullArgumentException, but I find writing this over and
> over
> > tedious:
> >
> > if (arg == null) {
> >  thrown new NullArgumentException(argName);
> > }
> > something(arg);
> >
> > How about this instead:
> >
> > NullArgumentException.check(arg, argName);
> > something(arg);
> >
> > or:
> >
> > something(NullArgumentException.check(arg, argName));
> >
> > Depending on the style you like.
> >
> > Where check is:
> >
> >    public static <T> T check(T arg, String argName) {
> >        if (arg == null) {
> >            throw new NullArgumentException(argName);
> >        }
> >        return arg;
> >    }
> >
> > Yes, you are pushing the argName on the stack (or passing it in a
> register)
> > and that is extra work, but you do not have to use the new method then ;)
> >
> > ?
>
> Notice that NullArgumentException doesn't live in [lang] v3.  :|
>


Hm... Clearly, I missed that day.

What happened? Did we decide it was not useful or redundant with something
else?

Gary

>
> Matt
>
> >
> > --
> > Thank you,
> > Gary
> >
> > http://garygregory.wordpress.com/
> > http://garygregory.com/
> > http://people.apache.org/~ggregory/
> > http://twitter.com/GaryGregory
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>


-- 
Thank you,
Gary

http://garygregory.wordpress.com/
http://garygregory.com/
http://people.apache.org/~ggregory/
http://twitter.com/GaryGregory

Re: [lang] NullArgumentException usage

Posted by Matt Benson <gu...@gmail.com>.
On Thu, Jul 7, 2011 at 11:56 AM, Gary Gregory <ga...@gmail.com> wrote:
> Hi All:
>
> I do like using NullArgumentException, but I find writing this over and over
> tedious:
>
> if (arg == null) {
>  thrown new NullArgumentException(argName);
> }
> something(arg);
>
> How about this instead:
>
> NullArgumentException.check(arg, argName);
> something(arg);
>
> or:
>
> something(NullArgumentException.check(arg, argName));
>
> Depending on the style you like.
>
> Where check is:
>
>    public static <T> T check(T arg, String argName) {
>        if (arg == null) {
>            throw new NullArgumentException(argName);
>        }
>        return arg;
>    }
>
> Yes, you are pushing the argName on the stack (or passing it in a register)
> and that is extra work, but you do not have to use the new method then ;)
>
> ?

Notice that NullArgumentException doesn't live in [lang] v3.  :|

Matt

>
> --
> Thank you,
> Gary
>
> http://garygregory.wordpress.com/
> http://garygregory.com/
> http://people.apache.org/~ggregory/
> http://twitter.com/GaryGregory
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [lang] NullArgumentException usage

Posted by Adrian Crum <ad...@sandglass-software.com>.
I use an Assert class (similar to commons Validate):

Assert.notNull(argName, arg);

http://ci.apache.org/projects/ofbiz/site/javadocs/org/ofbiz/base/util/Assert.html

The difference is it throws IllegalArgumentException.

-Adrian


On 7/7/2011 5:56 PM, Gary Gregory wrote:
> Hi All:
>
> I do like using NullArgumentException, but I find writing this over and over
> tedious:
>
> if (arg == null) {
>   thrown new NullArgumentException(argName);
> }
> something(arg);
>
> How about this instead:
>
> NullArgumentException.check(arg, argName);
> something(arg);
>
> or:
>
> something(NullArgumentException.check(arg, argName));
>
> Depending on the style you like.
>
> Where check is:
>
>      public static<T>  T check(T arg, String argName) {
>          if (arg == null) {
>              throw new NullArgumentException(argName);
>          }
>          return arg;
>      }
>
> Yes, you are pushing the argName on the stack (or passing it in a register)
> and that is extra work, but you do not have to use the new method then ;)
>
> ?
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [lang] NullArgumentException usage

Posted by James Ring <sj...@jdns.org>.
Hey,

On Thu, Jul 7, 2011 at 9:56 AM, Gary Gregory <ga...@gmail.com> wrote:
> Hi All:
>
> I do like using NullArgumentException, but I find writing this over and over
> tedious:
>
> if (arg == null) {
>  thrown new NullArgumentException(argName);
> }
> something(arg);
>
> How about this instead:
>
> NullArgumentException.check(arg, argName);
> something(arg);
>
> or:
>
> something(NullArgumentException.check(arg, argName));
>
> Depending on the style you like.
>
> Where check is:
>
>    public static <T> T check(T arg, String argName) {
>        if (arg == null) {
>            throw new NullArgumentException(argName);
>        }
>        return arg;
>    }
>
> Yes, you are pushing the argName on the stack (or passing it in a register)
> and that is extra work, but you do not have to use the new method then ;)

Google Guava does it with com.google.common.base.Preconditions:

something(Preconditions.checkNotNull(arg));

> ?
>
> --
> Thank you,
> Gary
>
> http://garygregory.wordpress.com/
> http://garygregory.com/
> http://people.apache.org/~ggregory/
> http://twitter.com/GaryGregory
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org