Hacker News new | ask | show | jobs
by sph 1482 days ago
This is one recent change in Zig which really annoys me: unused variables are now errors. Languages that complain every time there's a unused variable become useless during experimentation and quick hacking. They turn my hyperfocus into a death from a thousand paper cuts.

I hate it with a passion. Please respect my mental flow, if I'm trying some ideas out, it's just rude to stop me in my tracks to tell me I forgot to comment out a variable. Who cares.

I will fight anyone who thinks this is a good idea.

1 comments

I have to agree with you. Zig already has a Debug build mode, they can just warn if you are running Debug build.
For a variety of reasons, Zig doesn't do warnings.

Personally I don't get the big deal with unused variables being errors, from either direction. I'm not sure what it accomplishes to make them errors and I'm not sure why people complain so much about having to comment them out.

If you comment out a statement, you cannot know for sure how many unused variables this caused unless you visually scan the entire previous code, which can cost time.

So the only way to find out is to compile and then have the compiler tell you that it refuses to continue because there's an unused variable at line X.

So now you needed to do not one but 2 compiles, to do something that should have taken 1 compile, which also costs time.

But it's then also possible that due to commenting out line X, there's now yet another unused variable (or more) somewhere. Etc... (Unless the language offers a way to fake-use the variable, like (void) cast in C++. Then at least you only have one wasted compile and not recursively more)

And then if you want to enable the original line that you commented out again, you need to also remember to uncomment out those other lines.

Repeat this process many times a day when really in progress of developing something, and due to the total time it costs for this silliness it's just a bad tool, not a good tool for efficient programming in the flow.

The goal of the unused variable as error is to prevent bad submitted code, but that should not cost you time during development. Only enforce that check at the end, not during.

I think that one thing that we really need to revisit in PLs is sorting out different kinds of comments. Like, in most languages, a comment is just whitespace, with no semantic distinctions. More recently, docstrings got some special handling. But what's really needed is a kind of comment that's specifically about commenting out code; ideally, working on syntax tree level (so you can comment out one entire {...} block, say), and with the compiler being aware that the stuff inside is code, and handling it accordingly for purposes such as these.
> Unless the language offers a way to fake-use the variable

Zig actually does have this:

  _ = my_variable;
Yeah, basically what the other commenter wrote - it requires potentially many changes recursively.