%CODEBRYTHONCLOCK%
If you’re reading this in a modern web browser window you should see a clock at the top of this post. Nice clock! Programmed in Python3 by a team called Brython (Browser-Python), it’s running in Python right now on your web browser. Have a look at the source view of this post if you don’t believe m… I’ll wait..
This is a great example of code translation. In reality, your web browser is running a JavaScript library that is interpreting the Python code. The way it works is like this. Once set running, the Brython Javascript interpreter:
Looks for script tags of type “text/python”
Reads the python program code inside the script tags
Interprets the code into JavaScript
Runs the JavaScript version
So, if you’ve grown up with, or just prefer, the friendly and disciplined style of coding offered by Python then there’s nothing to stop you using it on the web.
The choose-your-favourite-language-and-convert-it-to-javascript paradigm is becoming more popular, with several languages becoming available for use with a web browser. The pattern is mainly for ‘on the fly’ code translation with the original language embedded in the web page itself.
Why? Recently JavaScript has had a shot in the arm in terms of usage and capability thanks to two technologies:
AJAX calls which allow a browser to update part of a web page from the server rather than refreshing the whole page. This makes web applications behave much more like fully installed apps on your computer. JavaScript running embedded in the web page makes AJAX happen.
Node.JS – a technology that has brought JavaScript to the server side with some frankly outrageous improvements in web server performance in terms of the number of simultaneous connections that can be serviced by one machine. This is because JavaScript runs on a single CPU thread and is ‘non-blocking’; that is, it can continue doing something else while it waits for a callback that some other process has completed. Node.JS is the big growth area in server-side programming and these language conversions allow you to bring your non-JS skills to this table.
Another example is Google’s DART language, which was invented by Google developers to cut down on common developer errors when programming in JavaScript itself. DART is similar to JavaScript but has these advantages:
It enforces a degree of discipline over the use of different variable types. For example, you can declare a variable as an integer, and an integer it will stay or you’ll get an error.
It makes most of the more common JavaScript tasks easier to code with less chance of bugs.
It simplifies some of the most complex tasks in JavaScript such as setting up and running callbacks and other asynchronous code where errors can abound. Given the whopping size and scale of Google’s web apps, they should know!
An alternative to ‘on the fly’ code translation is to convert the finished program to Javascript then embed that resulting JS code in the web page. An example is JSBasic which has an example page where you write in the beloved programming language of my Sinclair ZX Spectrum-filled youth:
10 print "Hello, world!"20 goto 10
..and click a button which converts BASIC to JavaScript:
function runProgram()
{ run('console', 22, 40, line10);}/* Generated by the JSBasic compiler at 11:15:13 27/Sep/16 */function line10()
{ console.println("Hello, world!" ); return line10;}
…which you can embed in a web page.
Quite a movement in re-focussing many classic programming languages for the web that compile or translate the source code of that language to JavaScript. Here are some links I’ve found from a quick search (some are works in progress):
BASIC – JSBasic
Ć (Portable C) – Cito
COBOL: CobolScript
FORTRAN – Fortran-To-JavaScript
PASCAL – P2JS
PHP – php.js
A downside to all this? The challenge is that arguably the best code is one that is human-written especially for the task and ideally peer-reviewed. That means getting to know JavaScript itself – especially the new ECMAScript 6 which corrects a lot of the coding issues (and tightens up the discipline of coding, thus fixing common developer bugs) of ECMAScript 5.
These language converters will get you going, but perhaps teasing open a decent ECMAScript 6 book is the very best way of honing your skills for the present and future web.
Baz Luhrmann once said, ‘Wear Sunscreen’. I say, “Learn JavaScript“.
Comments