Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to nullify all out parameters always at the beginning of the method

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"!

like image 344
Sawan Avatar asked Dec 04 '25 11:12

Sawan


2 Answers

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.

like image 103
Marc Gravell Avatar answered Dec 06 '25 00:12

Marc Gravell


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

like image 29
Nogard Avatar answered Dec 06 '25 01:12

Nogard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!