[comp.arch] forced declare-time initialization considered useless

andrew@frip.WV.TEK.COM (Andrew Klossner) (09/12/89)

>> What if you don't know what value it should have yet?

> You can create the variable with a default value, or delay the creation of
> the variable until you know what value it should have.

Creating the variable with a default value is useless, as has already
been pointed out.

You can't always delay declaration of the variable until its value is
known, because there may be more than one path which determines a
variable's initial value.  Embellishing your example:

main() {
	char input[MAX_LINE_LENGTH];
	int doors;

	printf("How many doors?\n");
	gets(input);

	if (strcmp(input, "two")==0) {
		doors = 2;
	} else if (strcmp(input, "four")==0) {
		doors = 4;
	} else {
		printf("we don't make %s door cars\n", input);
	}
}

Here, you can't combine the declaration of variable "doors" with its
initialization because there is more than one point of initialization.
You could screw around to, e.g., make a function that performed some
computation and returned an initial value, but that's an inordinate
amount of contortion.

In general, a language which requires that each variable declaration
include an initial value adds nothing to a coder's ability to produce
correct programs.  By contrast, a utility or compiler which examines a
program and informs the programmer of possibly uninitialized variables
is worthwhile.

  -=- Andrew Klossner   (uunet!tektronix!frip.WV.TEK!andrew)    [UUCP]
                        (andrew%frip.wv.tek.com@relay.cs.net)   [ARPA]