Culture. Nurture. Tincture. Enrapture.

How to program ANYTHING

Let‘s face it: to newcomers, programming can seem unapproachable. Resources do exist to learn, but more often than not these are limited in use, due to the ever-evolving scene where features come and go in a matter of months. More evergreen alternatives do tend to overburden learners. In truth, most intricate details are unnecessary to create flexible and powerful programs. By mastering just a few general concepts, you can become proficient in any programming language in no time. In this post, I will break down these concepts to kickstart your programming journey. So, whenever you‘re ready, bring out your keyboard and let‘s jump in.

Variables - The alter-egos of programming

At its core, programming is our main way to tell a computer exactly what to do. By writing a series of instructions - a program, our computers can simply read those and do the rest of the job for us, whether it's an algorithm, a software or a website. Computer scientists had to agree on how these instructions should be handed down to computers. That‘s basically what we call programming languages. Many different programming languages are used depending on which industry you target, each with its syntax and features. Nevertheless, any programming language is a way to give instructions that a computer can understand. What can they understand? Fundamentally, those are binary sequences (series of 0s and 1s, as you might have seen) but, to us, this is equivalent to say information. This is where our first concept comes in. To learn how to program, you need to know how to store and manipulate data. This is done by assigning what are called variables.

Creating a variable can be as easy as writing “x = 2“. Such an instruction tells our computer two things. First, we are basically telling it that we have the information we want to store. In return, the machine will secure a piece of its memory for us to use. That is in essence our variable! Second, we assign a value to it. In this case, we put the numeric value “2“. In that way, variables are memory slots that can hold any piece of information we desire. The strength of variables comes in when we consider that we can manipulate this information. Say we use the same variable as above, then write this instruction: “x = x + 2“. In that way, we asked our machine to add 2 to our variable. No need to remember what “x“ stands for exactly, our computer friend will assign the correct value we need. Whether our two instructions are one right after the other or distanced by a thousand instructions: it will still work. I should point out that the ORDER of those two instructions is important. Programs are read sequentially by machines. Therefore, if we ask it to do “x = x + 2“ before we even create the variable with “x = 2“, it won‘t understand. Oh, and you should know that we can pretty much assign as many variables as we wish. Going back to our previous example, you could have written “y = x + 2“ instead. This would have kept our variable “x“ with a value of 2 and created a new variable called “y“ with a value of 4. You can use as many as you want, but remember that every variable we create uses additional memory. Not really a limitation anymore but you should still strive to create no more variables than needed. Thrust me: you will thank me later.

All variables need a unique name. This is important, as this is how our computer knows which piece of information we want to work with. In our previous example, that would be “x“. They also need a data type. Variables store data of a specific nature. This is important, as this tells our computer how to treat this information and determines what operations can be performed on it. Fundamental data types include: numbers (often separated on whether they have decimal values or not - respectively called integers and floats), strings (sequences of characters) and booleans (true or false). In the past, variables containing references to memory locations for other variables (more often than not “pointers“) were used. Today, you should not have to work with memory allocations except if you work with legacy languages (say, FORTRAN for example). The last important group of variables you should know about are containers, or variables with multiple information. These pieces of information could be in a specific order or not and hold a variety of information types. Typically, lists contain a sequence of information, arrays are used for matrices (see them as close to data tables) and dictionaries hold pairs of keys (unique identifiers) and their values - useful in more complex programs.

That‘s pretty much all you need to know about variables for now. Still, here are a few things I should point out that vary from language to language. Don‘t stress too much about these if they confuse you. Just remember that they exist:

  • Not all programming languages can have their variables change value during a program.
  • Some programming languages require you to specify the data type of your variables when you create them. For others, you can specify it separately. Others still will try to guess what type of data you have depending on how you want to use it. In all cases, always better to specify your data types to avoid issues.
  • In many programming languages, WHERE you create your variables can have an impact. Variables can be local (accessible only to a specific section of code) or global (accessible anywhere throughout the program).

