News for hackers. Stuff that matters.
Re:What's the deal with tab size (8 spaces) in the GitHub UI? by kazinator on Wednesday September 22, 2021 @04:21PM (#28621252)
> there is no reason to not just enforce using tabs for indentation since they are objectively superior.

I just demonstrated one. The semantics of tabs is that they move the printing position to the next tab stop position, which is absolutely determined, rather than relatively.

This does not work for nested indentation in modern (or even not-so-modern) programming languages.

If we re-imagine the tab as simply being a macro which expands to a configurable number of spaces, then tabs could be objectively superior.

It makes sense to advocate for that, and I'm all for it; but I don't place a high probability on it happening. Though a simple idea, it would have to be in every editor, and other tools.

You almost want to re-purpose some other control character for this purpose and leave tab alone.

About:

  void foo(arg, arg
           arg, arg)
  {
      ...
  }
Note that editor support this, so the only keystrokes you need to enter this from scratch, for instance in Vim, are:

  void foo(arg, arg[Enter]arg, arg)[Enter]{[Enter]...[Enter]}
The important attribute here is that the parameter list, which is an independent element of the phrase structure of the language, is presented in such a way that a bounding rectangle can be imagined around it. That rectangle includes only tokens from that element, and not from any other element:

          +----------+ 
  void foo|(arg, arg |
          | arg, arg)|
          +-----------+
  {
      ...
  }
Boxes help readability; that's why we use tables and grids for presenting information or laying out user interfaces.

Ideally, this principle would be applied to every syntactic element.

Looking at complex examples of Lisp code, I can't find a single counter-example: every sub-expression can be boxed in a rectangle which includes only that expression and its constituents: modulo a few closing parens that are written out to the right: )))). A big example of this is given below.

Now in the descendants of C syntax, the predominant brace placements throw a monkey wrench into this idea. We usually cannot draw a bounding box around a multi-line compound statement or other braced element such that the braces are included, and no tokens of neighboring constructs are included.

About that other style, for consistency, I would want this:

  void foo(
      arg, arg, 
      arg, arg
  )
  {
      ...
  }
if the opening parenthesis is not on the same line as the arguments, the closing one should not be either.

I work in a shop where the style closely resembles this one, except the parameter declarations are on separate lines:

  int put_bytes(
      int    fd,       // comment
      char  *buffer,   // comment
      size_t size,     // comment
  )
  {
  }
Now I personally don't like this, but I can work with it, and can see the advantages. If you have to add, remove or reorder arguments you are not fiddling with the parentheses at all. When you take a unified diff of the edit, it does not include lines that only differ because of a parenthesis. The lines marked + or - are all "payload". The inner alignment of the parameter names is helpful, as is having the space for commenting the parameters.

I still wouldn't do this in a side project where I dictate the coding convention, but I can respect that and work with it without grumbling.

Now about that bounding box thing, here is a random example I picked from the SBCL compiler sources. The file is:

https://github.com/sbcl/sbcl/blob/master/src/compiler/srctra...

The macro bound-binop, with some ASCII rectangles inserted, looks like this.

Only the closing parentheses at the end violate the principle: the rectangle includes more parentheses than are strictly part of that syntax, and this problem is minimized by closing the parentheses together on the same line.

