Computers are like your dog. Coding explained

Daniel Visca
Nerd For Tech
Published in
4 min readMar 15, 2021

--

Writing code is like speaking with your dog. Your dog can only understand a few keywords. They might understand ‘sit’, ‘stay’, ‘fetch’ and ‘drop it’. Simple words that on their own are mainly neat party tricks or fun to play with. But what if you string these commands together?

Let's say you are super lazy today and you are hanging out on the couch. You feel like reading the news but the rolled-up newspaper is way over on the other side of the room. You get the bright idea that maybe if you give your dog the right commands in the right order, she can get the newspaper for you. You call her over then tell her ‘fetch’ and point her where to go, she runs back to you with the newspaper in hand. But she won't put it down now because she doesn’t understand until you tell her with one of the commands she knows… ‘drop it’. Voila, your laziness has prevailed. With just a couple of simple commands, you have a dog that can do things for you.

Unlike dogs, computers only speak in binary, 1’s and 0’s. Fortunately, people before us have made programming languages, these languages give us the ability to write human-readable English commands that, when you run your code, get translated into 1’s and 0’s. When you hear someone say ‘my code won't run’ or ‘I need to debug my code’ they are saying that the programming language didn't know how to translate what they wrote into binary code for the computer. So they must have given a wrong command somewhere and they need to figure out what command couldn’t be understood, just like if you told your dog to do a backflip, she will probably just look at you very confused.

So what are these commands that a programming language knows and where do you write them? Let's look at the programming language python, it is known to be a language that is close to English, even if it knows very limited words.

Making a file that you can write python code in will be similar to making a new document in Microsoft Word. You need to create a new file. Instead of using Microsoft Word, programmers like to use different Text Editors such as Sublime Text, Atom or Visual Studio Code. You can think of these as ‘Word for programming’. Where a Word document ends in the extension ‘.docx’ a python file ends in ‘.py’.

Python has 32 base commands that it understands which are nicely laid out by w3schools: Python Keywords (w3schools.com)

Let's look at the commands ‘if’ and ‘else’

a = 33
b = 200
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")

In this code, we are setting a to be equal to 33. Anywhere a is used, you can just think of it as the number 33. Anywhere b is used you can think of it as 200. By writing if b > a: we are setting the condition that if this is true (b > a), the computer will print “b is greater than a” to the screen. If it is not true (else), we will print “b is not greater than a” to the screen. This way the computer knows how to act in both situations. In our example, b is greater than a (200 > 33) so the computer will only print “b is greater than a”.

One of the great things about coding is that you can define new words. With your dog, to get her to grab the newspaper for you, she had to be told to ‘fetch’ then ‘drop it’. But what if you are extra lazy and want to say as few commands as possible. You could teach her the new command ‘fetch newspaper’. When she hears this she knows that she needs to ‘fetch’ the newspaper first and then ‘drop it’.

In python, we can use the ‘def’ keyword to define a new command.

def compare(a, b):
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")

Now we have a new command called ‘compare’ which compares a and b the same way that we did earlier.

Once ‘compare’ is defined we can use it like this.

a = 33
b = 200
compare(a, b)

This code will do the same as the first piece of code we saw, but now we have a single command that we can use.

How do we go from these commands to the software that we use every time we are on a computer? With a lot of hard work and teamwork. If I define new commands that I think others could find helpful, I share them in a library for other coders who will use my commands to build even more complicated commands on top of it and so forth. Eventually, we can have a single command that can do a lot. For instance the clap button on this article (hint hint). The ‘clap’ command will start a bunch of smaller simpler commands that will display the number of times you clapped, update the number of claps this article has, send the update to me and so much more.

‘fetch’ is actually a command that your dog and many programming languages have in common. In your code, you can tell the computer to fetch information from a certain URL and like the good dog that your computer is, it will!

Different languages have different specialties. Some languages are made to be purely logical and easy to read like python, other languages are made to style a webpage like Cascading Style Sheets (CSS) which is what makes this article look better than just a bundle of text.

I hope this has given you a better understanding of what coding is, how programmers do what they do, and what goes on behind the scenes of the applications you use.

--

--

Daniel Visca
Nerd For Tech

Just trying to solve society’s problems, probably doing it all wrong, and getting overwhelmed in the process.