3 ms·
But that's runtime expensive, or else very restrictive on the compiler. Bad data should fail fast and null is good at doing that.
by LorenPechtel 4y ago
But that's runtime expensive, or else very restrictive on the compiler.
Bad data should fail fast and null is good at doing that.
- pjmlp 4y agoIt is done at compile time. For example in C#, you would get something like "Use of unassigned local variable xyz". If the performance cost of initilizing the variable is such an unberable cost, there is [SkipLocalsInit].
- LorenPechtel 4y agoWhich works in *most* cases. I forget the details but I do recall running into a case where it was griping erroneously (it couldn't reach the use without having reached the initialization) so I gave it a harmless initial value--and it turned around and complained (correctly) that the assignment would never be used. I insist on removing all such warnings so I ended up restructuring the code so the compiler wouldn't get confused. (Fundamentally, this was a case of a child operation that *might* be needed. It could have been rewritten polymorphically but that would have involved a fair amount of refactoring.)
- pjmlp 4y agoDeclare the variable closer to its definition point. Also such kind of situations are usually a sign that code blocks should be moved into a separate function.