Readable Math

A while back I was working on a proof-of-concept for an Android game when the following code found its way to the codebase:

It’s readable, but it might take a moment to realize what goes where. What if that method could look like this?

While this example is relatively easy to read and understand, the following example is less so:

And with math styling:

Suddenly it seems familiar, right?

This feature can be easily implemented in existing IDEs as a plugin. Programmers will write the expression and select an option in the IDE that will convert the expression to a stylized display – behind the scenes a LaTeX code will be generated and rendered. The IDE will then display the stylized expression and the compiler will get the equivalent expression in a form that it can understand. Programmers will have the ability to edit the actual LaTeX code so that they can style it however they choose, and the feature will make sure that the expression is always in sync with its LaTeX presentation.

Of course, we also need to think about programmers that don’t use an IDE that supports this feature – this can be made to work by injecting the LaTeX code inside comments that surround the expression. An example of how this will look in the source file (which still remains in text format) as well as in an IDE that doesn’t support this feature:

Why the duplication? There are many ways to format the same equation in LaTeX, and if we auto-generate the LaTeX every time the file loads in the IDE it might not be formatted as the programmer wanted it. Granted, this is not the most readable or elegant solution. I’m sure someone can think of an implementation that’s more readable and more stable from a technical standpoint.

 

This feature idea opens up new possibilities for embedding more intuitive formats of code inside the plain-text. Perform operations on a matrix, replace method calls with equivalent mathematic notations (such as replacing Math.abs(x) with |x|), etc. Any ideas of your own?