What if? Then what else?

Until now, we considered a fixed sequence of instructions that allows us to work on some data. But what about if we want to process our information differently depending on the nature of it? Being able to manipulate data is great, but the power of programming really comes in when we consider the flexibility that conditional instructions bring. In most programming languages, conditional statements look like this: “If CONDITION …“. Simply speaking, the “if“ indicates to our machine that what to follow is a condition to check. This condition should generally return in some way a true or false statement say, for example, is a specific variable value more than 2 (“x > 2“). If the expression is true, then the computer will follow the instructions follow the conditional statement. These are often separated from other instructions by a specific indent or closing character. In the case that the expression is false, the machines simply skip those instructions to continue after the conditional block of code. Generally, you can also specify instructions to run ONLY if the statement is not verified. In that case, the conditional block would look more like this: “if … else …“. You can also expect to have “if else“ or “switch/case“optional branches for your conditional statements. These allow programmers to check multiple conditions in the same block sequentially.

Frankly, that‘s about all you need to know about conditions. As inoffensive as it might look, this is paramount to avoid unnecessary computations and add flexibility to programs. This is also how machines can, in a way, make decisions while executing our code depending on inputs, variables, states, etc.

Over and over again

Now: for the final piece of our programming puzzle. Being able to dynamically execute instructions is terrific, but we can still have lots of repetitive instructions for more complex tasks. Sometimes, we need to repeat some instructions hundreds of thousands of times if not more. That‘s where recursion structures come in. The most simple example would be what‘s called a loop. It could look like this: “While CONDITION … end“. Just like conditional statements, the word “while“ is an indicator to the machine that instructions to follow should be repeated until the condition is no longer valid. Again, not too complex. Still, this lets us solve complex problems by breaking them down into simpler, repeatable steps.

Instructions loops are an excellent tool, but possibly the best recursion structures are functions and classes. A function can be seen as a block of code used as a variable. Just like variables, functions need a unique name and have to be defined before being used. Defining a function could look like this: “test_function(var_input): … return“. As stated, functions need a name and a set of instructions (here “…“) but I have included two other aspects they can (though not necessarily) have. First, functions can have other variables as input (here “var_input“). This variable can therefore be given to the function when it's called. Second, functions can actually return things - whether it is a value, a variable or another function itself. Think of all the implications of what we observed so far. We could for example create a conditional statement where a function is called when the condition is true and another function when it is false. Functions will quickly become your best friend. Finally, classes are the final tool you should know about. Classes are, in a way, the Swiss army knife of programming. These structures can encapsulate a specific set of functions and variables into one convenient package. This is generally used to represent objects. For example, say you create a “Ball“ class. By calling this class, you could automatically create a bundle of code with all the necessary variables and functions you would need to work with this “ball“. This could include for example a variable for its weight, a function to simulate its falling, etc. Fascinating stuff and way less code to write.

Get help

You have the fundamentals, now you only need to know the most important skill of any programmer: knowing of to get help. From total beginners to veterans, no single person can follow every feature update, trick or special situation. Feeling lost is not only natural but almost required. Luckily, whatever your problem, you can be sure that at least a few persons encountered it before. We can therefore learn from their experience to go one step further. Programming efficient code is a community effort built on years and years of experimentation. At the moment of writing, most programming help can be found on special platforms such as “StackOverflow “. Expect to pass most of your programming time looking at references of that kind. Just remember that community help goes both ways. You should really think about helping others in turn with problems YOU solved in the past when relevant. In that way, every line of code can be tidier and faster. Now that‘s thinking like a programmer.

Coffee break

Time to close our terminals and our screens. You might not realize it, but you now have all the tools you need to write your own programs. With just a few fundamentals, you can tackle almost any programming challenge. The only thing you're missing is the experience to do it quickly and efficiently. And the best way to do that is simply to start typing.