You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ofbiz.apache.org by Erwan de FERRIERES <er...@nereide.biz> on 2012/03/19 21:08:25 UTC
loop code simplification
Hi,
I'm trying to remove a lot of iterators, and use the for-each syntax,
which exists since java 1.5.
During my journey, I found a lot of double tests for a while like this one:
while (typePurposes != null && typePurposes.hasNext()) {
(ContactMechWorker.java line 606)
Can it be simplified to for(GenericValue contactMechTypePurpose :
theList) ? Or should I keep it like it is ?
Regards,
--
Erwan de FERRIERES
www.nereide.biz
Re: loop code simplification
Posted by Prince Sewani <pr...@ymail.com>.
Well I don't know why is it suggested so as in the mail by Paul,
But as I already mentioned :
----- Forwarded Message -----
From: Prince Sewani <pr...@ymail.com>
To: "dev@ofbiz.apache.org" <de...@ofbiz.apache.org>
Sent: Tuesday, March 20, 2012 5:46 PM
Subject: Re: loop code simplification
Well If we're talking about the 'Enhanced For loop' in Java from 1.5 onwards
Then it goes something like this, you'll only get NPE if you haven't initialized the collection,
otherwise if you've initialized the collection and there are no values in it then
the for loop won't execute any iteration.
For example :
import java.util.ArrayList;
class Test{
static ArrayList<String>t;
public static void main(String [] args){
System.out.println("before===========");
for(String k : t){
System.out.println("===="+k);
}
System.out.println("after===========");
}
}
The above program will generate an NPE when ran and the output will be :
before===========
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:11)
but if I replace the ArrayList declaration with :
static ArrayList<String> t = new ArrayList<String>();
then the output will be :
before===========
after===========
Hope this helps.
Regards
Prince
Java does take care of it.
Regards
Prince
________________________________
From: Jacques Le Roux <ja...@les7arts.com>
To: dev@ofbiz.apache.org
Sent: Tuesday, March 20, 2012 8:36 PM
Subject: Re: loop code simplification
BTW, this shows how stupid is the for loop in Java implementation. The suggested safeList() should be handled by the compiler IMO, I
see no gains to not have it in but to get NPEs. Did I miss something?
Jacques
From: "Paul Foxworthy" <pa...@cohsoft.com.au>
> Hi Erwan,
>
> To be sure there is no Null Pointer Exception, yes, you need to test for
> null first. One possibility is to just let the NPE happen.
>
> The discussion at
> http://stackoverflow.com/questions/2250031/null-check-in-an-enhanced-for-loop
>
> suggests
>
> for( Object o : safe( list ) ) {
> // do whatever
> }
>
> Where safe would be:
>
> public static List safe( List other ) {
> return other == null ? Collections.EMPTY_LIST : other;
> }
>
> Cleaner code. I suspect the method would be inlined by most Java compilers.
>
> Cheers
>
> Paul Foxworthy
>
>
> Erwan de FERRIERES-3 wrote
>>
>> Hi,
>>
>> I'm trying to remove a lot of iterators, and use the for-each syntax,
>> which exists since java 1.5.
>> During my journey, I found a lot of double tests for a while like this
>> one:
>>
>> while (typePurposes != null && typePurposes.hasNext()) {
>> (ContactMechWorker.java line 606)
>>
>> Can it be simplified to for(GenericValue contactMechTypePurpose :
>> theList) ? Or should I keep it like it is ?
>>
>> Regards,
>>
>> --
>> Erwan de FERRIERES
>> www.nereide.biz
>>
>
> -----
> --
> Coherent Software Australia Pty Ltd
> http://www.cohsoft.com.au/
>
> Bonsai ERP, the all-inclusive ERP system
> http://www.bonsaierp.com.au/
>
> --
> View this message in context: http://ofbiz.135035.n4.nabble.com/loop-code-simplification-tp4487741p4488324.html
> Sent from the OFBiz - Dev mailing list archive at Nabble.com.
Re: loop code simplification
Posted by Scott Gray <sc...@hotwaxmedia.com>.
I think it's really necessary to look at the bigger picture rather than worrying about having a null check in place. The delegator methods that return a List pretty much never return null, so the only time that specific list will be null is when an EntityException is thrown. So IMO the main problem with the methods in ContactMechWorker are that they silently (aside from logging) absorb those exceptions and then try to carry on as if nothing went wrong.
IMO those methods should probably have a 'throws EntityException' declaration added and then all the catch blocks and list<GenericValue> != null checks removed. There is so much wrong in that class though, it could do with plenty of other work as well.
Regards
Scott
On 21/03/2012, at 4:24 AM, Erwan de FERRIERES wrote:
> 2012/3/20 Jacques Le Roux <ja...@les7arts.com>:
>> BTW, this shows how stupid is the for loop in Java implementation. The
>> suggested safeList() should be handled by the compiler IMO, I
>> see no gains to not have it in but to get NPEs. Did I miss something?
>>
> Nothing. Is there an implementation of this safeList() in commons ?
>
> I think I will keep the while as they are, and work only on those
> which only have the .hasNext().
>
> Thanks for your comments !
>
>> Jacques
>>
>> From: "Paul Foxworthy" <pa...@cohsoft.com.au>
>>
>>> Hi Erwan,
>>>
>>> To be sure there is no Null Pointer Exception, yes, you need to test for
>>> null first. One possibility is to just let the NPE happen.
>>>
>>> The discussion at
>>>
>>> http://stackoverflow.com/questions/2250031/null-check-in-an-enhanced-for-loop
>>>
>>> suggests
>>>
>>> for( Object o : safe( list ) ) {
>>> // do whatever
>>> }
>>>
>>> Where safe would be:
>>>
>>> public static List safe( List other ) {
>>> return other == null ? Collections.EMPTY_LIST : other;
>>> }
>>>
>>> Cleaner code. I suspect the method would be inlined by most Java
>>> compilers.
>>>
>>> Cheers
>>>
>>> Paul Foxworthy
>>>
>>>
>>> Erwan de FERRIERES-3 wrote
>>>>
>>>>
>>>> Hi,
>>>>
>>>> I'm trying to remove a lot of iterators, and use the for-each syntax,
>>>> which exists since java 1.5.
>>>> During my journey, I found a lot of double tests for a while like this
>>>> one:
>>>>
>>>> while (typePurposes != null && typePurposes.hasNext()) {
>>>> (ContactMechWorker.java line 606)
>>>>
>>>> Can it be simplified to for(GenericValue contactMechTypePurpose :
>>>> theList) ? Or should I keep it like it is ?
>>>>
>>>> Regards,
>>>>
>>>> --
>>>> Erwan de FERRIERES
>>>> www.nereide.biz
>>>>
>>>
>>> -----
>>> --
>>> Coherent Software Australia Pty Ltd
>>> http://www.cohsoft.com.au/
>>>
>>> Bonsai ERP, the all-inclusive ERP system
>>> http://www.bonsaierp.com.au/
>>>
>>> --
>>> View this message in context:
>>> http://ofbiz.135035.n4.nabble.com/loop-code-simplification-tp4487741p4488324.html
>>> Sent from the OFBiz - Dev mailing list archive at Nabble.com.
>
>
>
> --
> Erwan de FERRIERES
Re: loop code simplification
Posted by Erwan de FERRIERES <er...@gmail.com>.
2012/3/20 Jacques Le Roux <ja...@les7arts.com>:
> BTW, this shows how stupid is the for loop in Java implementation. The
> suggested safeList() should be handled by the compiler IMO, I
> see no gains to not have it in but to get NPEs. Did I miss something?
>
Nothing. Is there an implementation of this safeList() in commons ?
I think I will keep the while as they are, and work only on those
which only have the .hasNext().
Thanks for your comments !
> Jacques
>
> From: "Paul Foxworthy" <pa...@cohsoft.com.au>
>
>> Hi Erwan,
>>
>> To be sure there is no Null Pointer Exception, yes, you need to test for
>> null first. One possibility is to just let the NPE happen.
>>
>> The discussion at
>>
>> http://stackoverflow.com/questions/2250031/null-check-in-an-enhanced-for-loop
>>
>> suggests
>>
>> for( Object o : safe( list ) ) {
>> // do whatever
>> }
>>
>> Where safe would be:
>>
>> public static List safe( List other ) {
>> return other == null ? Collections.EMPTY_LIST : other;
>> }
>>
>> Cleaner code. I suspect the method would be inlined by most Java
>> compilers.
>>
>> Cheers
>>
>> Paul Foxworthy
>>
>>
>> Erwan de FERRIERES-3 wrote
>>>
>>>
>>> Hi,
>>>
>>> I'm trying to remove a lot of iterators, and use the for-each syntax,
>>> which exists since java 1.5.
>>> During my journey, I found a lot of double tests for a while like this
>>> one:
>>>
>>> while (typePurposes != null && typePurposes.hasNext()) {
>>> (ContactMechWorker.java line 606)
>>>
>>> Can it be simplified to for(GenericValue contactMechTypePurpose :
>>> theList) ? Or should I keep it like it is ?
>>>
>>> Regards,
>>>
>>> --
>>> Erwan de FERRIERES
>>> www.nereide.biz
>>>
>>
>> -----
>> --
>> Coherent Software Australia Pty Ltd
>> http://www.cohsoft.com.au/
>>
>> Bonsai ERP, the all-inclusive ERP system
>> http://www.bonsaierp.com.au/
>>
>> --
>> View this message in context:
>> http://ofbiz.135035.n4.nabble.com/loop-code-simplification-tp4487741p4488324.html
>> Sent from the OFBiz - Dev mailing list archive at Nabble.com.
--
Erwan de FERRIERES
Re: loop code simplification
Posted by Jacques Le Roux <ja...@les7arts.com>.
BTW, this shows how stupid is the for loop in Java implementation. The suggested safeList() should be handled by the compiler IMO, I
see no gains to not have it in but to get NPEs. Did I miss something?
Jacques
From: "Paul Foxworthy" <pa...@cohsoft.com.au>
> Hi Erwan,
>
> To be sure there is no Null Pointer Exception, yes, you need to test for
> null first. One possibility is to just let the NPE happen.
>
> The discussion at
> http://stackoverflow.com/questions/2250031/null-check-in-an-enhanced-for-loop
>
> suggests
>
> for( Object o : safe( list ) ) {
> // do whatever
> }
>
> Where safe would be:
>
> public static List safe( List other ) {
> return other == null ? Collections.EMPTY_LIST : other;
> }
>
> Cleaner code. I suspect the method would be inlined by most Java compilers.
>
> Cheers
>
> Paul Foxworthy
>
>
> Erwan de FERRIERES-3 wrote
>>
>> Hi,
>>
>> I'm trying to remove a lot of iterators, and use the for-each syntax,
>> which exists since java 1.5.
>> During my journey, I found a lot of double tests for a while like this
>> one:
>>
>> while (typePurposes != null && typePurposes.hasNext()) {
>> (ContactMechWorker.java line 606)
>>
>> Can it be simplified to for(GenericValue contactMechTypePurpose :
>> theList) ? Or should I keep it like it is ?
>>
>> Regards,
>>
>> --
>> Erwan de FERRIERES
>> www.nereide.biz
>>
>
> -----
> --
> Coherent Software Australia Pty Ltd
> http://www.cohsoft.com.au/
>
> Bonsai ERP, the all-inclusive ERP system
> http://www.bonsaierp.com.au/
>
> --
> View this message in context: http://ofbiz.135035.n4.nabble.com/loop-code-simplification-tp4487741p4488324.html
> Sent from the OFBiz - Dev mailing list archive at Nabble.com.
Re: loop code simplification
Posted by Prince Sewani <pr...@ymail.com>.
Well If we're talking about the 'Enhanced For loop' in Java from 1.5 onwards
Then it goes something like this, you'll only get NPE if you haven't initialized the collection,
otherwise if you've initialized the collection and there are no values in it then
the for loop won't execute any iteration.
For example :
import java.util.ArrayList;
class Test{
static ArrayList<String>t;
public static void main(String [] args){
System.out.println("before===========");
for(String k : t){
System.out.println("===="+k);
}
System.out.println("after===========");
}
}
The above program will generate an NPE when ran and the output will be :
before===========
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:11)
but if I replace the ArrayList declaration with :
static ArrayList<String> t = new ArrayList<String>();
then the output will be :
before===========
after===========
Hope this helps.
Regards
Prince
________________________________
From: Paul Foxworthy <pa...@cohsoft.com.au>
To: dev@ofbiz.apache.org
Sent: Tuesday, March 20, 2012 4:17 PM
Subject: Re: loop code simplification
Hi Erwan,
To be sure there is no Null Pointer Exception, yes, you need to test for
null first. One possibility is to just let the NPE happen.
The discussion at
http://stackoverflow.com/questions/2250031/null-check-in-an-enhanced-for-loop
suggests
for( Object o : safe( list ) ) {
// do whatever
}
Where safe would be:
public static List safe( List other ) {
return other == null ? Collections.EMPTY_LIST : other;
}
Cleaner code. I suspect the method would be inlined by most Java compilers.
Cheers
Paul Foxworthy
Erwan de FERRIERES-3 wrote
>
> Hi,
>
> I'm trying to remove a lot of iterators, and use the for-each syntax,
> which exists since java 1.5.
> During my journey, I found a lot of double tests for a while like this
> one:
>
> while (typePurposes != null && typePurposes.hasNext()) {
> (ContactMechWorker.java line 606)
>
> Can it be simplified to for(GenericValue contactMechTypePurpose :
> theList) ? Or should I keep it like it is ?
>
> Regards,
>
> --
> Erwan de FERRIERES
> www.nereide.biz
>
-----
--
Coherent Software Australia Pty Ltd
http://www.cohsoft.com.au/
Bonsai ERP, the all-inclusive ERP system
http://www.bonsaierp.com.au/
--
View this message in context: http://ofbiz.135035.n4.nabble.com/loop-code-simplification-tp4487741p4488324.html
Sent from the OFBiz - Dev mailing list archive at Nabble.com.
Re: loop code simplification
Posted by Paul Foxworthy <pa...@cohsoft.com.au>.
Hi Erwan,
To be sure there is no Null Pointer Exception, yes, you need to test for
null first. One possibility is to just let the NPE happen.
The discussion at
http://stackoverflow.com/questions/2250031/null-check-in-an-enhanced-for-loop
suggests
for( Object o : safe( list ) ) {
// do whatever
}
Where safe would be:
public static List safe( List other ) {
return other == null ? Collections.EMPTY_LIST : other;
}
Cleaner code. I suspect the method would be inlined by most Java compilers.
Cheers
Paul Foxworthy
Erwan de FERRIERES-3 wrote
>
> Hi,
>
> I'm trying to remove a lot of iterators, and use the for-each syntax,
> which exists since java 1.5.
> During my journey, I found a lot of double tests for a while like this
> one:
>
> while (typePurposes != null && typePurposes.hasNext()) {
> (ContactMechWorker.java line 606)
>
> Can it be simplified to for(GenericValue contactMechTypePurpose :
> theList) ? Or should I keep it like it is ?
>
> Regards,
>
> --
> Erwan de FERRIERES
> www.nereide.biz
>
-----
--
Coherent Software Australia Pty Ltd
http://www.cohsoft.com.au/
Bonsai ERP, the all-inclusive ERP system
http://www.bonsaierp.com.au/
--
View this message in context: http://ofbiz.135035.n4.nabble.com/loop-code-simplification-tp4487741p4488324.html
Sent from the OFBiz - Dev mailing list archive at Nabble.com.