My Answer to The Parenthesis Problem

As I wrote in the previous post, the solution to the problem is to have the editor visualize the code a certain way while having the programmer’s inputs, and the programming language syntax, remain the same.

The idea I present here takes advantage of the fact that there are temporary and interactive aspects to code. When code is not yet finished you might have opening parentheses that have no closing counterparts. Assuming the code is finished, and has the usual formatting, the editor would change the code’s appearance as follows.

(define (factorial n)
  (if (zero? n)
      1
      (* n (factorial (sub1 n)))))

        🡫

 define (factorial n)
   if |zero? n					   
      1
      |* n (factorial (sub1 n)

If unfinished or incorrectly indented, the editor would make the code look a certain way. The ‘finished’ look above builds on that. This makes many parentheses visually obsolete and lets the text’s formatting do the job. The unseen parentheses enable the editor to provide such visualizations.

In Detail

What the finished look does specifically:

  1. They are part of a ’list-like’ format (for lack of a better term) and

  2. Their respective closing parenthesis is at the end of the same line:

    (if (zero? n)
        (add1 n)
        (sub1 n))
    
     if |zero? n
        |add1 n
        |sub1 n
    

The unfinished look gives opening parentheses with no closing counterpart a different shape.

add1 gif

The incorrect indentation look provides visual cues to point out any incorrect or atypical indentation.

    (define X 5)
      (define Y 4)
      (define Z (+ 2 3))

     define X 5
    →  define Y 4
    →  define Z (+ 2 3)

The availability of these visualizations makes the finished look possible.

factorial gif

Questions

Implementation…?
I can’t say when I’ll get around to implementing what I’ve described, as I’m an inexperienced programmer. That said, anyone interested in implementing, copying or modifying this proposal is free to do so.

How to edit such code?
If you would previously edit some code by finding the appropriate closing parens in a massive pile-up of )))))), where would you now place the cursor? This means the editing experience changes a bit. One possible solution is to have parentheses show up where you put the cursor, essentially toggling back to the text view for that particular line of code.

It’s likely that for users of editing tools like paredit, the editing experience would remain the same.

What to call an implementation of this proposal?
The project name for this idea was Lisp Beyond Parentheses, but I think an implementation should have a more descriptive name. I would consider calling it Adaptive Code Visualization.