File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Correct position of return type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Correct position of return type?" Watch "Correct position of return type?" New topic

Correct position of return type?

Gavi Raaghav
Ranch Hand

Joined: Apr 28, 2005
Posts: 82
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.

return ((AgendaServiceEJB) this.ejbSession).getMeetingAgenda(
catch (RemoteException remoteException)
throw new ProblemComunicatingWithEJBException(
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
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.
I agree. Here's the link:
subject: Correct position of return type?
It's not a secret anymore!