Sunday, March 14, 2021

Procedural Thinking

What is programming?

Programming is the act of instructing computers to carry out tasks and is often interchangeable with the term coding.

What is a computer program?

A computer program is just a sequence of instructions that a computer executes step by step. Nowadays we also refer to programs as applications.

You can think about this sequence of instructions using an every day example. Let's use "making a peanut butter sandwich". As a human you might:

  1. Get the ingredients

  2. Get a knife and plate

  3. Place bread on plate

  4. Spread peanut butter

  5. Eat

but this isn't quite how a computer works. A computer needs really specific instructions. Just "get the ingredients" alone might not be specific enough! A computer will do exactly what it's told. And when there's not enough instructions it might act in a completely different way to the original intention.

A good lesson from this is a computer will do exactly what you tell it to do, no more or no less. So the fault never lies with the computer but simply with the instructions provided.

What is an algorithm?

An algorithm is just a sequence of code instructions. For example, you might have an algorithm to sort words into alphabetical order.

What language do we use to issue our instructions?

In the peanut butter example we used the English language but a real computer doesn't understand English!

Computers have their own natural language just like humans called binary. Binary represents two different states, on which is indicated by the number 1 and off which is indicated by the number 0. Now if we had to code in the computers natural language of binary it would be really really hard because as a human we don't speak like that. To solve the problem where a human needs to be able to write instructions for a computer to understand we use what are known as programming languages.

Programming languages are closer to our own natural language but way more structured. There are both high level and low level programming languages. The difference being is that low level languages are more similar to the machines language (binary), where higher level languages are more similar to how a human would speak. In this course we will learn a couple different higher level languages (Ruby and JavaScript).

Programming languages are great and everything but still a computer only understands binary. So how does a programming language become binary? Well just like moving from any language to another we need to use a translator! A translator turns our code written in a particular programming language to binary for the computer to understand and execute. This is done in several different ways but we will focus on two which are interpreting and compiling.

Whether a programming language is compiled or interpreted depends on the language itself. Some compiled languages you may have heard of are C, C#, Java and Go. Compilers will translate all the lines of a program to binary file and then execute the entire file. If there was an error during compiling (you made a mistake), then the compiler will let you know that an error has occurred and the code could not be compiled.

Interpreted programming languages include Python, PHP, JavaScript and Ruby. The way interpreters work is that they will translate a single line of code into binary, execute that binary and then move onto the next line, until it runs every line of code. If an interpreter ever runs into an error than the entire program will stop and the error will be reported. This is a significant difference between compiled code because your app will run even if there is an error somewhere within your code! And once the app gets to that error it will crash.

For now we will focus on the programming language Ruby. Lets write some code and get our computer to execute it!