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

Olof Bjarnason <[email protected]> Wed, 10 Feb 2010 11:30:16 +0100
Newsgroups gmane.comp.programming.refactoring
Message-ID <[email protected]>
2010/2/10 George Dinwiddie <[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.
>

To continue on that line of thinking, what is the operation contained within
the block..? Maybe it should be an operation inside the adminUsers object?

adminUsers.ApplyOperation()

Then we avoid the 'if' altogether.


>
> - 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
> ----------------------------------------------------------
>
>  
>



-- 
http://olofb.wordpress.com


[Non-text portions of this message have been removed]



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

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/