I suspect that the programmers who work with Lisp and find it readable all grok this bounding box model, whether consciouosly or not.

  (defmacro bound-binop (op x y)
  +-----------------------------------------------------------------------------------------------+
  | (with-unique-names (xb yb res)                                                                |
  | +-------------------------------------------------------------------------------------------+ |
  | | `(and ,x ,y                                                                               | |
  | |     +-----------------------------------------------------------------------------------+ | |
  | |     | (with-float-traps-masked (:underflow :overflow :inexact :divide-by-zero)          | | |
  | |     | +-------------------------------------------------------------------------------+ | | |
  | |     | |       +-------------------------------------+                                 | | | |
  | |     | | (let* |((,xb (type-bound-number ,x))        |                                 | | | |
  | |     | |       | (,yb (type-bound-number ,y))        |                                 | | | |
  | |     | |       | (,res (safely-binop ,op ,xb ,yb)))  |                                 | | | |
  | |     | |       +-------------------------------------+                                 | | | |
  | |     | | +---------------------------------------------------------------------------+ | | | |
  | |     | | | (set-bound ,res                                                           | | | | |
  | |     | | |          +--------------------------------------------------------------+ | | | | |
  | |     | | |          |      +----------------------------+                          | | | | | |
  | |     | | |          | (and |(or (consp ,x) (consp ,y))  |                          | | | | | |
  | |     | | |          |      +----------------------------+                          | | | | | |
  | |     | | |          |       ;; Open bounds can very easily be messed up            | | | | | |
  | |     | | |          |       ;; by FP rounding, so take care here.                  | | | | | |
  | |     | | |          |      +-----------------------------------------------------+ | | | | | |
  | |     | | |          |      |,(ecase op                                           | | | | | | |
  | |     | | |          |      | +-------------------------------------------------+ | | | | | | |
  | |     | | |          |      | | (sb-xc:*                                        | | | | | | | |
  | |     | | |          |      | |  ;; Multiplying a greater-than-zero with        | | | | | | | |
  | |     | | |          |      | |  ;; less than one can round to zero.            | | | | | | | |
  | |     | | |          |      | |  `(or (not (fp-zero-p ,res))                    | | | | | | | |
  | |     | | |          |      | |       (cond ((and (consp ,x) (fp-zero-p ,xb))   | | | | | | | |
  | |     | | |          |      | |              (>= (abs ,yb) 1))                  | | | | | | | |
  | |     | | |          |      | |             ((and (consp ,y) (fp-zero-p ,yb))   | | | | | | | |
  | |     | | |          |      | |              (>= (abs ,xb) 1)))))               | | | | | | | |
  | |     | | |          |      | +-------------------------------------------------+ | | | | | | |
  | |     | | |          |      |                                                     | | | | | | |
  | |     | | |          |      | +-------------------------------------------------+ | | | | | | |
  | |     | | |          |      | | (sb-xc:/                                        | | | | | | | |
  | |     | | |          |      | |  ;; Dividing a greater-than-zero with           | | | | | | | |
  | |     | | |          |      | |  ;; greater than one can round to zero.         | | | | | | | |
  | |     | | |          |      | |  `(or (not (fp-zero-p ,res))                    | | | | | | | |
  | |     | | |          |      | |       (cond ((and (consp ,x) (fp-zero-p ,xb))   | | | | | | | |
  | |     | | |          |      | |              (<= (abs ,yb) 1))                  | | | | | | | |
  | |     | | |          |      | |             ((and (consp ,y) (fp-zero-p ,yb))   | | | | | | | |
  | |     | | |          |      | |              (<= (abs ,xb) 1)))))               | | | | | | | |
  | |     | | |          |      | +-------------------------------------------------+ | | | | | | |
  | |     | | |          |      |                                                     | | | | | | |
  | |     | | |          |      | +-------------------------------------------------+ | | | | | | |
  | |     | | |          |      | | ((sb-xc:+ sb-xc:-)                              | | | | | | | |
  | |     | | |          |      | |  ;; Adding or subtracting greater-than-zero     | | | | | | | |
  | |     | | |          |      | |  ;; can end up with identity.                   | | | | | | | |
  | |     | | |          |      | |  `(and (not (fp-zero-p ,xb))                    | | | | | | | |
  | |     | | |          |      | |        (not (fp-zero-p ,yb))))))))))))          | | | | | | | |
  | |     | | |          |      | +-------------------------------------------------+ | | | | | | |
  | |     | | |          |      +-----------------------------------------------------+ | | | | | |
  | |     | | |          +--------------------------------------------------------------+ | | | | |
  | |     | | +---------------------------------------------------------------------------+ | | | |
  | |     | +-------------------------------------------------------------------------------+ | | |
  | |     +-----------------------------------------------------------------------------------+ | |
  | +-------------------------------------------------------------------------------------------+ |
  +-----------------------------------------------------------------------------------------------+
