Alex Rivera | Logout

is it better practice to return a complex object or use reference/out parameters?

Asked 2010-07-28T19:59:14.033
17

I'm putting together a method which is supposed to evaluate the input and return true if all conditions are met or false if some test fails. I'd also like to have some sort of status message available to the caller if there's a failure.

Designs I've come across include returning the bool and using an out (or ref) parameter for the message, returning an instance of a (specifically designed) class with the bool and string properties, or even returning an enum indicating pass or a specific error. what's the best way to get all the information out of the method? Are any of these "good"? Does anyone have other recommendations?

Edit
Report

2 Answers

3

Returning objects is more readable and takes less code. There are no performance differences, except for the your own while you're jumping through the hoops "out parameters" require.

answered 2010-07-28T20:06:26.417
2

I guess it depends on your definition of good.

For me, I'd prefer returning the bool with the out parameter. I think it's more readable. Seoncd to that (and better in some instances) is the Enum. As a personal choice, I tend not to like returning a class just for message data; it abstracts what I'm doing just one level too far away for my tastes.

answered 2010-07-28T20:03:00.293

Your Answer