Table of Contents
Dedication
To my teacher Mrs. Annamma George, who believed in me even when I did not.
To my parents, who made me who I am.
To my wife Pallavi Kanchana and children Anika and Adrith, for their constant and unfailing love.
1. Introduction
Humans are social creatures. Our survival as a species depend on our daily interactions with our fellow humans. Over a period of many thousand years, we developed sophisticated techniques to facilitate such interactions. One of the techniques we invented is the human language.
Over the course of multiple millennia, we have created a multitude of languages. Those languages also vary vastly in complexity, starting from simplest languages such as Pirah to complex languages such as Mandarin.
Although languages help us to communicate with each other, quite often it is the case that we are unable to convey exactly what we mean. It is up to the other person to interpret our words in their own context. This creates misunderstandings, which could result in conflicts. This happens because most of the commonly used human languages evolved over many centuries, instead of having been explicitly designed. This gradual evolution resulted in transformation of words and their meanings, resulting in a much richer context. This richness enable us to create beautiful poems that have multiple layers of meanings within them. We are able to understand such poems because we are quite good at contextualizing what we read or hear.
However, there are situations where having multiple interpretations cause issues. One such case is within legal proceedings, where lawyers often spend countless hours in preparing statements that have precise meanings and very little room for alternate interpretations.
Another domain in which we need precise language is in computer programming. Computers are unable to autonomously contextualize instructions given to them. Therefore, we need to ensure that the instructions given to a computer is unambiguous.
There are thousands of computer languages in the world. These languages helps one to translate their ideas into instructions that a computer can blindly execute. These languages vary a great deal on how a person can express his or her ideas.
There are many ways through which one can instruct a computer to perform a series of tasks. The art of programming often involves on finding the way which is suitable for the set of trade offs you are willing to take. Sometimes you want a trade off between performance and understandability. On other times you would like to have the best performance possible.
In this book we will learn how to program using Microsofts TypeScript language. This book does not intend to be a tutorial for TypeScript, but instead it aims to teach you how to program.
2. Compilers and interpreters
In order for a program to execute tasks on a computer, the program must be in a form that the computer can understand. This form is called the Machine Language of the computer.
A Machine Language is very different from a human language and therefore is very difficult for a human to understand. In order to make it easier for humans to program, a language more suited for human consumption is used. This language is then translated to the machine language using a translator, usually called a compiler.
Computer languages vary greatly on ease of programming. Some languages are very low level - sometimes just one step above being a machine language - these are called Assembly Language . Most of the programmers use higher level languages to program a computer. These languages allow one to program in languages that are close to human languages, while still being strict enough to be translatable easily into machine languages.
Translation from a high level language to a machine language can be done in two different ways. Either the translation is done before executing a program, or during a program is being executed.
To translate a program to machine language before its execution, an application called compiler is used. To translate a program to machine language during its execution, an application called interpreter is used.
You may have noticed that when we have to perform some complex tasks, it is more efficient to prepare for those tasks beforehand than exactly at the moment the task is being performed. This is the same approach that the compilers take. Basically compilers look into the program and translate them into machine language. During the translation it performs complex optimizations - basically finding ways to make the program execute as efficiently as possible.
Interpreters are more lazy. They translate the program at execution time - this means that they cannot spend much time on optimizations - as this will make the overall execution of the program slower. This means that programs running under interpreters are less efficient than those that have been compiled by a compiler beforehand. On the other hand, interpreters are more convenient for programmers because they can instantly execute a program without having to go through a separate compilation step.
Another common approach to execute a program involves using both the techniques at the same time. In this, the program is compiled by an application in the background during startup, while giving the impression to a user that it is executing the program like an interpreter. This approach is called Just-in-time compilation.
In this book we will use Deno runtime, which has a just-in-time compiler underneath that compiles and then executes our Typescript programs.
Visit the website https://deno.land/ to download the Deno application. The following chapters would use Deno to demonstrate different programming techniques.
3. Quintessential Hello World
A famous tradition popularized by the seminal book The C Programming Language is to start learning a programming language with a program printing "Hello, World!" on the computer display.
Without much ado, lets start.
Please open a text editor of your choice and type the following and save the file as HelloWorld.js . Note that the content of the file is also known as the "source code" of the program you are about to execute.
console . log ( "Hello, World!" )
Now open a command line program and enter the following in its terminal.
deno run HelloWorld.js
You should see the following in the terminal.
Check file:////HelloWorld.js
Hello, World!
Bravo! You have successfully executed a program.
3.1. Behind the scenes
As you saw, the program printed some text on screen. Lets see what goes behind all of that.
3.1.1. Strings and Characters
The first piece of code we will look is the text 'Hello, World!'. In many programming languages, a sequence of characters is called a string . Here the text 'Hello, World!' forms the following string of characters.
'H' , 'e' , 'l' , 'l' , 'o' , ', ' , ' ' , 'W' , 'o' , 'r' , 'l' , 'd' , '!' .
A character may be used to represent a number, or an alphabet from a given language. There are thousands of different alphabetical and numeric characters in the world. In order for computers to process such characters uniformly and across different operating systems and hardware, there needs to be standards. First step to standardize such character processing is to assign unique identifiers to characters, so that computers can process these characters the same way, irrespective of the fact that we are writing in English or Chinese. Assigning unique identifiers to characters in order to make it easy to process is called character encoding.