• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Correct position of return type?

 
Gavi Raaghav
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the return position in the following piece od code correct or should it be after the catch block? If i put the return type at the end then i need to create an object for the same.

try
{
return ((AgendaServiceEJB) this.ejbSession).getMeetingAgenda(
aMeetingCriteria);
}
catch (RemoteException remoteException)
{
throw new ProblemComunicatingWithEJBException(
CLASS_NAME,
"getMeetingAgenda",
remoteException.getMessage(),
remoteException);
}
 
Rob Acraman
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gavi,

Depends on what you mean by "Correct" :
- If you mean, does it work?, then presumably you've tested it and it's doing what you want.
- If you mean is this the best way of laying out the code, then I think the fact that your gut feeling has led you to post here is its own answer

You have one statement doing three things :
- Casting this.ejbSession
- Calling a function
- Doing a return

Generally, you want each statement to do just one thing, so this statement should be split.

First off, why do you need the cast? What type is "this.ejbSession", and could it instead be declared as AgendaServiceEJB? If not, you could always declare a local variable.

So, how about something like :



One last point: You say "you need to create an object". Not true - the code above does not create any more objects than your original. All you've created is a local REFERENCE to the agenda.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic