Introduction to Computer Programming

Programming is my favorite hobby. It is a fun and entertaining activity that is also full of learning opportunities. Given that we learn a lot when we try to explain a topic to someone, this text should help me learn a great deal. I'll try to guide the reader from knowing nothing at all about computer programming to being able to develop a complex software. There will be no tricks, though. In this journey we will need to read and understand code in several different complex programming languages and by the end of it, that will have been the easy part.

Bits

Let's start. First things first. You are probably aware that computers work in binary. That means that computers deal with zeroes and ones only. On top of these zeroes and ones languages are created. It is not difficult to grasp how this is done. We can represent any number using only zeroes and ones. Check out this table:

numberbinary
00
11
210
311
4100

You got the idea. There is a problem, though, if we have a large stream of binary digits, how can we tell when a number start and the other ends? Consider this stream of binary digits: 10010110. Should it represent a single number (one hundred and fifty) or should it represent several numbers, that is 1001 (nine) 0110 (six) for example?

We can agree on specific lenghts of streams of binary digits and simply preceed smaller numbers with zeroes. In the previous example, if we assume that each number will always be represented by four binary digits, then we would have two numbers: nine and six. However, if we agree that we will represent numbers with eight digits, we will have a single number, one hundred and fifty.

This is per se an important idea to hold: we are representing things in binary digits using specific conventions. Should we use a different convention, a whole different thing would be read from the same binary digit stream.

You may have noticed that I am repeating binary digits a lot. Let's get rid of this. Let's call a binary digit a bit. Now, the thing about bit streams is that we need to agree on a chunk size so that we can represent things with it.

It is particularly useful to use chunks of lenght eight. One of the benefits is that they can be easily represented with Hexadecimal numbers. The number 15 is represented in digits as 1111 while in Hexadecimal it is represented as F. Now, the number 255 is represented in bits as 11111111 while in hexadecimal as FF. Now, a color can be represented as three numbers, one for the amount of red, another for the amount of green and the third for the amount of blue. Each of these numbers can be anything between 0 and 255. The representation of white in RGB using bits is 111111111111111111111111. That is not really easy to read. In hexadecimal it is represented as FFF, which is much easier.

OK, we can now represent numbers using bits. But we need to represent much more, specially text. The key to achieve this is the same as before: the chunk lenght. Say we agree to use chunks of size eight. Well, we can represent any decimal digit using only ten different bits, but we have 256 possible combinations. We can use these combinations to represent any number and character of the English alphabet and still have plenty of room.

The ASCII - American Standard Code for Information Interchange does exactly that. It defines bit patterns using 7 bits to represent any character. The letter "A", for example, is the pattern 1000001, or 41 in hexadecimal. The "?" symbol is the hexadecimal 3F.

You got the idea. Now we can represent any text. And with text we can represent anything.

Text files

Programming languages are basically text and are written in files containing only text. Word processors like Microsoft Word, LibreOffice or Google Docs contains a lot more than text. They contain both text and something else to represent formatting, images, sometimes even sounds. Files such as docx and odt are not text files.

Text files are those .txt that literally contain only text. That is what we will be working with when we are programming. If you want to check this out, you can open any website, then click with the right button and choose "see source code". You will see something that resembles a txt file with lots of code in it. You will be writing these files.

To start, open any text editor available in your machine. On Windows you can use Notepad. On Linux you can use Kate, Gedit, or any other text editor. We will learn more about text editors in the future. Now let's create one.

Open a text editor and type anything you want. Traditionally, people would write "Hello, World!", but feel free to do as you please, then save it as mypage.html. Next, double click the file. If the text editor open again, then right click it and choose to open with a Browser.

You will see your text on a web page you just created.

Now let's make it a little better. Let's create a title for it. Open the file again using your text editor and create a new paragraph before that one. You can write any title you want, but you should surround it with a title "tag", like this:

<h1>My title</h1>

Now check you file again. You will notice that the title font is bigger and bold. Also, it's margins are bigger. You did all that by using the h1 tag. Actually, you made the browser aware that that text was a title, so the Browser styled it as a title.

Almost everything a programmer works with are text files. We use code to represent everything, and code is basically text.

Command Line Interface

Most computer users use programs fit for their needs, and that usually means to have some buttons, some fields and some gestures available to them. Although these interfaces are easy to learn, they are not efficient because they are not programmable.

Programmers usually use a command line interface, that is, a terminal where they type commands so that the computer can execute. This interface provides a huge advantage: you can save your commands in files and execute them again whenever you like. More than that, you can literally program your computer to execute them in a given time or when a given event happens.

Now, we will need to face an important issue: different Operational Systems behave differently and I am about to start using the Linux Command Line Interface. If you are using a Mac this should not be a big deal as Linux and Mac behave similarly with this respect. If you are using a Windows, you should now install a Windows Subsystem for Linux following this guide

You will learn a lot about the Command Line Interface (CLI for short) throughout you programming journey. You can check this post to get a basic introduction about it. For now, though, let dive directly on doing thins with it.

First, let's open a terminal. Each OS does this differently, so I'm assuming you can figure out how to open a terminal in your system. If you are using Windows, open the Subsystem for Linux, not the Windows terminal.

So that we won't pollute your filesystem, let's first create a new directory for us to play. Type the following command and then press Enter:


    mkdir playground
    cd playground
    

The mkdir command creates a new directory. "playground" is the name of the new directory we created. We also say that playground is the argument that was passed to the mkdir command.

The cd command changes the directory your are working in. Again, "playground" is the argument we used, so we are now in the playground directory.

Now let's start to see some of the power of the CLI. Press the Arrow Up key until you find the mkdir playground command again and press enter. You will notice that now you created another playground directory inside the playground. Commands are repeatable! It can be nicer than that. Press Ctrl+R then start typing a part of the mkdir command. You will notice that the terminal will autocomplete the command for you, based on the history of the commands you have already used.

Ok, we can create directories. Let's improve on this. Use the following command to create several directories at once:


    mkdir how nice many directories
    

Now we have created four directories at once. We can do better, let's create a directory structure for an imaginary project. Our project should have a main folder called "project", with a folder inside for the code, called "source", another for the documentation, called "doc" and another for tests, called "tests". Also, inside each of them we should have three folders, one called "app", another called "styles" and a third called "assets". Here is a command that will do that:

mkdir project/{source,doc,tests}/{app,styles,assets} -p

You already know that mkdir is a command and that project/... is an argument. There is something different about this argument, though. First of all, we have two arguments. They are separated by a blank space. project... is one argument and -p is another argument. Second, there are these curly braces surrounding some words in the first argument.

I think this should be enough to convince you that it is worthwhile to learn to use the CLI. The sooner you realize this, the better.