1. 30
  1.  

  2. 12

    A realization I recently had:

    Why don’t we abstract away all display affordances from a piece of code’s position in a file? That is, the editor reads the file, parses its AST, and displays it according to the programmer’s preference (e.g., elastic tabstops, elm-like comma-leading lists, newline/no-newline before opening braces, etc). And prior to save, the editor simply runs it through an uncustomized prettier first.

    There are a million and one ways to view XML data without actually reading/writing pure XML. Why not do that with code as well?

    1. 4

      This idea is floating around the interwebz for a long time. I recall it being stated almost verbatim on Reddit, HN, probably on /.

      1. 6

        And once you take it a step further, it’s clear that it shouldn’t be in a text file in the first place. Code just isn’t text. If you store it as a tree or a graph in some sort of database, it becomes possible to interact with it in much more powerful ways (including displaying it any way you like). We’ve been hobbled by equating display representation with storage format.

        1. 7

          This talk touches on this issue, along with some related ones and HCI in general: Bret Victor: The Future of Programming

          1. 2

            God, I have been trying to recall the name of this talk for ages! Thank you so much, it is a great recommendation

          2. 5

            Text is great when (not if) your more complicated tools fail or do something you can’t tolerate and you need to use tools which don’t Respect The Intent of designers who, for whatever reason, don’t respect your intent or workflow. Sometimes, solving a problem means working around a breakage, whether or not that breakage is intentional on someone else’s part.

            Besides, we just (like, last fifteen or so years) got text to the point where it’s largely compatible. Would be a shame to throw that away in favor of some new AST-database-thing which only exists on a few platforms.

            1. 1

              I’m not sure I get your point about about intent. Isn’t the same already true of, say, compilers? There are compiler bugs that we have to work around, there are programs that seem logical to us but the compiler won’t accept, and so on. Still, everybody seems to be mostly happy to file a compiler bug or a feature request, and live with a workaround for the present. Seems like it works well enough in practice.

              I understand your concern about introducing a new format but it sounds like a case of worse-is-better. Sure, we get a lot of convenience from the ubiquity of text, but it would nevertheless be sad if we were stuck with it for the next two centuries.

              1. 1

                With compilers, there are multiple of them for any given language, if the language is important enough, and you can feed the same source into all of them, assuming that source is text.

                1. 2

                  I’ve never seen anyone casually swap out the compiler for production code. Also, for the longest time, if you wrote C++ for Windows, you pretty much had to use the Microsoft compiler. I’m sure that there are many embedded platforms with a single compiler.

                  If there’s a bug in the compiler, in most casss you work around it, then patiently wait for a fix from the vendor.

                  So that’s hardly a valid counterpoint.

                  1. 1

                    Re: swapping out compiler for production code: most if not all cross-platform C++ libraries can be compiled on at least llvm, gcc and msvc.

                    1. 1

                      Yes, I’m aware of that, but what does it have to do with anything I said?

                      EDIT: Hey, I went to Canterbury :)

                      1. 1

                        “I’ve never seen anyone casually swap out the compiler for production code” sounded like you were saying people didn’t tend to compile the same production code on multiple compilers, which of course anyone that compiles on windows and non-windows does. Sorry if I misinterpreted your comment!

                        My first comment is in response to another Kiwi. Small world. Pretty cool.

            2. 1

              This, this, a thousand times this. Text is a good user-interface for code (for now). But it’s a terrible storage and interchange format. Every tool needs its own parser, and each one is slightly different, leaving begging the amount of cpu and programmer time we waste going from text<->ast<->text.

              1. 2

                Yeah, it’s obviously wasteful and limiting. Why do you think we are still stuck with text? Is it just sheer inertia and incrementalism, or does text really offer advantages that are challenging to recreate with other formats?

                1. 7

                  The text editor I use can handle any computer language you can throw at it. It doesn’t matter if it’s BASIC, C, BCPL, C++, SQL, Prolog, Fortran 77, Pascal, x86 Assembler, Forth, Lisp, JavaScript, Java, Lua, Make, Hope, Go, Swift, Objective-C, Rexx, Ruby, XSLT, HTML, Perl, TCL, Clojure, 6502 Assembler, 68000 Assembler, COBOL, Coffee, Erlang, Haskell, Ocaml, ML, 6809 Assembler, PostScript, Scala, Brainfuck, or even Whitespace. [1]

                  Meanwhile, the last time I tried an IDE (last year I think) it crashed hard on a simple C program I attempted to load into it. It was valid C code [2]. That just reinforced my notion that we aren’t anywhere close to getting away from text.

                  [1] APL is an issue, but only because I can’t type the character set on my keyboard.

                  [2] But NOT C++, which of course, everybody uses, right?

                  1. 0

                    To your point about text editors working with any language, I think this is like arguing that the only tool required by a carpenter is a single large screwdriver: you can use it as a hammer, as a chisel, as a knife (if sharpened), as a wedge, as a nail puller, and so on. Just apply sufficient effort and ingenuity! Does that sound like an optimal solution?

                    My preference is for powerful specialised tools rather than a single thing that can be kind of sort of applied to a task.

                    Or, to approach from the opposite direction, would you say that a CAD application or Blender are bad tools because they only work with a limited number of formats? If only they also allowed you to edit JPEGs and PDFs, they would be so much better!

                    To your point about IDEs: I think that might even support my argument. Parsing of freeform text is apparently sufficiently hard that we’re still getting issues like the one you saw.

                    1. 9

                      I use other tools besides the text editor—I use version control, compilers, linkers, debuggers, and a whole litany of Unix tools (grep, sed, awk, sort, etc). The thing I want to point out is that as long as the source code is in ASCII (or UTF-8), I can edit it. I can study it. I might not be able to compile it (because I lack the INRAC compiler but I can still view the code). How does one “view” Smalltalk code when one doesn’t have Smalltalk? Or Visual Basic? Last I hear, Microsoft wasn’t giving out the format for Visual Basic programs (and good luck even finding the format for VB from the late 90s).

                      The other issue I have with IDEs (and I will come out and say I have a bias against the things because I’ve never had one that worked for me for any length of time without crashing, and I’ve tried quite a few over 30 years) is that you have one IDE for C++, and one for Java, and one for Pascal, and one for Assembly [1] and one for Lua and one for Python and man … that’s just too many damn environments to deal with [2]. Maybe there are IDEs now that can work with more than one language [3] but again, I’ve yet to find one that works.

                      I have nothing against specialized tools like AutoCAD or Blender or PhotoShop or even Deluxe Paint, as long as there is a way to extract the data when the tool (or the company) is no longer around. Photo Shop and Deluxe Paint work with defined formats that other tools can understand. I think Blender works with several formats, but I am not sure about AutoCAD (never having used it).

                      So, why hasn’t anyone stored and manipulated ASTs? I keep hearing cries that we should do it, but yet, no one has yet done it … I wonder if it’s harder than you even imagine …

                      Edited to add: Also, I’m a language maven, not a tool maven. It sounds like you are a tool maven. That colors our perspectives.

                      [1] Yes, I’ve come across several of those. Never understood the appeal …

                      [2] For work, I have to deal with C, C++, Lua, Make and Perl.

                      [3] Yeah, the last one that claimed C/C++ worked out so well for me.

                      1. 1

                        For your first concern about the long term accessibility of the code, you’ve already pointed out the solution: a defined open format.

                        Regarding IDEs: I’m not actually talking about IDEs; I’m talking about an editor that works with something other than text. Debugging, running the code, profiling etc. are different concerns and they can be handled separately (although again, the input would be something other than text). I suppose it would have some aspects of an IDE because you’d be manipulating the whole code base rather than individual files.

                        Regarding the language maven post: I enjoyed reading it a few years ago (and in practice, I’ve always ended up in the language camp as an early adopter). It was written 14 years ago, and I think the situation is different now. People have come to expect tooling, and it’s much easier to provide it in the form of editor/IDE plugins. Since language creators already have to do a huge amount of work to make programs in their languages executable in some form, I don’t think it would be an obstacle if the price of admission also included dealing with the storage format and representation.

                        To your point about lack of implementations: don’t Smalltalk and derivatives such as Pharo qualify? I don’t know if they store ASTs but at least they don’t store text. I think they demonstrate that it’s at least technically possible to get away from text, so the lack of mainstream adoption might be caused by non-technical reasons like being in a local maximum in terms of tools.

                        The problem, as always, is that there is such a huge number of tools already built around text that it’s very difficult to move to something else, even if the post-transition state of affairs would be much better.

                        1. 1

                          Text editors are language agnostic.

                          I’m trying to conceive of an “editor” that works with something other than text. Say an AST. Okay, but in Pascal, you have to declare variables at the top of each scope; you can declare variables anywhere in C++. In Lua, you can just use a variable, no declaration required. LISP, Lua and JavaScript allow anonymous functions; only the latest versions of C++ and Java allow anonymous functions, but they they’re restricted in that you can’t create closures, since C++ and Java have no concept of closures. C++ has exceptions, Java has two types of exceptions, C doesn’t; Lua kind of has exceptions but not really. An “AST editor” would have to somehow know that is and isn’t allowed per language, so if I’m editing C++ and write an anonymous function, I don’t reference variables outside the scope of said function, but that it can for Lua.

                          Okay, so we step away from AST—what other format do you see as being better than text?

                          1. 1

                            I don’t think it could be language agnostic - it would defeat the purpose as it wouldn’t be any more powerful than existing editors. However, I think it could offer largely the same UI, for similar languages at least.

                            1. 1

                              And that is my problem with it. As stated, I use C, C++ [1], Lua, Make and a bit of Perl. That’s at least what? Three different “editors” (C/C++, Lua/Perl (maybe), Make). No thank you, I’ll stick with a tool that can work with any language.

                              [1] Sparingly and where we have no choice; no one on my team actually enjoys it.

                            2. 1

                              Personally, I’m not saying you should need to give up your editor of choice. Text is a good (enough for now) UI for coding. But it’s a terrible format to build tools on. If the current state of the code lived in some sort of event-based graph database for example, your changes could trigger not only your incremental compiler, but source analysis (only on what’s new), it could also maintain a semantic changelog for version control, trigger code-generation (again, only what’s new).

                              There’s a million things that are currently “too hard” which would cease to be too hard if we had a live model of the code as various graphs (not just the ast, but call graphs, inheritance graphs, you-name-it) that we could subscribe to, or even write purely-functional consumers that are triggered only on changes.

                    2. 4

                      Inertia, arrogance, worse-is-better; Working systems being trapped behind closed doors at big companies; Hackers taking their language / editor / process on as part of their identity that needs to be defended with religious zeal; The complete destruction of dev tools as a viable business model; Methodologies-of-the-week…. The causes are numerous and varied, and the result is software dev is being hamstrung and we’re all wasting countless hours and dollars doing things computers should be doing for us.

                      1. 2

                        I think that part of the issue is that we haven’t seen good structured editor support outside of Haskell and some Lisps.

                        Having a principled foundation for structured editor + a critical mass by having it work for a language like Javascript/Ruby, would go a long way to making this concept more mainstream. After which we could say “provide a grammar for favorite language X and get structured editor support!”. This then becomes “everything is structured at all levels!”

                        1. 3

                          I think it’s possible that this only works for a subset of languages.

                          Structured editing is good in that it operates at a higher level than characters, but ultimately it’s still a text editing tool, isn’t it? For example, I think it should be trivial to pull up a list of (editable) definitions for all the functions in a project that call a given function, or to sort function and type definitions in different ways, or to substitute function calls in a function with the bodies of those functions to a given depth (as opposed to switching between different views to see what those functions do). I don’t think structured editing can help with tasks like that.

                          There are also ideas like Luna, have you seen it? I’m not convinced by the visual representation (it’s useful in some situations but I’m not sure it’s generally effective), but the interesting thing is they provide both a textual and a visual representation of the code.

                      2. 1

                        Python has a standard library module for parsing Python code into an AST and modifying the AST, but I don’t know of any Python tools that actually use it. I’m sure some of them do, though.

                      3. 1

                        Smalltalk. The word you’re looking for is Smalltalk. ;)

                        1. 2

                          Lisp, in fact. Smalltalk lives in an image, Lisp lives in the real world. ;)

                          Besides, Lisp already is the AST. Smalltalk has too much sugar, which is a pain in the AST.

                          1. 1

                            Possibly, but I’m only talking about a single aspect of it: being able to analyse and manipulate the code in more powerful ways than afforded by plain text. I think that’s equally possible for FP languages.

                        2. 1

                          Ultimately I think this is the only teneble solution. I feel I must be in the minority in having an extreme dislike of columnar-style code, and what I call “white space cliffs” where a column dictates a sudden huge increase in whitespace. But I realize how much it comes down to personal aesthetics, so I wish we could all just coexist :)

                          1. 1

                            Yeah, I’ve been messing around with similar ideas, see https://nick.zoic.org/art/waste-web-abstract-syntax-tree-editor/ although it’s only vapourware so far because things got busy …

                            1. 1

                              Many editors already do this to some extent. They just render 4-space tabs as whatever the user asks for. Everything after the indent, though, is assumed to be spaced appropriately (which seems right, anyway?)

                              1. 1

                                You can’t convert to elastic-tabstop style from that, and without heavy language-grammar knowledge you can’t do this for 4-space “tabs” generally.

                                Every editor ever supports this for traditional indent style, though: http://intellindent.info/seriously/

                                1. 1

                                  To be clear, you can absolutely render a file that doesn’t have elastic tabstops as if it did. The way a file is rendered has nothing to do with the actual text in the file.

                                  It’s like you’re suggesting that you can’t render a file containing a ton of numbers as a 3D scene in a game engine. That would be just wrong.

                                  Regardless, my point is specifically that this elastic tabstops thing is not necessary and hurts code readability more than it helps.

                                  The pefantics of clarifying between tabs and tabstops is a silly thing as well. Context gives more than enough information to know which one is being talked about.

                                  It sounds like this concept is creating more problems than it solves, and is causing your editor to solve problems that only exist in the seveloper’s imagination. It’s not “KISS” at all, quite the opposite.

                              2. 1

                                Because presentation isn’t just a function of the AST. Indentation usually is, but alignment can be visually useful for all kinds of reasons.

                              3. 6

                                Please, please stop trying to shoehorn a markup language into ASCII.

                                Tab characters originally were used not to indent text, but to align columns of tables on fixed-width terminals and punched cards (and this is the only usage for which tab characters do not suck). Much later this was repurposed as a “clever” hack to add indentation to text instead of a proper markup language. Elastic tabstops are an effort to make tabs do alignment too, introducing a block-based model into editing, which sounds like even more “clever” hack and requiring more logic from anything that could display text.

                                On the one hand, I am not a die-hard proponent of “code must be fixed-width plain text”, I find the idea of smart, helpful code self-representation theoretically appealing and I sympathize with some visual prototypes and smart editors. I am still open to the idea that someday fixed-width grids of characters will not be the optimal programming interface and source code will be more smart.

                                On the other hand, I have yet to see a practical solution to represent source code as something else than plain text. vim and modern editors make editing not smart, but at least not painful (they understand blocks and indentation), so I see no practical reason to put markup into the text itself without turning it into a rich-text mark up. And rich-text markup for source code is kinda nice, but not very useful on its own.

                                So, until something obviously superior makes fixed-width grids of characters clearly obsolete, I’d rather stay with the most straightforward possible representation of source code (even tab characters have too much cleverness for my taste) and I’d rather rely on editors to be a little smarter, not the the text itself. Formatting should happen when code is written, not when it is displayed.

                                1. 3

                                  “Using spaces to align columns is obviously a kludge”

                                  I don’t think that this is true at all. You can easily tell editors to display indents differently than they are in the file. Furthermore, not using tabs after an indent itself solves the problem completely.

                                  This seems like someone just REALLY doesn’t want to use spaces - which isn’t even an argument that I’ve heard anyone have in nearly 10 years. I honestly figured everyone figured this out by now, but I guess not?

                                  Do y’all still see this argument a lot?

                                  1. 2

                                    I fully intend to incorporate this into my text editor (that I only have plans for this far). Though it’d be nice to have the editor automatically detect space-alignment, e.g. before comments, and replace that with elastic tabs automatically on load and back with spaces again on save. That way sans-serif fonts will work even with the fascist gofmt.

                                    1. 2

                                      What if I don’t want to align to the adjacent line?

                                      1. 1

                                        Well, then you have to add a blank line in between.

                                      2. 2

                                        Top comments pretty much address my views on this, so I’ll just drop a relevant XKCD

                                        1. 1

                                          I once tried to implement this for an in-browser editor using the tab-size CSS property, only to find that no browsers implement that property fully. The CSS standard says that you should be able to pass any distance such as 17px to set the width of each tab character, but in practice browsers only support passing an integer such as 2, which makes the width an exact multiple of the width of the font’s space character.

                                          It’s a shame that no text formatting API I have ever seen makes implementing elastic tabstops easy.

                                          Though, thinking about the above limitation of CSS implementations, maybe it would be possible to combine tab-size with a web font that changes tab characters to be one pixel wide? The unicode-range property of a @font-face declaration can be used to specify that the font is only used to render certain characters.