7 ms·
I would argue your insistence on giving the variable a "sane default" is a matter of opinion. I personally am much happier seeing a variable left uninitialized
by saurik 12y ago
I would argue your insistence on giving the variable a "sane default" is a matter of opinion. I personally am much happier seeing a variable left uninitialized than ever seeing it double-assigned, as to me a fundamental semantic logic error has occurred if you are overriding the value of this kind of variable, especially as part of a conditional later where it might not be clear whether or not it happened: I really would prefer to look at the variable being assigned and think "ok, now we know the value". The compiler is entirely able to determine whether or not the variable has been initialized in all possible control flows for a case like this (at least, once we move the scope of the variable to inside of the for loop, of course), so there is no safety benefit to hoisting that default assignment out, nor is it saving a meaningful amount of code: it only serves to obfuscate the "default" control flow by making what had been an explicit if/else into an implicit if/default :/.
- pierrebai 12y agoAlways initialize your variable. There is not aesthetic argument, but there are multiple software engineering argument against failing to initialize variables. For starter, you don't have prescience. You can't prevent future version from growing new alternate code paths. As the code evolve, checking that every initialization-free variable will still get initialized is a burden and easily overlooked. Then there are more potent evolution that will make it just impossible: the code will get generalized, or moved into a library, or get a a context object to be able to run multiple instances. Or will get a state machine to handle complex combination of options. You're relying on the compiler telling you the variable will get initialized. But the compiler may change. Or the human compiling it might not look at warnings. OTOH, double-iniitalization is mostly a myth. If the compiler can tell the variable may not get any initialization and warn you are exactly the same as the case the optimizer will be able to see the double init and optimize it out. I've looked at generated code and see compilers properly eliminate redundant stores. You're better off betting on optimization and get bitten by occasional double-stores than get bitten by uninitialized variables and give system penetrators yet another free lunch.
- dllthomas 12y agoIf you use static analysis, defensively initializing to a wrong value may mean that your tooling doesn't tell you when you fail to correctly set it from that wrong value.
- kazinator 12y agoI have seen this argument numerous times before. It doesn't hold water, because potentially initializing variables to wrong values is just as much as consequence of using that static tool, as it is a consequence of an "always initialize" coding habit. The static tool produces a report of uninitialized variables or potentially uninitialized ones. The developers react to this by fixing all those instances, so that the tool is "shut up". Once that happens, they have a code base for which the tool doesn't report anything, just like the other developers who always initialize. There is no reason to believe that either team has a lower or higher rate of incorrect-initial-value bugs. Or is there? I would argue that the developer who is writing the code, at that moment, quite possibly has a better idea of what the correct initial value should be, than someone who is running a tool across a large code base and following up (by him or herself). Really, the coding convention should be "always initialize, with the correct value which doesn't need to be overwritten later." Even in imperative languages, we should program as "functionally" as is reasonably possible. Imperative code that mutates local variables should only do so if it implements iteration. If the code has no loops and is setting local variables conditionally, that shows that it's trying to influence the behavior of some common code where the control flow paths merge --- instead of putting it into a function! In the specific program we are looking at, for instance, the function is trying to use the head processing block at the end, but over different input streams. This can be done by putting it into a function, so then there is no assignment to the fp variable: if (!*argv) { head_processing(stdin); } else { FILE *fp = fopen(*argv, "r"); /* check for fopen failure ... */ head_processing(fp); fclose(fp); } We can eliminate assignments out of loops, too, but that requires tail recursion: /* pseudo code */ head(int argc, char **argv, int first_time) { if (argc == 0 && first_time) { return head_processing(stdin); } else if (argc == 0) { return 0; } else { FILE *fp = fopen(*argv, "r"); /* head_processing checks for null */ int status = head_processing(fp); fclose(fp); /* tail call, if status was good */ return (status != 0) ? status : head(argc - 1, argv + 1, 0); } } C programmers will tend not to be comfortable with this style, and it requires tail call support in the compiler to not generate O(N) stack. So for pragmatic reasons, you can't make a coding convention out of this.
- deleted 12y ago[deleted]