Andrew wants to ban tabs again?

It all started a couple months ago when I found a // TODO delete hard tabs from the language in the code. I of course immediately filed a PR to remove it (I say that jokingly, the comment was placed in the stack unwinding code, which needs to handle tabs as they are allowed in other language like C/C++ which may be in the callstack. This is what the PR actually addresses), but it’s been stuck in the pile of PRs waiting for review so I never heard back on it.

Of course a TODO comment in the code is not a commitment to anything.

Now just a few minutes ago, I saw a new comment from andrew:

  1. hard tabs become errors again, but fixed by zig fmt

I’m using tabs in my codebase, and there are many reason to do so, some objective, many subjective. Surely I’m not the only one, and this was a big drama pre-0.10.0. I don’t want history to repeat itself here.

I can get behind many decisions that involve having a uniform style and making code more readable, but in my opinion this goes to far. It’s not even providing any visible or function change to the code.

And it’s a futile attempt anyways. If this lands, I for my part will do what I have done in 0.9.0: Remove tabs in a build step before passing source files to the compiler

Am I the only one here who uses (or wants to use) tabs in Zig? How many language users are we going to lose over a new tabs vs spaces war? Is this really worth it?

16 Likes

I use tabs for the (commonly cited) reason that being able to adjust the size of indentation helps visually.

I also don’t use zig fmt either though. If I was contributing to a project that wanted it sure, but for my personal stuff I like being in control. That’s partly why I like systems programming. :wink:

5 Likes

I use and like the autoformatter. Also I always used spaces.
If the formatter does something I don’t like I deactivate it for a few lines and do manual formatting with spaces.

With https://codeberg.org/ziglang/zig/issues/37373 you might be using 30% fewer tabs anyway :wink:

8 Likes

If they want to touch the formatter, I think there’s some more pressing changes, that would be neat.
First it would be cool, if zig forced mandatory {``} around if, else, for, while, switch (except a few special cases like for assigning values), and it would be even cooler if zig could enforce 1 line of space, between blocks of { } to give an example.

        pub fn jsonParseFromValue(allocator: Allocator, source: Value, options: ParseOptions) !@This() {
            if (source != .object) return error.UnexpectedToken;

            var map: std.StringArrayHashMapUnmanaged(T) = .empty;
            errdefer map.deinit(allocator);

            var it = source.object.iterator();
            while (it.next()) |kv| {
                try map.put(allocator, kv.key_ptr.*, try innerParseFromValue(T, allocator, kv.value_ptr.*, options));
            }
            return .{ .map = map };
        }

would become:

        pub fn jsonParseFromValue(allocator: Allocator, source: Value, options: ParseOptions) !@This() {
            if (source != .object) {
                return error.UnexpectedToken;
            }

            var map: std.StringArrayHashMapUnmanaged(T) = .empty;
            errdefer map.deinit(allocator);

            var it = source.object.iterator();
            while (it.next()) |kv| {
                try map.put(allocator, kv.key_ptr.*, try innerParseFromValue(
                    T,
                    allocator,
                    kv.value_ptr.*,
                    options,
                ));
            }

            return .{
                .map = map,
            };
        }

I know this is very very high level bike shedding, but I really don’t like one liners, and I don’t like 300 characters lines, and I always prefer the , forcing a new line, rather than one liners, and I prefer space between things.

Now for the “objective” reasons, is that space helps read things better, it helps those who have trouble reading, because the distinction is clearer.

I know this will never, happen, but I’m this close to make a formatter extension for this, since this is totally compatible with current zig formatting rules, and maybe that’s the way to go.

6 Likes

I like one-liners.

14 Likes

I know a lot of people like them which is why I’m fine with writing my own formatter for this, it’s a few lines more than zig fmt. But I genuinely find it harder to read one-liners but to each their own :slight_smile:

3 Likes

to each their own

Hear hear :slight_smile:
So about those tabs…

I also like one liners, for what it’s worth. (Maybe I just like unpopular stylistic choices? I can live with that.)

go fmt uses Tabs, so it might not be as unpopular as you might think (if we talk about programming in general).

6 Likes

About the one-liners.
Yeah I also like one-liners. Very much actually.
If they get too long or too complicated, I use some helper var, like for the above example:

 while (it.next()) |kv| {
    try map.put(allocator, kv.key_ptr.*, try innerParseFromValue(
        T,
        allocator,
        kv.value_ptr.*,
        options,
    ));
}

