Why are uninitialized variables and return values the default?

If I think about the types of bugs that cause me the most pain and the most difficult and random errors, uninitialized variables and return values on functions have to be up there.

Now I understand there are ways to catch these problems, but the whole ideal of modern languages is to try and help you write quality code fast. For me, the use case for needing uninitialized variables or any claimed ‘performance gain’ of uninitialized variables is immaterial compared with the number of serious faults this default causes.

So I’m wondering, why can’t we either have a compiler switch (maybe there is one?) or a new default that means that all function variables, function return values and inline variables auto initialize to their default i.e. empty string, 0, false, nil etc and if you have a use case for some extreme performance need you can disable this feature either project wide, unit wide or per function.

This change alone would surely reduce bugs for everyone.

Can anyone think of a reason this would actually be a bad idea in any real world situation?

Whilst there are compiler warnings about unitialized variables etc, they fall short of catching everything.

The next best option is static code analysis like TMS Fix Insight or Pascal Analyzer. Both of those tools have caught bugs for me that were lurking in the background waiting to happen.

3 Likes