S
Sankar Nemani
When I am designing a method that returns a value of a certain type (say Type1) and then occassionally has to return value of certian other type (say Type2), I can design the method in the following ways (I am not sure if there are more ways).
1.. Type1 foo(ref Type2 x){ //populate x occassionally}
2.. Type1 foo(){//throw an instance of Type2 instead of populating a ref parameter}
3.. Type3 foo(){} //where Type3 is a class that encapsulates a variable of Type1 and a variable of Type2
Which one is better or more elegant and why?
My initial take on this considering that the exception handling in .NET is expensive is that,
If returning a Type2 is very rare then implement use method 2 (using exception handling) and otherwise use method 3.
Especially languages such as Java do not have pass by ref (they do have reference types pass by val) so I was wondering if method 1 is less elegant or less preferred as far as good design goes.
TIA
Sankar Nemani
1.. Type1 foo(ref Type2 x){ //populate x occassionally}
2.. Type1 foo(){//throw an instance of Type2 instead of populating a ref parameter}
3.. Type3 foo(){} //where Type3 is a class that encapsulates a variable of Type1 and a variable of Type2
Which one is better or more elegant and why?
My initial take on this considering that the exception handling in .NET is expensive is that,
If returning a Type2 is very rare then implement use method 2 (using exception handling) and otherwise use method 3.
Especially languages such as Java do not have pass by ref (they do have reference types pass by val) so I was wondering if method 1 is less elegant or less preferred as far as good design goes.
TIA
Sankar Nemani