Skip to content

Comment on Playing whack-a-mole is losingparent

Comments

Rather than this being too good at solving this class of problem, it seems to me that zero-initialization is the wrong approach

That's exactly why I wrote this here:

> I'm obviously not saying we should write unsafe code or that we shouldn't try to eliminate entire classes of bugs, but that HOW we do it matters.

After you get past the hurdle of noticing this problem (which, as you saw, is very much not obvious), the harder question becomes: what is the right approach?

In this particular case it's not too hard to think of a better approach once you concede the obvious solution isn't so great, but in other cases it is, and often the better alternatives put some kind of selection pressure too... just less frequently. And even in this case, it's not at all obvious that this approach is bad - plenty of people think it's better to force a default value you can rely on, and they want to remove undefined behavior from C++ by forcing initialization on everything. For longstanding examples elsewhere, just look at how Java and C# initialize fields, for example.

Outside of programming it's even harder to notice and find a better alternative, but selection pressure has these kinds of effects in other areas too.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.