Re: Literate style, making code more readable or going overboard?

George Dinwiddie <[email protected]> Tue, 09 Feb 2010 20:38:54 -0500
Newsgroups gmane.comp.programming.refactoring
Organization iDIA Computing, LLC
Message-ID <[email protected]>
I would tend to do it, especially the second time I had to do such a 
test.  I would do it to bundle the null check & size check together.

Even more likely, I would write getUsersWithAdminRights() such that it 
never returned null.  Then I could write

	if (!adminUsers.isEmpty()) {
	    ...
	}

I find this to be as readable, and less error prone. (What if someone 
forgot about the method and the need to make both checks?)  With 
collections, the Null Object Pattern is free--you just return an empty 
collection.

  - George


aglet_sesamoid 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)
> }
> 

-- 
  ----------------------------------------------------------------------
   * George Dinwiddie *                      http://blog.gdinwiddie.com
   Software Development                    http://www.idiacomputing.com
   Consultant and Coach                    http://www.agilemaryland.org
  ----------------------------------------------------------------------



------------------------------------

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/