Richard | 2 Feb 2010 20:20
Favicon
Gravatar

C++ Refactoring Tools Test Suite Available

<http://d3dgraphicspipeline.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=39824>

-- 
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
 <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

      Legalize Adulthood! <http://legalizeadulthood.wordpress.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:
    refactoring-digest <at> yahoogroups.com 
    refactoring-fullfeatured <at> yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    refactoring-unsubscribe <at> yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
(Continue reading)

aglet_sesamoid | 10 Feb 2010 02:21
Picon

Literate style, making code more readable or going overboard?

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

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

Yahoo! Groups Links

(Continue reading)

William Wake | 10 Feb 2010 02:47
Picon
Favicon

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

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 <simonvarley <at> comcast.net> 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?
>
(Continue reading)

George Dinwiddie | 10 Feb 2010 02:38
Favicon

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

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?
> 
(Continue reading)

Markus Knittig | 10 Feb 2010 09:12
Picon
Gravatar

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

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) {
     ...
}
?
Cause I learned that you should avoid negotiating conditions.

Best regards,
  Markus

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

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)

(Continue reading)

George Dinwiddie | 10 Feb 2010 10:30
Favicon

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

--

-- 
(Continue reading)

Donaldson, John (GEO | 10 Feb 2010 11:07
Picon
Favicon

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

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: refactoring <at> yahoogroups.com [mailto:refactoring <at> yahoogroups.com] On Behalf Of George Dinwiddie
Sent: 10 February 2010 10:30
To: refactoring <at> yahoogroups.com
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 
(Continue reading)

Olof Bjarnason | 10 Feb 2010 11:30
Picon
Gravatar

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

2010/2/10 George Dinwiddie <lists <at> idiacomputing.com>

>
>
> 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
>
(Continue reading)

George Dinwiddie | 10 Feb 2010 14:16
Favicon

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

Olof Bjarnason wrote:
>> 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.

I like that, Olof.

  - George

--

-- 
  ----------------------------------------------------------------------
   * 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:
(Continue reading)

Jeff Foster | 10 Feb 2010 10:22
Picon

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

I can think of two reasons
- IsEmpty expresses the intent more clearly (I'm not checking the size of
the list, I'm checking whether it is empty)
- isEmpty is often O(1) implementation whereas size() can be an O(N)
operation.

On 10 February 2010 08:12, Markus Knittig <markus <at> myd0.de> 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) {
> ...
> }
> ?
> Cause I learned that you should avoid negotiating conditions.
>
> Best regards,
> Markus
>  
>

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

(Continue reading)


Gmane