Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Thanks for explaining. I guess that makes sense, but it surprises me: language implementors choosing to do the slightly worse option not due to technical limitations or historical baggage, but just because it's easier. I thought only C was like that. That's not to insult any of them; it's not like I'm the maintainer of a compiler for a major programming language.


I mean, not having uninitialized variables is simpler to understand, as a user. It’s not inherently about ease of implementation.

Furthermore, language designers are also human. Many of the languages used in industry weren’t even created by people who have a PLT background, or were created a very long time ago.


I think it depends. Not having uninitialized variables by requiring the user to provide some value, as in expression-oriented languages, makes sense to me, because it wouldn't make sense in an expression for a variable not to have a value. An example of SML:

  1 + let val x = 1 in x * 2 end
That's both conceptually simpler and seems like one of the good solutions.

On the other hand, in languages where you don't have to explicitly initalize variables after declaring them, I think the best solution is to do flow analysis and forbid uses of uninitialized variables rather than implicitly set them all to null or zero. The latter solution just leads to confusing bugs if you ever don't initialize a variable in all branches (which is why I think that should be a compiler error). It's only a little better than C's solution of just letting the value be undefined, or in practice, a non-deterministic garbage value. An error message just seems more user-friendly, particularly for beginner programmers who are more likely to make mistakes like those and not immediately understand what the problem is.

I do get what you're saying about it being simpler, but I don't think it's conceptually simpler for the user to always initialize variables to zero or null.


> On the other hand, in languages where you don't have to explicitly initalize variables after declaring them, I think the best solution is

I agree, for sure.

> I don't think it's conceptually simpler for the user to always initialize variables to zero or null.

I'm not saying zero, I'm just saying null. Because null is a valid value for that type, it's equivalent to

> Not having uninitialized variables by requiring the user to provide some value


> I'm not saying zero, I'm just saying null.

What would happent to the primitive types of that language, then? When I said that, I was thinking of languages like Go and Java, both of which implicitly initialize integers to 0, booleans to false, floats to 0.0, and references to nil or null. That's what I'm calling less user-friendly than disallowing variables that aren't explicitly initialized before being used in all branches.


> What would happent to the primitive types of that language, then?

Not every language has them. A common example here is JavaScript, which, while `undefined` is a thing in, is closer to null than the C sense of undefined.

> When I said that, I was thinking of languages like Go and Java, both of which implicitly initialize integers to 0, booleans to false, floats to 0.0, and references to nil or null.

Right, that's zero initialization. I also agree that I think it's a mis-feature generally, but I think in the overall design space there's reasons you'd end up there. It's not the path I'd personally take, but I can see why people choose it, even if I disagree.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: