How Does Programming Work?
A program is a set of instructions given to a computer for it to follow.
Writing a computer program involves a programmer writing code with the help of a programming language.
It’s the same as writing an article in a chosen language like English. A program gets written in a chosen programming language.
There are two types of programming languages : high-level and low-level.
Low-level languages are closer to the native language of a computer which is binary, only numbers.
High-level languages are farther away from binary code and use words and symbols, making it easier for humans to read and write in.
Most programmers write code using high-level languages. Some examples of high-level languages include Python, Java, C, etc. This code that the programmer writes is considered “source code”.
Computers, however, do not understand source code, they only understand machine language. So, in order for the computer to comprehend and execute the source code, it has to be converted into machine code.
Machine code is binary code which is literally 1’s and 0’s. The computer only understands 1’s and 0’s, yes or no, true or false, there’s no in between.
Programmers on the other hand are humans and we, for the most part, understand things through words and symbols. This is where high-level programming languages come into the picture. In the technology world, we are lucky enough to have these languages which are easy to write in because of the nature of them.
Here’s an example of what code written in python looks like:
This program would output:
Even if you were not a programmer, you could easily decipher what this program is trying to do because it’s written using symbols in which you are familiar with and in a language which you understand — English. It’s human readable.
Computers on the the other hand, understand machine language which is not easily readable by humans.
Here’s an example of what binary code looks like:
This binary code is saying:
Most programmers would have no idea what that binary code says.
So, how do programmers convert their source code into machine code? There are two ways you could do that, either compile your source code or interpret it.
All programming languages can either be compiled or interpreted, it’s really left up to the programmer’s discretion. However, certain languages were designed to be converted using one particular method, so we choose wisely.
Here are some of the pros and cons of both,
Compiling — pros:
Fast — programs that are compiled tend to run faster because the source code is translated all in one shot.
Privacy — programs that are compiled are private, this is because the programs are turned into machine code before the program is ran. If you are doing something like selling software, you definitely want your source code to be private.
Compiling — cons:
Not cross-platform — this means that your source code has to be recompiled for every single type of platform like Mac, Windows, Linux, Android, etc. Your program might work on a Mac but will not work on a Windows unless you recompile your source code to do so.
Inflexible — you can’t fix your mistakes, which means it takes an extra step to debug.
Interpreting — pros:
Cross-platform — this means your program will run on every single platform it comes across without you having to take any extra steps, it does this on its own.
Immediate results — therefore, you can easily fix your mistakes.
Interpreting — cons:
Slow — with interpretation the source code gets translated line by line which will obviously take longer.
Public source — anyone can read/view your source code, this is because with interpretation, your source code gets translated into machine code at exactly the same time the program is ran.
Nonetheless, compiling and interpreting both do the same thing — they translate source code into machine code. Same end goal, slightly different process to get there. When deciding which method of execution to use, there are certain things you want to consider. For example, is maximum speed important? Do you want your program to be able to run on multiple platforms?
All in all, we are fortunate that computer programs like a compiler and interpreter exist because it allows our computers to understand the awesome programs we create written in human readable code making programming that much easier and fun!
Thanks for reading, happy coding!:)
Karina