User: Password:
|
|
Subscribe / Log in / New account

Removing uninitialized_var()

Removing uninitialized_var()

Posted Dec 21, 2012 5:32 UTC (Fri) by dvdeug (subscriber, #10998)
In reply to: Removing uninitialized_var() by giraffedata
Parent article: Removing uninitialized_var()

Anytime this comes up, the code is hard to read. Variables that don't have valid values in chunks of code are problematic, and GCC wouldn't issue this error in a case where there's no branching or other stuff to confuse the issue. If you can't rearrange it so that it's clear to GCC that it's set before used, whether you initialize it isn't going to make it significantly more unclear to readers.


(Log in to post comments)

Removing uninitialized_var()

Posted Dec 21, 2012 9:02 UTC (Fri) by hummassa (subscriber, #307) [Link]

My response was going to be similar; "if you managed to confuse the compiler, you lost your human readers a long time ago."

Removing uninitialized_var()

Posted Dec 21, 2012 17:58 UTC (Fri) by bronson (subscriber, #4806) [Link]

Yes, well said! That's worth hanging on the wall.

Removing uninitialized_var()

Posted Dec 21, 2012 20:14 UTC (Fri) by giraffedata (subscriber, #1954) [Link]

As the article points out, it's amazingly easy to confuse the compiler. In the cases where I've had to deal with this, there was clearly no practical alternative way to write the code. The only practical ways to shut the compiler up were to give a variable a fake value or disable the warning class.

I do agree that code with variables that are sometimes meaningless is harder to read than code without. I don't agree that when such meaningless variables exist, the code is equally hard to read when you assign values to them.

Here's the analogy: You're filling in an online medical history form. Question 1: sex. Question 2: if female, when was your last menstruation? No doctor will be the least bit concerned when he sees a man leave Question 2 blank. He might be confused if he sees a date in there for a man. In fact, he would be well justified in thinking Question 1 might be an error in that case. GCC is the automatic field checker that notices Question 2 is blank and makes the man put a date in there before it will accept the form, because it isn't smart enough to know what menstruation is and simply insists that every field be filled in, to avoid accidental omissions.

Removing uninitialized_var()

Posted Dec 21, 2012 21:22 UTC (Fri) by hummassa (subscriber, #307) [Link]

My point is that, as easy as it is to confuse gcc or clang, confusong the programmer who has to read the code after you is even easier! Let is substitute your question #2 for "did you ever have had eclampsia?" And now the comparison is fair. 50 percent of male respondents will not know what it is and many will answer wrongly.

Removing uninitialized_var()

Posted Dec 21, 2012 23:13 UTC (Fri) by apoelstra (subscriber, #75205) [Link]

> My point is that, as easy as it is to confuse gcc or clang, confusong the programmer who has to read the code after you is even easier!

This is not always true. For a small-enough function (or small-enough block), a programmer can probably build a mental parse tree just as well as the compiler can -- and then, because he is a human gifted with all manner of high-level knowledge and context, he can easily see things that the compiler might miss.

And in my experience, "small enough" includes many non-trivial functions of 5-10 lines. Also in my experience, these are the sorts of functions that trigger uninitialized variable warnings.


Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds