What type is best for the return value of a function?

J

John Dann

A question that I'm in two minds about - maybe someone more
experienced could offer some advice?

Let's say I have a method in a class whose function is to generate an
image (But it could equally apply to any more complex object that the
method might generate.)

Then it seems to me that I have two options:

1. Set the image as the return value for the function.

2. Have the function update a class property with the new image and
return eg an integer value to denote success or otherwise of the call
to the function.

I'm kind of leaning towards the second option. Although it seems less
elegant and less concise, using an integer as the return value does
give me the option to test the call for different types of errors that
might have occurred in generating the image and then take appropriate
action.

Anyone care to comment on what might be reckoned to be the better
programming practice?

JGD
 
P

Peter Macej

1. Set the image as the return value for the function.
I would do this and in addition I would throw an exception from this
function in the case of any error. You can define separate exception for
each kind of error.
2. Have the function update a class property with the new image and
return eg an integer value to denote success or otherwise of the call
to the function.

If you don't want to use exception, use class or structure as return
value. The class can contain image and integer, e.g.:
Class MyImage
Public img as Image
Public errorCode as Integer
End class
 
P

Patrice

My personal preference would be to return an image and use Exceptions to
signal an error...
 
G

Guest

im in full agreement that you should use a return value from a function. this
makes the code alot more readable without jumping around too much. also
easier to maintain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top