Chapter
Programming
This chapter talks about how programming languages have evolved over time. Since the rise of the Internet in the 1990s, a lot of languages and tools have been developed to support it. One of the best-known languages is HTML, which is used to create web sites. Together with JavaScript and CSS style sheets, it allows the creation of dynamic web sites that can be displayed by a browser. I discuss HTML and JavaScript in detail in this chapter, and you see how to create a simple web application that uses the HTML5 canvas in combination with JavaScript.
Computers and Programs
Before you start dealing with HTML and JavaScript, this section briefly covers computers and programming in general. After that, you move on to how to create a simple HTML page in combination with JavaScript.
Processor and Memory
Generally speaking, a computer consists of a processor and memory. This is true for all modern computers, including game consoles, smartphones, and tablets. I define memory as something that you can read things from, and/or write things to. Memory comes in different varieties, mainly differing in the speed of data transfer and data access. Some memory can be read and written as many times as you want, some memory can only be read, and other memory can only be written to. The main processor in the computer is called the central processing unit (CPU). The most common other processor on a computer is a graphics processing unit (GPU). Even the CPU itself nowadays is no longer a single processor but often consists of a number of cores.
Input and output equipment, such as a mouse, gamepad, keyboard, monitor, printer, touch screen, and so on, seems to fall outside the processor and memory categories at first glance. However, abstractly speaking, theyre actually memory. A touch screen is read-only memory, and a printer is write-only memory.
The main task of the processor is to execute instructions. The effect of executing these instructions is that the memory is changed. Especially with my very broad definition of memory, every instruction a processor executes changes the memory in some way. You probably dont want the computer to execute only one instruction. Generally, you have a very long list of instructions to be executedMove this part of the memory over there, clear this part of the memory, draw this sprite on the screen, check if the player is pressing a key on the gamepad, and make some coffee while youre at itand (as you probably expect) such a list of instructions that is executed by the computer is called a program.
Programs
In summary, a program is a long list of instructions to change the computers memory. However, the program itself is also stored in memory. Before the instructions in the program are executed, theyre stored on a hard disk, a DVD, or a USB flash disk; or in the cloud; or on any other storage medium. When they need to be executed, the program is moved to the internal memory of the machine.
The instructions that, combined together, form the program need to be expressed in some way. The computer cant grasp instructions typed in plain English, which is why you need programming languages such as JavaScript. In practice, the instructions are coded as text, but you need to follow a very strict way of writing them down, according to a set of rules that defines a programming language. Many programming languages exist, because when somebody thinks of a slightly better way of expressing a certain type of instruction, their approach often becomes a new programming language. Its difficult to say how many programming languages there are, because that depends on whether you count all the versions and dialects of a language; but suffice to say that there are thousands.
Fortunately, its not necessary to learn all these different languages, because they have many similarities. In the early days, the main goal of programming languages was to use the new possibilities of computers. However, more recent languages focus on bringing some order to the chaos that writing programs can cause. Programming languages that share similar properties are said to belong to the same programming paradigm. A paradigm refers to a set of practices that is commonly used.
The Early Days: Imperative Programming
A large group of programming languages belongs to the imperative paradigm. Therefore, these languages are called imperative languages . Imperative languages are based on instructions to change the computers memory. As such, theyre well suited to the processor-memory model described in the previous section. JavaScript is an example of an imperative language.
In the early days, programming computer games was a very difficult task that required great skill. A game console like the popular Atari VCS had only 128 bytes of RAM (Random Access Memory) and could use cartridges with at most 4,096 bytes of ROM (Read-Only Memory) that had to contain both the program and the game data. This limited the possibilities considerably. For example, most games had a symmetric level design because that halved the memory requirements. The machines were also extremely slow.
Programming such games was done in an Assembler language. Assembler languages were the first imperative programming languages. Each type of processor had its own set of Assembler instructions, so these Assembler languages were different for each processor. Because such a limited amount of memory was available, game programmers were experts at squeezing out the last bits of memory and performing extremely clever hacks to increase efficiency. The final programs, though, were unreadable and couldnt be understood by anyone but the original programmer. Fortunately that wasnt a problem, because back then, games were typically developed by a single person.
A bigger issue was that because each processor had its own version of the Assembler language, every time a new processor came around, all the existing programs had to be completely rewritten for that processor. Therefore, a need arose for processor-independent programming languages. This resulted in languages such as Fortran (FORmula TRANslator) and BASIC (Beginners All-purpose Symbolic Instruction Code). BASIC was very popular in the 1970s because it came with early personal computers such as the Apple II in 1978, the IBM-PC in 1979, and their descendants. Unfortunately this language was never standardized, so every computer brand used its own dialect of BASIC.
Note The fact that I made the effort to identify the paradigm of imperative programming languages implies that there are other programming paradigms that arent based on instructions. Is this possible? What does the processor do if it doesnt execute instructions? Well, the processor always executes instructions, but that doesnt mean the programming language contains them. For example, suppose you build a very complicated spreadsheet with many links between different cells in the sheet. You could call this activity programming and call the empty spreadsheet the program, ready to process data. In this case, the program is based not on instructions but on functional links between the cells. In addition to these functional programming languages, there are languages based on propositional logicthe logical programming languagessuch as Prolog. These two types of programming languages together form the declarative paradigm.
Procedural Programming: Imperative + Procedures
As programs became more complex, it was clear that a better way of organizing all these instructions was necessary. In the procedural programming paradigm , related instructions are grouped together in procedures (or functions, or methods, the latter of which is the more common modern name). Because a procedural programming language still contains instructions, all procedural languages are also imperative.
Next page