Re:What's the deal with tab size (8 spaces) in the GitHub UI? by jdrek1 on Thursday September 23, 2021 @07:40AM (#28627820)
Okay I think I understand your core issue. My position is that you do one indent per open scope and those should be done by tabs. Since those usually appear at the beginning of the line there's no problem because tabs behave exactly like you want them to (equal X spaces). It also produces nice boxes, especially in C like languages if I may say so, personally I think Python forcing you to obey this principle is actually a good think to teach people. Can look like this:

  +--------------------------------------+
  |namespace foo                         |
  |{                                     |
  |     +------------------------------+ |
  |[TAB]|void bar(args)                | |
  |[TAB]|{                             | |
  |     |     +-----------------+      | |
  |[TAB]|[TAB]|while (condition)|      | |
  |[TAB]|[TAB]|{                |      | |
  |[TAB]|[TAB]|[TAB]// body     |      | |
  |[TAB]|[TAB]|}                |      | |
  |     |     +-----------------+      | |
  |     |                              | |
  |     |     // and another scope     | |
  |     |     +-----------------+      | |
  |[TAB]|[TAB]|if (condition)   |      | |
  |[TAB]|[TAB]|{                |      | |
  |[TAB]|[TAB]|[TAB]// body     |      | |
  |[TAB]|[TAB]|}                |      | |
  |     |     +-----------------+      | |
  |[TAB]|}                             | |
  |     +------------------------------+ |
  |}                                     |
  +--------------------------------------+
Your problem appears when a new scope does not seeem to start at the beginning of a line, correct? For example like this:

  +--------------------------------------+
  |namespace foo                         |
  |{                                     |
  |     +------------------------------+ |
  |[TAB]|auto bar(args)                | |
  |     | // function body             | |
  |[TAB]|{                             | |
  |[TAB]|[TAB]return baz([args](){     | |
  |[TAB]|[TAB][TAB]// lambda body      | |
  |[TAB]|[TAB]});                      | |
  |[TAB]|}                             | |
  |     +------------------------------+ |
  |}                                     |
  +--------------------------------------+
If you try to draw a rectangle around only the lambda you have to "indent" the body by spaces to make it nicely aligned like this

  +--------------------------------------+
  |namespace foo                         |
  |{                                     |
  |     +------------------------------+ |
  |[TAB]|auto bar(args)                | |
  |[TAB]|{                             | |
  |     |                +----------+  | |
  |[TAB]|[TAB]return baz(|[args](){ |  | |
  |[TAB]|[TAB][TAB]      |     ...  |  | |
  |     |[TAB]           |}         |  | |
  |     |                +----------+  | |
  |     |[TAB]);                       | |
  |[TAB]|}                             | |
  |     +------------------------------+ |
  |}                                     |
  +--------------------------------------+
and someone misguided could have the really bad idea of first wanting to align a box and then doing the indent for the lambda scope:

  +--------------------------------------+
  |namespace foo                         |
  |{                                     |
  |     +------------------------------+ |
  |[TAB]|auto bar(args)                | |
  |[TAB]|{                             | |
  |     |                +----------+  | |
  |[TAB]|[TAB]return baz(|[args](){ |  | |
  |[TAB]|[TAB]           |[TAB]...  |  | |
  |     |[TAB]           |}         |  | |
  |     |                +----------+  | |
  |     |[TAB]);                       | |
  |[TAB]|}                             | |
  |     +------------------------------+ |
  |}                                     |
  +--------------------------------------+
That obviously leads to problems, yes, but that is simply someone who did not understand "tabs for indent, spaces for alignment" then. You should train that person better, it's really not that hard and also there's tools that can easily warn on such things. Banning tabs gets rid of this problem but it also gets rid of the advantage tabs have, namely that the indentation can look differently to many people without causing any VCS problems. And that's worth a lot more than saving a one time 2 minute work of enforcing a check for such things in the VCS for a single person.
Re:What's the deal with tab size (8 spaces) in the GitHub UI? by kazinator on Thursday September 23, 2021 @12:11PM (#28631175)
To clarify, I do not have a problem; I'm simply revealing the existence of certain requirements in the wild.

The 1960's tab modeled after typewriter tab stops is not up to snuff to these requirements.

You fall back on spaces, and then you have a half-baked result where only some of the indentation scales with the tab size.

I suspect that a successful solution will not involve tabs at all. It's algorithmically possible for the text presentation tool, such as the editor, to infer which spaces are indentation and which are alignment, informed by the language syntax, and then allow the indentation spaces to have a configurable size.

I posited that if tabs had simple expansion semantics (tab produces fixed number of spaces starting at any position) that would also be workable. Unfortunately, it requires integration into way too many tools all at the same time.