I -sometimes- use out parameters, the method sometimes is complicated, and contain loops and conditional blocks, In most cases the compiler gives me this error
"The out parameter 'xxx' must be assigned to before control leaves the current method"
So, I find my self always start the method with nullifying all out parameters, I need to know, is it a good practice?
* EDIT *
I mean assign the default value by the word "Nullify"!
I wouldn't, as leaving them uninitialised will prompt you to set appropriate values in all code branches (as a compiler error), rather than forgetting and leaving the null/etc. If, however, you have multiple code paths and all but one of them use the same default value (like a TryGet / TryParse method), then it might be pragmatic to assign the default at the start, so there is only one other assignment.
Depends on the meaning you apply to the null parameters.
Normally null means undefined, unknown value, which could not be retrieved by some error. When you encounter null value in one of your parameters it is usualy not normal program flow.
Thus, it is better to define set of uniform default values whose meaning will be ready to be filled and nullify them if something unexpected occurs.
Of course all of this depends on the projects and present standarts of codding and naming
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With