💾 Archived View for yujiri.xyz › software › guide › programming.gmi captured on 2024-06-16 at 12:38:08. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-12-28)

-=-=-=-=-=-=-

yujiri.xyz

Software

Learn programming for good instead of profit

How does programming work?

Let's start with the concept of an executable file.

Executable files are files that can be run ('executed') directly by the operating system. On Windows they end in `.exe`; on Linux or Unix they have no name extension. These files contain the actual list of instructions for your processor to execute - in the form the processor understands, not a text form that humans can read. So, if you open an executable file in a text editor, it'll look like gibberish, not like the code that programmers write.

(Executables files are also called 'binaries'.)

An assembly language is a textual way of representing those same instructions so that humans can write and read them. A program called an assembler is used to convert the text (called "source code") into an executable.

https://en.wikipedia.org/wiki/Assembly_language

While assembly code is technically human-readable text, it's still incredibly difficult to read and write, even for an experienced programmer. Hence why "higher-level" languages exist.

The languages most people actually use, like C or JavaScript, are called "high-level" languages. Unlike assembly code, source code in a high-level language is not a direct description of machine instructions. Each machine instruction does something primitive like "move the number from memory address 0xFF68A0C0 to memory address 0xFF68A0B0", whereas a line of code in a high-level language does something like "add this item to a list". The latter is made of the former, but you don't have to deal with all the inscrutable details.

Like assembly languages, high-level languages require a program to convert the source code into machine instructions, called a compiler. Compilers are much more complex than assemblers because source code in a high-level language isn't just a textual representation of machine instructions, so translating it to machine instructions is a very complex process. (Often they do it by first translating the high-level code into assembly code and then running an assembler on that.)

High-level languages make software as we know it possible, because they're much easier and more efficient for humans to read and write. If a program written in C is 30 lines long, the equivalent assembly code might be 100 lines. And the equivalent JavaScript code might be only 10 lines! Some high-level languages are higher-level than others - in fact, we often think of C as a low-level language, because it's low-level compared to other high-level languages.

In general though, high-level languges come with the tradeoff of worse performance, because the generated machine instructions won't be as good as they could be if they were hand-optimized by a human. It's like how translating human speech between languages often loses some nuance. Those 10 lines of JavaScript will run much slower than the 30 lines of C. And the 30 lines of C will probably run slower than the 100 lines of assembly (but only slightly).

Compilers versus interpreters

Actually, not all high-level languages use a compiler. Some use an interpreter instead.

The difference is that whereas a compiler converts your source code to an executable so the operating system can run it, an interpreter reads and runs your code by itself. It doesn't convert it to an executable. The interpreter itself is an executable program written in a compiled language (usually C). JavaScript is actually an interpreted language.

Using a compiler goes like this:

Using an interpreter goes like this:

Interpreters are more convenient than compilers in some ways, but they have two downsides:

So, as you might suspect, we've just covered one of the reasons why software has gotten slower and more resource-hungry: we've largely switched from lower-level, compiled languages like C and C++ to higher-level and interpreted languages like JavaScript! This isn't the entire reason (lots of modern programs written in C and C++ are still slower than old programs), but it's part of it.

You might be wondering now, if JavaScript is popular, and interpreted languages require users to have the interpreter installed to run the program, why have you never had to install a JavaScript interpreter? You have! It's called a web browser. JavaScript is the language used to make dynamic web pages, and web browsers have JavaScript interpreters built-in.

Nowadays, even many seemingly native applications, such as the messaging app Element, are written in JavaScript, using a framework called Electron, which means that the app basically comes with a built-in copy of the Chromium web browser to interpret the code. If that sounds absurdly inefficient to you, it is, and that's why Electron apps are some of the slowest and most memory-hungry apps out there.

Ren'Py is a visual novel engine that does something similar. It lets you write visual novel games using Python, another interpreted language, and generate a .zip file to send to players which includes a copy of the Python interpreter along with the game files.