I use:

 while (it.next()) |kv| {
    const xxx = try innerParseFromValue(T, allocator, kv.value_ptr.*, options);
    try map.put(allocator, kv.key_ptr.*, xxx);
}

which is much shorter and (imho) more readable.

About the tabs: no real opinion. A bit crazy forbidding tab in syntax or comments but I can live with it.

2 Likes

Tabs go bad when they are used to align code, rather than indent it, and when mixed with spaces.

So if you’re going to use them:

  • Only ever at the start of a line.
  • The number of them only changes when there’s a scope level change.
  • Everything else uses spaces (e.g. line continuation).
  • No mixed mode (tabs / spaces) files.

Otherwise tabs can take a long run off a short pier IMHO.

2 Likes

Isn’t a “one-liner” more like:

    // One-liner
    if (blah) x += 1;

    // Naked statement
    if (blah)
        x += 1;

    // Full block
    if (blah) {
        x += 1;
    }
1 Like

I am one of the persons who uses tabs for indentation instead of spaces. It’s really annoying that it’s disallowed in comments, wish they would remove that limitation instead. It makes as little sense as not allowing crlf as line endings(Which luckily is allowed nowadays).

Also not a user of zigfmt or any other formatters in other languages on my own projects, as I really dislike the formatting, making code harder to read for me.

4 Likes

same here i have zig fmt turned off because i don’t like 4 spaces indenting it’s to wastefull in my opinion especially when the language is so verbose and i prefer 2 so i just use the mint formatter

1 Like

I don’t use tabs and I don’t want any ASCII control characters sans LF anywhere near my source code, but I still think a compiler-level hardline stance that makes tabs a compile error is unproductive (zig fmt replacing them is fine, because it’s an optional component). Same with banning CRLF or UTF-8 BOMs. It makes the language as a whole less compatible/interoperable with different editors and tooling; it becomes more cumbersome to use primitive editors like Windows Notepad for quick edits because you can’t use the tab key on your keyboard, or to use e.g. stock PowerShell pipelines to generate/transform code without postprocessing the output.

I think you can make a good technical argument for banning tabs in certain contexts where they might mislead, obfuscate author intent or be indicative of a bug, such as inside a string literal like "a<tab>b" where depending on the editor and where the literal is positioned in relation to the next tab stop it could look like it contains anywhere between 1 and 7 space characters. But that’s a pretty separate case from tabs at the start of the line (or at the start of comments).

17 Likes

agree, even if you are working on different platforms this can be a problem. On the other hand, I understand everyone who would like to keep that to themselves. Personally, I automatically format TABS to Spaces in Neovim:

vim.opt.expandtab = true
vim.api.nvim_create_autocmd("BufReadPost", {
    pattern = "*",
    command = "retab",
})

Perhaps proponents of TABS could do the same in reverse?

1 Like

I always thought that surely this must have been a move to troll everybody.

I don’t really care about tabs/spaces, but to me banning tabs seems to make as much sense as banning unformatted code.

5 Likes

I don’t like the title of this thread, way to personal. Zig is more then Andrew nowadays.

1 Like

On this point I think there’s a clear cut reason to need to ban CR in Zig, I happened to just have thought about it in the contest of Ziggy, as I’m polishing how strings work, and I copied Zig’s multiline string literals.

If you have multiline string literals, allowing CR at the end of a line will cause different users to inadvertently create different string literals depending on the platform/editor config they’re using, plus all the weird bullshit git does when the autocrlf box is left checked on windows.

In concrete terms:

Alice (on Linux):

\\foo<LF>
\\bar<LF>

Bob (using Microsoft Notepad):

\\foo<CR><LF>
\\bar<CR><LF>

I think it’s reasonable to consider it a footgun if two users type the same thing and inadvertently end up with different string literals. Languages with only quoted strings don’t incur in this problem.

You could then argue that one could disallow them in those literals, but still allow them in the rest of the source code, but IMO at that point you’ve already lost the “don’t have to even think about it” factor and you’re just making things even more complicated for no ultimate gain.

EDIT: just for extra context, I’m going to ban CR in multiline literals, but I will allow it elsewhere in order to guarantee better JSON backwards compatibility (Ziggy is a superset of JSON).

9 Likes