I'm guessing your colleague felt that something where a response of 0 things to operate on is not an error condition it is easier to deal with a 0 entry list, for example:
public int getFooTotalValue() { int fooTotal = 0; List<Foo> fooList = getFooList(); for (Foo afoo : fooList ) { fooTotal += afoo.getValue(); } return fooTotal; } Handles the 0 length case without any checks. (Works nice for totals, or "do an operation on a foo" loops, not so well for average, or search for mean/median type loops.) It's nice in that it doesn't make assumptions about what the List consuming code considers an acceptable response. If you are prescient and know that every possible consumer of the getFooList() result MUST have at least one entry or it's an exceptional condition, I suppose you could go with the NPE check, but I tend to feel that in isolation if (!fooList.isEmpty()) { } gives me more information than if (fooList != null) { } or the even more compact if (fooList) { } As the isEmpty() case I know the list was set up correctly and I didn't process fooList because there were no foo, where the !=null case could be some initialization issue with foo (in the case of reading a file to fill foo, I might want fooList = null for error condition of no/improperly formatted file, empty list for read the file and found no foo.) Dan On Monday, August 4, 2014 3:51:38 PM UTC-4, sweety fx wrote: > > My colleague mentioned list is the better way of doing it. So that we > don't face null pointer exceptions. since we check for list.isEmpty() > But I wasn't sure, thats why I am checking. > > On Monday, August 4, 2014 3:42:35 PM UTC-4, Steve Gabrilowitz wrote: >> >> How would list.isEmpty() be better than object==null? If you never plan >> on returning more than one object then it makes no sense to use a list. >> On Aug 4, 2014 11:04 AM, "sweety fx" <fxsw...@gmail.com> wrote: >> >>> A method is supposed to return a object when called. >>> >>> Which is better to implement for the method: >>> 1. Is it better to have the method return an object. >>> 2. Or return List<Object> where the single object is added to the array >>> list. >>> >>> because, we can check for list.isEmpty() which is better than >>> object!=null. not sure which one is better. >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Android Developers" group. >>> To post to this group, send email to android-d...@googlegroups.com >>> To unsubscribe from this group, send email to >>> android-developers+unsubscr...@googlegroups.com >>> For more options, visit this group at >>> http://groups.google.com/group/android-developers?hl=en >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "Android Developers" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to android-developers+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/d/optout. >>> >> -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.