Programming In English

Programming In English

This ebook describes how to build a versatile computer language whose scripts look like a series of step-by-step English commands with a syntax similar to cookery recipes, navigation instructions or car maintenance manuals. The syntax will be English stripped to the minimum; terse but understandable by anyone familiar with the problem domain. The environment will be that of a web browser and the language will be a comprehensive, capable and easy to use replacement for JavaScript, permitting scripts to be embedded in any web page then compiled and run on the fly as the page loads. Here is a list of key requirements:

  • English-like scripts embedded in a web page or loaded via REST
  • Able to handle any functionality provided by JavaScript
  • No build tools required
  • Compact and expressive - low memory usage
  • Direct compilation in the browser with no perceptible delays
  • Good runtime performance
  • Extendable with plugins

Background

Programming is about communication; specifically about getting computers to do things. I'm assuming that you, my reader, are a programmer and that among your circle of friends and acquaintances there are others with whom you share specialized skills you've worked long and hard to gain.

You'll also know a lot of other people who aren't programmers, most of whom find your interests and skills baffling or even frightening. For them, computers are just tools to be used, preferably without the need to understand how they work any more than most of us know about what happens under the hood of a car, or indeed anywhere outside our own areas of speciality.

Just as non-programmers find it hard to understand how programmers think, we often fail to fully appreciate the complexities of the world outside the computer, for which we build software systems. We have a regrettable tendency to disregard advice from users of a system because we all too often assume we know better than those users. Better understanding makes for better, more reliable software, and better communication is the key to better understanding.

Computers are literal devices. They blindly follow the instructions given, without stopping to question if you really meant what you said. A programmer is someone who has the skill to create sets of instructions that are unambiguous, so the computer will do exactly what is intended. In order for this to happen, instructions are written in a computer language; a formal syntax that's understood by a computer and more importantly by the programmers tasked with ensuring the right things are done. Many computer languages have been invented, most of which are much more similar to each other than any of them are to human languages. And just as human languages develop specialized dialects to suit the needs of a particular group, whether it be chemists, lawyers or footballers, new computer languages are always being created to provide a better match to problem domains of one kind or another.

Why is programming so difficult?

Most if not all of our current computer languages are less than half a century old. Programming started by devising symbolic code that offered a human-readable alternative to coding in the hard-to-remember numbers required by the computing devices themselves. As time went by, more and more layers of abstraction were added, but in most cases they retained a fundamental characteristic, being more or less a system of mathematical symbols and expressions.

Take a few lines of any computer program written in C/C++, Java, Python or JavaScript, and try to read it out loud. It's almost impossible to do it in such a way that another human being - even another programmer - can make sense of what you are saying. The only sensible way to communicate computer code to another person is in writing. And this is one reason why non-programmers find programming so difficult. We learn to speak before we learn to read; most of us can "hear" written text when we see it and many people subvocalize to help them process the meaning, without even being aware of it. For them, coding feels unnatural; something to be left to those strange geeks.

It's all a matter of motivation. As programmers, you and I may be happy to live in a silent world of cryptic formulae and text, but the majority of people aren't built like that and for them it's a complete turn-off. It's not that they can't become programmers; they just don't have any motivation for doing so. In fact, nearly everybody is a programmer, quite capable of handling cooking recipes or step by step car maintenance instructions that resemble simple computer programs. The human species developed complex societies long before we invented computer language. There were programmes long before there was programming. Lawyers and engineers have the mental discipline to organise facts in a coherent manner so that the outcome of an action can be safely predicted, but for some reason, when it comes to computers we assume it's necessary to use a way of expressing things that's completely alien to our natural modes of thinking. Small wonder there's a shortage of good programmers.

It gets worse. Computer software is structurally complex, requiring a huge amount of training and experience to understand how a large program works or even where it begins. As a result, maintenance is often a time bomb waiting to blow up big systems. By the time a few years have passed, the original designers of a software system are long gone and maintenance is often left in the hands of people who are inadequately trained or for whom this is only one of many other responsibilities. Lacking the insights possessed by the original design team they inevitably make mistakes, causing invisible damage to the whole edifice. Slowly but surely the system begins to decay, with every attempt to fix it creating further problems. In the end, to avoid catastrophic failure the system will have to be replaced, often well before its originally projected lifetime.

For centuries we have built complex and robust human societies without the need for computers or software. Language had a huge, vital part to play in this; without it we'd still be living in caves.

Programming in English

So back to the title of this book. While freely admitting that there are no universal solutions, I argue that many software products would benefit from being written in something closer to English so they could be understood by more of the people with "skin in the game". I'd like to offer a couple of basic guiding principles that make the case for what follows:

  1. If it's too complex to express in English, it's too complex. Period.
  2. If it can be expressed in English it should be possible to code it in English.

The rest of this book explains how to build a computer language that's powerful enough to address any problem that can be described in English, yet readable enough that anyone with an understanding of the problem domain can discover how it works.

The techniques described are not just theoretical; they're ones that I've used for over 20 years, starting in about 1998 with a Java-based compiler and runtime. In 2018 I decided to recreate the same thing using JavaScript, partly to gain experience in using the language and partly to build an environment that would make it easier to program web applications. Both aims were successful. The end result is called EasyCoder; its source code repository is hosted on GitHub at https://github.com/easycoder and that's where most of the code excerpts in this book come from.

For anyone interested, building a language along these lines isn't a huge job. It took me about 4 months (part time) to get the compiler and runtime up, then a year or two to gradually refine and enhance them. There's quite a lot of code but nothing massively complicated anywhere, so in terms of size it would make quite a reasonable student project, albeit a fairly unusual one. I found the whole exercise most beneficial in getting me proficient in understanding and using core JavaScript.

It's possible this book may inspire some to take the existing EasyCoder system and customize it for their own purposes. This is fine; you are most welcome to use the book as a manual on how the system currently works and to make whatever changes you like to improve it.

EasyCoder has its own website at https://easycoder.github.io (built entirely using EasyCoder scripts, of course). One of the links on the home page is to the Codex, a combination of tutorial, reference manual and coding playground where users can create and run scripts. Readers of this book can get a feel for the kind of language being described, as well as appreciating its ability to handle a very wide range of varied tasks. It is my hope that this may inspire the creation of languages far more capable than my own.

This book can be read in a linear fashion; at the bottom of each page is a link to the next. Alternatively, you can choose any page from the table of contents below.

I'm assuming you have some experience with JavaScript and ES6. You don't need to be an expert but complete beginners will find it a little hard going in places as I expect you to be able to read sections of code without my having to spell out everything line by line. When I started the EasyCoder project my level of JS knowledge was pretty low, and it shows in places. I keep promising myself I'll do a major refactoring exercise some day but the time never seems to be available.

Photo by Cookie the Pom on Unsplash

Designing a language


Contents

Designing a language

The coding environment

Vocabulary

Example code

Variables

Commands

Compiling scripts

The tokenizer

The compiler

Domains

Compiling variables

Keyword handlers

The if handler

Goto, Gosub and Multitasking

Document Object Model commands

Value handlers

Condition handlers

The runtime engine

Runtime handlers

Running DOM commands

Plugins

Handling plugins

Export and import

Conclusion