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

"Donaldson, John (GEO)" <[email protected]> Wed, 10 Feb 2010 10:07:42 +0000
Newsgroups gmane.comp.programming.refactoring
Message-ID <8A89F8BA0CF8D14BB5E18AEEBBAC36EF6F8ABBE4E2@GVW1121EXC.americas.hpqcorp.net>
George,

I agree with your position (IsEmpty is better than Size>0). But just to answer for Markus, it's harder to understand "not is empty" than "has some" (psychologically I mean). So, it seems good practice to frame conditions in their positive sense rather than their equivalent negative form.

John D.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of George Dinwiddie
Sent: 10 February 2010 10:30
To: [email protected]
Subject: Re: [refactoring] Literate style, making code more readable or going overboard?

Hi, Markus,

Markus Knittig wrote:
> George Dinwiddie wrote:
>> 	if (!adminUsers.isEmpty()) {
>> 	    ...
>> 	}
> 
> Not exactly topic related but are there any specific reason why you 
> prefer this instead of:
> if (adminUsers.size() > 0) {
>      ...
> }
> ?

I think it communicates my intent better.  The predicate isn't really 
concerned with size beyond whether it contains something.  The count of 
items is irrelevant detail.

Last I looked, isEmpty() just looks at the size in ArrayList, but I can 
imagine a situation where a collection might be able to evaluate 
isEmpty() a lot cheaper than size().

> Cause I learned that you should avoid negotiating conditions.

What does that mean?  Do you mean "negating?"  And why?

  - George

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



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

Yahoo! Groups Links





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

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/