• Post Reply Bookmark Topic Watch Topic
  • New Topic

Should EJB not return initialized objects?

 
James Tikalsky
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a friendly argument with a coworker, and we need some advice.
In general, I return an empty object when coding instead of null. For example:
public String getString() {
String aString;
// do something and assign value to myString
if ( myString == null )
{
aString = "";
}
return aString;
}
This makes my API nice and friendly, since the caller doesn't have to check for null every time they call the method.
So, the question is... if the method getString() were being called on an EJB, is this a bad idea?
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This really depends on the context. An empty object means something very different than a null object. For example, an empty String and a null will be persisted to a database in different ways. Furthermore, it would be conceptually incorrect to return an empty String for a null database entry.
For your particular application this might not make a difference. However, for many applications this makes a very big difference both from a business standpoint and possibly from a legal standpoint.
[ January 21, 2004: Message edited by: Chris Mathews ]
 
James Tikalsky
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Mathews:
Furthermore, it would be conceptually incorrect to return an empty String for a null database entry.
[ January 21, 2004: Message edited by: Chris Mathews ]

What do you mean by "conceptually incorrect"? Do you mean if a record has a null value in one column, it must be represented as an object reference to null? Or do you mean that you should return somehing more explicit, like a string with the value "null"? Or do you mean that no records were found, and therefore no objects were created?
 
Ram Mohan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Tikalsky:

What do you mean by "conceptually incorrect"? Do you mean if a record has a null value in one column, it must be represented as an object reference to null? Or do you mean that you should return somehing more explicit, like a string with the value "null"? Or do you mean that no records were found, and therefore no objects were created?

basically all modern databases store & treat "null" values as totally different from, say for example an empty string. If it remember right, it is Codd's rule #7 for a DBMS to be treated as an RDBMS.
So, in your program you can always catch the Null value exception & store a null value in your database. The SQL statement "insert into database values('ram',null,null)" would store null values in the columns specified.
[ January 22, 2004: Message edited by: Ram Mohan ]
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Related to the discussion, I have another question.
I always assumed that Empty strings and NULL values are treated the same in Oracle, especially when used in comparison statements in PL/SQL.
[ January 22, 2004: Message edited by: Vish Kumar ]
 
Ram Mohan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vish Kumar:
Related to the discussion, I have another question.
I always assumed that Empty strings and NULL values are treated the same in Oracle, especially when used in comparison statements in PL/SQL.
[ January 22, 2004: Message edited by: Vish Kumar ]

i really don't know about the validity of your statement. But even if it is correct, you're then tying your code down to work only with Oracle, so any porting of databases become difficult. I hope i'm correct here....(this i should consider is more of a design issue)....
well well well, we've moved from EJB to databases to design issues!!! reminds me of a quote...
Just when you think you've figured it out, somebody comes along and changes the questions!!
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vish Kumar:
Related to the discussion, I have another question.
I always assumed that Empty strings and NULL values are treated the same in Oracle, especially when used in comparison statements in PL/SQL.
[ January 22, 2004: Message edited by: Vish Kumar ]

Vish is right
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!