What is code?

What is code? Chapter 3

We know we need code, we know it somehow interacts with the computer to do things, but what is it exactly?

The best way to describe code is to compare it with a cooking recipe.

Imagine you have a recipe to make muffins. The recipe starts by defining which ingredients you need and their quantity. In code, the ingredients are called variables. The quantity for an ingredient would be called the value of the variable. We can recall that the whole purpose of the computer is to do things with data, so these ingredients and their quantities are our data.

After the recipe defines the ingredients, it usually presents a series of steps that you need to execute sequentially (in order) to produce the muffins. In code, these are operations/functions.

That’s it! Really, simple code is just like a cooking recipe.

To really see this, I’ll show you an hypothetical Python script that would make muffins:

eggs = 2
oil = 125
milk = 250
sugar = 250
flour = 400
baking_powder = 3
salt = 1
chocolate_chips = 100
muffin_tray = None
bowl = None

heat_oven(200)

bowl = eggs
beat(bowl)
bowl = bowl + oil + milk
beat(bowl)
bowl = bowl + sugar
beat(bowl)

bowl = bowl + flour + baking_powder + salt
mix(bowl)
bowl = bowl + chocolate_chips
mix(bowl)

muffin_tray = bowl
insert_oven(muffin_tray)

wait(20)

remove_oven(muffin_tray)

This is what Python code actually looks like, even if this particular script doesn’t really do anything useful.

We can see four elements in this code:

Variables

The ingredient names are the names of the variables used in the code. You can recognize them in the first 10 lines.

Values of the Variables

Without its quantity, an ingredient is pretty useless. It’s the same for variables. This is why a value is assigned to each of the variables in the code. You might have noticed that the bowl and muffin_tray variables have a value of None assigned to them. In Python, this is a sort of a placeholder value.

Operations

There are many types of operations you can perform on data. In this code, we can see the + sign being used to add ingredients together.

Functions

We also saw a few functions: heat_oven, beat, mix, insert_oven, wait and remove_oven. These are sort of actions that you perform on data that you give them. You can see the data given to each function in the parentheses after the function names.


This was a very short description of each element of our code, but it’s important that we start small.

Now, there’s a problem with this code, it wouldn’t run properly. This is because I haven’t defined what each function actually does. You might understand what they do because of how I named them, but the computer doesn’t. The computer wants you to write code for each function so that it knows what exactly to do when it executes the function.

Oh, it’s starting to look complicated, right?

The reality is that every single program uses functions. Since they’re the actions you perform on data, it only makes sense that a piece of code would use them. However, programmers rarely rewrite a function that already exists.

Was that last sentence cryptic? It’ll make sense after you learn about…

Libraries

Libraries are going to be your best friends when programming. They are nothing more than a collection of functions that other programmers have written so that everyone can reuse them. There’s usually a library for almost anything.

In our case, if there was a library for cooking functions called, let’s say, cooking, our code would look like this:

from cooking import heat_oven, beat, mix, insert_oven, remove_open

eggs = 2
oil = 125
milk = 250
sugar = 250
flour = 400
baking_powder = 3
salt = 1
chocolate_chips = 100
muffin_tray = None
bowl = None

heat_oven(200)

bowl = eggs
beat(bowl)
bowl = bowl + oil + milk
beat(bowl)
bowl = bowl + sugar
beat(bowl)

bowl = bowl + flour + baking_powder + salt
mix(bowl)
bowl = bowl + chocolate_chips
mix(bowl)

muffin_tray = bowl
insert_oven(muffin_tray)

wait(20)

remove_oven(muffin_tray)

Here, the first line of the code imports a few functions from the cooking library. This is how you import any library in Python. Other programming languages also do it similarly.

Let’s recap

Any software will have variables to hold data and values associated to those variables. The data held in those variables will be used to perform operations or to use functions on them. These operations and functions will do something to the data, whether it’s additions, divisions, comparisons, or anything else.

Finally, you will be using many libraries, which are just a collection of already programmed functions written by other programmers. These libraries will help you by not making you write a piece of code that already exists, or what programmers like to call “not reinventing the wheel”.

I want to remind you that it’s not yet important to understand the code shown, we’re not there yet. What you really need to understand is what the building blocks are for code.

Again, what is code? Variables, values, operations, functions, and libraries!


Next: Chapter 4 – Which programming language should I learn?

If you want to start from the beginning of the series, go to chapter 0.

By Radu

Software Engineer and Computational Scientist graduated from the Technical University of Munich. Worked at Shopify, DRW and Ubisoft.

Leave a comment

Your email address will not be published.

Join my mailing list to get an email whenever a new chapter comes out!