Was Go really made for stupid programmers?

Programming for non-programmers

introduction

When I write specifications I often think the reader will understand my "writing". So if I write: “As an accountant, I want to enter currencies with their international ISO standard code so that I am not misunderstood by other employees”, would everyone understand what I mean by ISO standard?

Of course, in the example above, I would just enter the reference and say: "Read the manual". Unfortunately, it is often not that easy. In order to write understandable specifications, one has to understand the readers of the specifications - in particular the programmers. And that is the challenge: you are not a programmer and the thinking of programmers is really very special.

Therefore I try to explain the programming principles in this blog post. It is not intended to make you a programmer. But then you may find it easier to write specifications that programmers understand.

A really stupid robot

To understand how programming works, let's use a real stupid robot to explain. Please get me right: robots are stupid, but this one is a little dumber. You have to tell him everything he has to do. Nothing, really nothing is given. If you say, "Go!" He will go and not jump. Even if there is a stone in the way, it will try to leave because you didn't say, "Jump!"

The programming principles can be explained very well using such a robot. For those of you who are even more interested, I've added a little more information in the appendix.

variables

Variables are very useful when programming because they can be used to store almost anything that we would like to store in a computer. Variables can represent numbers, simple letters, text or complex structures such as a to-do list with a due date or a weather map.

Our robot only has two variables: steps and energy. The step variable gives him the number of steps he can take in one round. The energy variable contains the energy that he has left. When the robot takes a step, the energy in one part is reduced. In a program it would look like this:

energy = 5;
step = 2;

You may be wondering why every line has a semicolon at the end. This is because I very often use C-like languages ​​such as C # (pronounced “Cie” sharp), Java or Objective-C. All of these languages ​​were developed on the basis of the C programming language, which was introduced in the 1970s. These languages ​​indicate the end of a command with a semicolon. You have probably seen Visual Basic Code as used in Excel. There the end of the command is simply indicated by the end of the line, without any additional characters.

Okay, but our robot just doesn't do anything. Did I mention he's really stupid? We have to tell him what to do with his variables. We do that in functions.

Functions

Functions tell the computer what to do. If our robot is to walk, we need a function that tells it how many steps to take and how its energy is reduced with each step. The number of steps is passed to the function in parameters. When we call this function, we have to pass the number of steps so that the robot knows how many steps to take.

Such a function looks something like this:

Now I have to explain a few things:

  1. There is a void in front of our function. Functions return values, e.g. a function returns the sum of two values ​​that were passed as parameters. Our function does not return anything - the robot is only supposed to WALK. Therefore we wrote a void in front of the function, but that does not mean that we are lost in the nothing of programming.
  2. We used curly braces. Curly brackets tell our computer where a function begins and ends. So we can write a lot of functions one after the other without confusing the end of one function and the beginning of another.
  3. We used the words function and go. Function tells the computer that the following text is a function. Such words are "keywords" and are well defined for any programming language. And: void is also such a keyword.
  4. "For (int steps = ..." is a loop. Loops are used very often in programming. No programmer wants to have to write the same things over and over again. Therefore, they use loops. Loops are executed as long as a certain condition is met - in our case, as long as the variable “steps” is smaller than “numberOfSteps”. All these things belong to the parameters of the loop, which is indicated by the round brackets. The curly brackets, on the other hand, indicate what should be done while the loop is running is repeated.
  5. The same is true for abbreviations. Because programmers are usually lazy, they don't want to write too much. Instead of steps = steps + 1, write steps ++. That doesn't mean anything else, steps is only increased by one as long as the loop is repeated. The same goes for energy. It is decremented by one every time it goes through the loop

With this feature we can make our robot run two steps:

static int energy = 5;
int steps = 2;
go (steps);

After the robot has done its job, the energy is reduced by two because it is reduced by one for each step within the go () function. We have defined a variable outside of the function. Usually variables can only be used within the context (indicated by the curly brackets) in which they were defined. But we have defined a global variable energy that can be used anywhere - so this is a global variable.

Objects

Objects are very important if you want to use modern programming languages. So-called object-oriented programming languages ​​were introduced in the late 1980s. Before that, the programming languages ​​were purely procedural.

That means: programmers have "only" defined procedures or functions and then applied them to data. This often caused errors because the functions could not be applied to the data currently in memory. E.g. it makes no sense to call our function go () on data representing a house because a house cannot normally move.

Object-oriented programming (or OOP for short) has solved this problem. We define data and the functions applicable to it together. In our small example we define the function go together with the object on which it can be applied - the robot.

By the way, functions are called “methods” in object-oriented programming languages. That means nothing else, but when the new programming style (or better: the new programming thinking pattern) was introduced, one wanted to have a different name for this different type.

When we define a class, we define a class. A class is a template for an object. It tells the computer how to create an object. If we need multiple robots when our program is running, I only need to define this once when I program.

The beauty of OOP is that objects can contain other objects. So our stupid robot contains a head, a body, arms and legs. To implement the goforward () function, we should implement some movements of at least the arms and legs. The body and head should also move, otherwise our robot would leave its head behind.

Debugging

Let's assume that we have implemented the goforward () method with the following code.

Now we can run our little program and see what happens.

Oh dear - our robot collapsed. We call this a bug. The program we created is not working as we expected. Now we have to find the bug by debugging - that is, we run the program line by line.

  1. leftleg.up () - OK, the robot lifts its left leg
  2. rightar.moveforward () - OK, the robot is moving its right arm forward
  3. rightleg.up () - That's not OK, the robot is in the air and is breaking.

We found our mistake: we forgot to put the left leg down before lifting the right leg so the robot can really walk. OK - we're bugfixing our program (that is, we're going to fix the bug).

We'll run our little program again after debugging and see that it works.

Summary

I hope you had as much fun with our little story as I did and that you understand better how programmers think. Of course, this was a really simple example, but it shows that programming is really hard work. And that sometimes it can be really frustrating when there are more bugs in the code than you thought. Please let me know if you have any more information about programming or software design.

Appendix: Various programming languages

There are different programming languages ​​that look similar but are very different. Since we have different operating systems, we also need different programming languages.

Our example in Java:

Java is often used to program server tasks, e.g. when you want to receive a message from a user that was sent by another user. Usually Java is chosen here, since most server operating systems understand Java. But it is also used to program Android smartphones.

Our example in Objective C:

Objective C is a programming language introduced by Apple. It is used for all Apple devices such as Mac, iPhone and Apple Watch.