Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 946
lincolnk Avatar asked Jul 28 '10 19:07

lincolnk


1 Answers

I usually try to return a complex object and fall back to using an out parameter when I have to.

But you look at the TryParse methods in .NET's conversions, and they follow the pattern of returning a bool and an out parameter of the converted value. So, I don't think it's bad to have out parameters - it really depends on what you're trying to do.

like image 200
David Hoerster Avatar answered Oct 14 '22 02:10

David Hoerster