Re: Beginnerquestion about if (personName==null || personName.equals(""))
Re: Beginnerquestion about if (personName==null || personName.equals(""))
- Subject: Re: Beginnerquestion about if (personName==null || personName.equals(""))
- From: Chuck Hill <email@hidden>
- Date: Fri, 7 Sep 2007 11:17:34 -0700
On Sep 7, 2007, at 11:09 AM, Ray Kiddy wrote:
On Sep 7, 2007, at 2:44 AM, Rainer User wrote:
Hi Folks,
i am new at the webobjects-dev maillist and new to webobjects too.
I am reading the white printed book "Webobjects 5, Web
Applications", from Apple, it was included in the Webobjects
Package of my Webobject-Box.
On Page 84 i found an example where a funktion trys to detect if a
variable named "personName" [type of it is "String"] is empty or not.
The substantial Code says:
if (personName==null || personName.equals(""))
{
return(true);
}
else
{
return(false);
}
In my opinion it would be the same if i write only:
if (personName==null)
{
return(true);
}
else
{
return(false);
}
Where is my misunderstanding?
Why i have to use personName.equals("") +and+
personName==null to detect if personName is empty or not?
Maybe someone can help me ore give me a hint?
Thanks from Germany,
Thomas
There are reasons for these things, though they are sometimes
historical.
Any framework has to make decisions about this kind of thing and
try to be consistent. I am finding the same thing in javascript
where it is not clear if a function is returning an object.
Sometimes one needs to check for "" (the empty string), null, and
undefined.
If you are getting something back from a method in WebObjects and
it is null and you think it should be guaranteed to be a non-null
object, you could file a bug and try to make that case. It might be
more productive to do this in the Project Wonder code, as it is
open-source, you can see the code, and they already do a lot of
wrapping of WebObjects APIs.
You will probably find it just as bothersome to get back an array
and have to check if it is a null or an empty array.
Back in the day, during the port of WebObjects from ObjC to java,
there were lots and lots of arguments about this. The fact that
ObjC allows you to send a message to null and get back something
made some interesting hoops to jump through during the port. It is
nice that in ObjC, you can just say:
foo = [[ec registeredObjects] count];
or:
bar = [[eo firstName] description];
You would just literally be able to spend 0 brain cycles worrying
about these calls. But this is java.
Gee, thanks Ray. I was almost getting over that emotional scar. :-)
Null handling is Java is a huge PITA.
I have resorted to writing methods like
public static boolean isEmtpy(String aString) {
return aString == null || aString.length() == 0;
}
So this
if (personName==null || personName.equals(""))
{
return(true);
}
else
{
return(false);
}
reduces to
return Strings.isEmpty(personName);
Objective-C. Sigh.
Chuck
--
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden