Home All Groups Group Topic Archive Search About

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

Author
30 Jun 2006 10:04 AM
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

Author
30 Jun 2006 10:36 AM
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


--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Author
30 Jun 2006 11:18 AM
Patrice
My personal preference would be to return an image and use Exceptions to
signal an error...

--
Patrice

"John Dann" <n***@prodata.co.uk> a écrit dans le message de news:
1rs9a21dun7d1sh90fa4ju6l9vnok7m***@4ax.com...
Show quoteHide quote
>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
Author
30 Jun 2006 6:24 PM
iwdu15
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
--
-iwdu15