Technology
August 12, 2015

Programming for non-programmers

Introduction

When I write specifications, I often wonder if the reader understands what I am trying to say. For example, if I write: “As an account clerk, I want to enter currencies using the ISO standard so that I am not misunderstood by other employees,” would everyone understand what I mean by ISO standard?

Of course, in the given case, I could simply quote a relevant source and say “read the manual”. But it is often not that easy. To write understandable specifications, one has to understand the people who read these especially programmers. And that’s the challenge – because you aren’t a programmer and programmers think in a very special way.

This is why I will try to explain the principles of programming in this blog. The idea is not to make you a programmer, but it may make it easier for you to write specifications that can even be understood by programmers.

A Really Stupid Robot

RobotGoTo explain how programming works, we will explain it by using a stupid robot. Don’t get me wrong, robots are stupid, but this one is REALLY stupid. You have to tell it everything it has to do — nothing, absolutely nothing is taken for granted. If you say: “go”, it will go and not jump. Even if there is a stone in its path, it will still try to go, because you didn’t say: “Jump”. 

The principles of programming can be explained quite easily using such a robot. For the more curious among you, I have provided more information about computer programs in an Annex.

Variables

Variables are very helpful when programming because we can store almost everything in them that we want to store in a computer program. Variables can be numbers, simple characters, a text, or even complex structures like a to-do list with due dates or even a weather map. 

Our current robot has only two variables – step and energy. The step variable tells it how many fields it can move in one turn, and energy how much energy it has left to go further. If the robot moves one field, one piece of energy is reduced. In a program it would look like this:

energy=5;
step=2;

You may be wondering why there is a semicolon at the end of each line. This is because I often use C-like programming languages such as C# (pronounced C sharp), Java, or Objective-C. All of these have been developed on the basis of the programming language C, which itself was introduced in the 1970s. These languages indicate the end of a statement with a semicolon. You have probably seen Visual Basic code — used for example in Excel where the end of a statement is indicated simply by the end of the line without any sign.

OK, but our robot is still doing nothing. Did I mention that he’s really stupid? We have to tell it what it has to do with its variables. And we do this in functions.

Functions

Functions are things that tell our computer what to do. So if we want our robot to go, we need a function that tells it to move a certain number of steps forwards and how much of its energy is used up with each step. The number of steps are set in the parameters of the function. If we call the function, we have to send the number of steps so that the robot knows how many steps it has to take.

This type of function looks something like this:

Ups, I think I forgot to explain quite a few things.

  1. There is a void before our function. Functions can return values, e.g. a function for a sum returns the sum of two values set as parameters. Our small function returns nothing — the robot should only GO. We therefore placed a void in front of it — which doesn’t mean that we are now lost in the void of programming :-).
  2. We have used circled brackets. These brackets tell our computer where a function begins and ends. This allows us to write a number of functions in succession — without becoming confused about where one ends and the next one begins.
  3. We have used the words “function” and “go”. “Function” tells the computer that the following text is a function. These words are called “keywords” and are precisely defined for each programming language. By the way, void is such a keyword as well.
  4. “for (int steps=……” is a loop. Loops are often used in programming — no programmer wants to have to type in the same things again and again. So they use loops. Loops are repeated as long as a condition is true — in our case, as long as the variable “steps” is smaller than “numberOf-Steps”. All of these things belong to the parameters of the loop, which is indicated by the round brackets, whereas the circled brackets indicate what should be done as long as the loop is repeated.
  5. The same thing applies for shorthands. Because programmers are naturally lazy, they don’t want to write too much. So instead of writing steps=steps+1, they write steps++. Which means exactly the same – steps is simply increased by one each time the loop is repeated. The same applies for energy. It is decreased by one each time the loop is run.

So we can let our robot take two steps with the function.

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

After the robot has done its work, the energy is reduced by two — because it is reduced by one for each step inside our function go(). We have defined a variable outside of our function. Variables can normally only be accessed within the context in which they have been defined — indicated by the circled brackets. But we have defined a global variable “energy”, which can be accessed from everywhere — making it a “global” variable.

Objects

Objects are very important if you want to work with modern programming languages. So-called object-oriented programming languages were introduced at the end of the 1980s. Up until then, programming languages had been procedural.

This means that programmers “only” defined procedures or functions and applied them to data stored in the memory. That often caused errors because the functions could not be applied to the data currently in the memory. For instance, it makes no sense to call our function go() for data representing a house, because a house usually can’t move.

Object-oriented programming (OOP for short) solved that problem. We define the data and the functions which can be applied to each other. In our small example, we define the function go together with the object to which it can be applied — the robot.

Btw, in an object-oriented language a function is called a method. It doesn’t mean anything different, but when the new style (or better paradigm) was introduced, one wanted to have a different name for a different thing. If we define an object, we define it as a class. A class is a template for an object. It tells the computer how to create an object. So if we need several robots when the program is running, we only have to define it once when programming.

The nice thing about OOP is that objects can contain other objects. Our stupid robot, for example, 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 move as well, otherwise our robot will leave its head behind.

Debugging

Let us assume that we have implemented the method goforward() with the following code:

We can now run our small program and see what happens. RobotBug

Oh dear– our robot has collapsed. That is what we call a bug. The program we have created doesn’t run as we expected. We now have to find the bug by debugging – which means we run the program line by line.

  1. leftleg.up() – OK, the robot lifts its left leg
  2. rightarm.moveforward() – OK, the robot moves its right arm forwards
  3. rightleg.up() – That’s not OK the robot is in the air and crashes.

We have found our bug – we forgot to put the left leg down before lifting the right leg so that the robot can actually walk. So we bug fix our program (which means we correct the mistake).

We can now run our small program again – and see that is works. RoboterSuccess

Conclusion

I hope you have had as much fun as I have with our small story and now understand more about how programmers think. Of course it was a very simple example, but I think it shows you that programming really is hard work and can sometimes be really frustrating, especially when you see that you have programmed more bugs in your nice little code than you ever thought possible. Please let me know if you want to know more about software programming or even design.  

Outbreak — Different Programming Languages

We used something that looks like C in our small example. But there are several programming languages available and although they may look alike, they are in fact quite different. Different languages are used on account of the different operating systems of the computers, or on account of the different tasks which have to be solved by programming.

Our example in Java

Java is often used to program server tasks — for example if you want to receive a message from one user that has been sent by a different user. This is why most server operating systems understand Java. It is even used to program Android mobiles.

Our example in Objective C

Objective C is the programming language introduced by Apple. It is used to program all Apple devices such as Mac and iPhone.

   

annegret

Dr. Annegret Junker

Enterprise Architect Cloud

I have been in the software industry in various positions for more than 20 years. For the past few years I have focused intensively on methodologies and architectures. My blog addresses a variety of issues regarding current software architecture and methodologies in software development.

Leave a Reply

Your email address will not be published.

eleven − eight =

Jump to the top of this page