Re: Literate style, making code more readable or going overboard?
William Wake <[email protected]> Tue, 9 Feb 2010 20:47:43 -0500
| Newsgroups | gmane.comp.programming.refactoring |
|---|---|
| Message-ID | <[email protected]> |
First I'd probably look at making getUsersWithAdminRights() & see if I could make it return an empty array to avoid the null check completely. Then I'd start to wonder why I needed to return an array list at all, and see if there's a missing object there somewhere. (Who am I asking for this list of users? This object itself? Is allUsers a real object that I can ask, or is it yet another ArrayList?) Can I make some sort of iterator/enumerator? (Do I really need the whole collection?) To answer your immediate question a little more, if I have two conditions I start to wonder about pulling it out to a separate method, especially if more than one place uses those conditions. (But I try to eliminate null checks, and I'm always suspicious of ArrayLists being passed around.) --Bill Wake On Tue, Feb 9, 2010 at 8:21 PM, aglet_sesamoid <[email protected]> wrote: > Consider this snippet of java code: > > ... > ArrayList adminUsers = getUsersWithAdminRights( allUsers ); > if ((adminUsers != null) && (adminUsers.size() > 0)) { > .... > } > ... > > Does anyone think that this refactoring to a more literate style makes the code easier to read and understand or is it overkill? > > ... > ArrayList adminUsers = getUsersWithAdminRights( all Users ); > if (thereAreAny(adminUsers) { > .... > } > ... > > private boolean thereAreAny(ArrayList list) { > if (list == null) > return false; > else > return (list.size() > 0) > } > -- Bill Wake Industrial Logic, Inc. Try our eLearning on TDD, refactoring, and more - http://industriallogic.com ------------------------------------ Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/refactoring/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/refactoring/join (Yahoo! ID required) <*> To change settings via email: [email protected] [email protected] <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/