Language: EN

programacion-estructura-de-un-programa

Program structure

We start talking about programming through high-level programming languages (yay!🎉) To begin with, the first thing we need to discuss is the structure of a program.

By program structure, 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 perform a specific task.

A well-defined program structure makes the code easy to read, understand, and maintain. Additionally, a good program structure also makes the code faster to write, debug, and improve its performance (all of this translates into some cash 💸).

Code Examples

The best way to see what a program structure is, is to get our hands dirty, and put 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 that is used as an example to teach a programming language because it is simple and basic.

Alright, let’s go for it!

Here’s 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 reach enormous sizes—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 consist 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 see that they have many things in common. For example,

  • Programs consist 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 obvious 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. 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.

Moreover, just like in languages, some languages are more concise, while 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 differences in syntax. 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 concept of to have, bicycle, and yellow.

Well, in programming languages, it’s similar. Only in rare cases, and in very complex aspects of the languages, do we find real differences (which, by the way, is the most interesting part to compare, and we will see it in this course 😉).