We begin talking about programming using high-level programming languages (yay!🎉) To start, the first thing we need to talk about is the structure of a program.
By structure of a program, I mean the way the code is organized and structured. It’s how we divide our code into parts, and how they relate and interact with each other to carry out a specific task.
A well-defined program structure makes the code easy to read, understand, and maintain. Furthermore, a good program structure also makes the code faster to write, debug, and improves its performance (all of this translates to money money 💸).
Code Examples
The best way to see what this “structure of a program” thing is all about is to get our hands dirty and look at a Hello World program in different programming languages.
A “Hello world” is a program that simply displays the message “Hello world” on the console. Traditionally, it is the program used as an example to teach a programming language because it is simple and basic.
Alright, let’s do it!
This is what a “Hello world” program looks like in C++
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
While in C# we could do it like this:
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, world!");
}
}
In JavaScript it would simply be:
console.log("Hello, world!");
Finally, in Python it would be:
print("Hello, world!")
Of course, this is just a very simple example, for a first contact. In a “real” program, the number of lines can be huge—up to millions of lines!
Logically, keeping your program’s structure clean and easy to maintain will be one of the most important points as your program grows.
Similarities and Differences
Let’s look at the similarities and differences between the codes. Let’s start with the most obvious. A program in a high-level programming language will be made up of lines of text (which is what we call code).
These lines are compiled or interpreted by the computer to perform the actions we want.
Even in such a simple example, we can see that they have many things in common. For example,
- Programs are composed of lines (or statements)
- Statements can be grouped into functions or blocks
- The language provides certain predefined functionalities (in our example, displaying text on the command console)
However, there are also evident differences. The lines are different, the words are different. In general, the same program, in different languages, has a different form.
This shouldn’t seem too strange either. In the end, it’s like speaking Spanish, French, or English. What you want to say is the same, but the words and phrases you use are different.
Also, as happens with languages, some programming languages are more concise, and others are more “verbose”. That is, some languages need more words than others to express the same thing.
But don’t be overwhelmed by the syntax differences. The important thing is that, more or less, all languages work the same way. I don’t know how to say I have a yellow bicycle in Chinese. But I’m almost sure they have the concepts to have, bicycle, and yellow.
Well, it’s similar with programming languages. Only in rare cases, and in very complex aspects of languages, do we find real differences (which, by the way, is the most interesting thing to compare, and we’ll see it in this course 😉).
