I believe the solution is to get the object you want, and then perform operations only on the initial object you received.

So in the case that David mentioned:

>>Object f ()
>>{
>>        If (rand() > 0.5) return someNonNullObject;
>>        else return null;
>>}
>>
>>Object x = (f() == null) ? "" : f();

You would do

Object x = f();
String objectIfNull = "";

x = (x == null) ? objectIfNull : x;

Or whatever would solve your business requirements.

Cory


Tony Spencer wrote:
I understand the dilemna you have stated. So what is the solution to the
problem?



-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of David Spitz
Sent: Thursday, July 22, 2004 4:01 PM
To: 'Research Triangle Java User's Group mailing list.'
Subject: RE: [Juglist] RE: Which is more efficient?




A benefit of the #2 approach over the #1

approach is that the variable 'formcomments'
will not exist in a 'null' state.


Actually, the #2 approach is NOT safe. There is no guarantee that the value returned by attr.getFormComments() will not change between calls (even in a single-thread environment, the call could cause an internal counter to increment, for example); it could be non-null on the first call and null on the second, in which case the logic fails, and worse, fails in an unexpected manner. Consider this pseudocode:

Object f ()
{
        If (rand() > 0.5) return someNonNullObject;
        else return null;
}

Object x = (f() == null) ? "" : f();

The resulting value of x is not predictable; it has a 50% change of being null or not null.

This is why you should code clearly first and optimize later, preferably with a more scientific approach (e.g., a profiler, etc.).

David

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Rippy, Jonathan
Sent: Thursday, July 22, 2004 2:24 PM
To: Research Triangle Java User's Group mailing list.
Subject: RE: [Juglist] RE: Which is more efficient?



I would say example #1 is potentially more efficient. Example #2 invokes a method on a object instance twice and this could result in a resource intensive operation occuring twice. Of course, it appears to be a simple getter method and it probably does not do that, but I assume you are asking the question in general.


A benefit of the #2 approach over the #1 approach is that the variable 'formcomments' will not exist in a 'null' state. With #1 someone could come along and interject code between your first and second steps and cause a problem (unlikely of course, but it is a potential issue.)


-- Jonathan Rippy



-----Original Message-----
From: Tony Spencer [mailto:[EMAIL PROTECTED]
Sent: Thursday, July 22, 2004 2:18 PM
To: 'Research Triangle Java User's Group mailing list.'
Subject: [Juglist] RE: Which is more efficient?


Sorry. My stupid email client dropped some carriage returns.

Example 1:
----------
String formcomments = attr.getFormComments();

formcomments = (formcomments == null) ? "" : formcomments;


Example 2: ---------- String formcomments = (attr.getFormComments()== null) ? "" : attr.getFormComments();

T O N Y S P E N C E R
Notsleepy LLC
6512 Six Forks Rd.
Suite 502-B Raleigh, NC 27615
Phone: 919.848.0691
Mobile: 415.637.6481
[EMAIL PROTECTED]




_______________________________________________
Juglist mailing list
[EMAIL PROTECTED]
http://trijug.org/mailman/listinfo/juglist_trijug.org





_______________________________________________ Juglist mailing list [EMAIL PROTECTED] http://trijug.org/mailman/listinfo/juglist_trijug.org



_______________________________________________ Juglist mailing list [EMAIL PROTECTED] http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to