This post is a big introductory course to programming.
Programming in general.
Before getting your hands on a particular, specific programming language, there are concepts you need to be familiar with. Concepts that do not depend on a specific programming language, but are essential for every developer.
If you're new to programming, the goal of this post is to prepare you and give you the basic build blocks, the concepts.
In particular you'll learn:
- what is a computer
- what is a program
- what is a programming language
- what's your job as a programmer
- what's not your job as a programmer
- the different programming paradigms that exist
- the different programming languages you can use, and which one to choose
- what is a bug
I teach programming and I get a lot of emails every day. I talk to lots and lots of programmers, and wannabe programmers.
I have a good grasp of what makes people learn programming.
People want to build the app they always dreamed about. They want to make this project come to life, they want to get their dream come true. Having an app run on your computer or on your phone, creating the game you and your friends dream about!
People want to learn the craft as a hobby. I've talked with people in their 70s and 80s that are learning to program. For fun. To enjoy a new challenge. To create an app for their friends.
People want to start a new career in tech. It's not a surprise - programming as a job is very attractive: it's in high demand, you work indoors, and the average pay is good. Depending on the location and the level, of course. You can also work remotely, which is a huge advantage in my opinion.
And of course those are just 3 examples. You might have a different need, but whichever the need is, learning programming is definitely one skill you will want to have in the future.
You don't need to be a computer scientist or study engineering or be a genius at math to pick up programming.
It's a skill that you can learn at any point in time, at any age, and - let me say - it's an amazing skill to have.
You need 4 things to become a programmer, from scratch:
That's all you need.
Patience because every time you learn something new, you need to be patient and do one step after another, until you can connect the dots.
Dedication because it's not certainly an easy skill to master.
Time because learning things properly requires time. And with programming, many times you will get stuck on something and it will take some time to research. And a great programmer spends as much time thinking about the problem than the time they spend programming.
Consistency because if you can dedicate a little time every day, say 1 or 2 hours, learning to code, you will become a great programmer in a short amount of time.
Oh and you will probably need a computer, too. Any moderately recent computer is good enough, you don't need a fancy top of the line MacBook Pro. Don't use that as an excuse :)
By computer I mean every electronic device that's programmable: laptops and desktop computers, smartphones, smartwatches, your car, your dishwasher, your camera and even smaller things like a dog's GPS device. A lot of things, right? We're surrounded by computers.
Computers are electronic devices powered by programs. Since the middle of the 20th century, programmers have been working on making those computers always faster, and always easier and more accessible to program.
Machines are dumb. They do what you tell them. They can't really think or decide things. They can only follow your instructions, and this makes you, a programmer, a very powerful person.
We call the electronic part of a computer hardware, and the programs part software.
When you program a computer, you provide it instructions that are transformed into bits, the only thing that the electronics can understand. A bit can only have two values: 0 and 1.
When you look at your computer or smartphone, you see a nice interface. That's the result of years of evolution. Underneath, millions of instructions are executed every second, to serve you the list of the best restaurants nearby. All by talking to the Internet, an incredibly vast network of computers - the biggest artificial ecosystem we humans ever built.
Covering how a computer works (and the network of computers) would take us tons of hours. Instead, after this brief overview I'm going to focus on the software part.
We'll keep the hardware side for another course, in the future!
Let's now look at what a program is. Your browser (Chrome, Firefox or the one you use) is a program.
This website is made possible by a program that I use to help me generate it. I provide the content, the program generates the HTML pages.
Your phone is run by a big program, called iOS or Android. That's a huge program, of course, and it's not even fair to call it a program - it's a collection, an ecosystem of programs. To which we give a name.
The apps on your computer are programs. The apps on your phone, too.
Your car is controlled by programs.
A program is a set of instructions, which the programmer assembles to make the machine perform a specific action.
Typically the program is compiled by the programmer before it can be executed. Sometimes however programs are interpreted by another program.
So, here's a first big separation of programs: compiled and interpreted. And different programming languages will allow one or the other method.
Ultimately a program is something you build to perform something specific. It might be helping simplify the management of an enterprise, or solving a problem, like the problem of boredom, by creating the ultimate video game.
Now that we've defined what a program is, let's dive into what is programming.
By programming we generally mean the craft of structuring instructions of code, written using a programming language, in order to create a working program.
I want to introduce something bigger to this concept however.
To me, programming is solving problems. And as such, programming starts way, way earlier than the time you reach for the keyboard and you start to type instructions on the computer.
Programming starts when you are thinking about the problem. Or the app.
Programming is when you are gathering the requirements, talking to all the people involved in the app, talking to the clients, analyzing all the different things that the app should do.
Programming is defining what the app should not do.
Programming is determining if your app is impossible or too costly to run, even before you start working on it.
Programming is drawing all the flow diagrams to help you solve a problem.
Programming is also when you are stuck and you need to solve a problem. How to search for answers on Google and how to ask a good question is a skill on its own.
In the end, 70% of programming is done outside of your code editor.
Computers are dumb. Despite all the talk about Artificial Intelligence, we're not there yet.
The computers we work with, today, are just machines that execute the things we tell them.
And we talk to them using a programming language.
A programming language is a set of instructions and rules that we use to interface with a machine.
There are many, many different programming languages. You can even create your own, if you are into that. But more frequently you'll want to use a programming language that others created a long time ago, made it perfect over time, and which just works without causing you headaches.
Every one of those languages solves a problem in a different domain or is particularly well suited in some situations.
Python and Ruby are mainly used on Web servers, or as more general purpose languages. Python is well known in the data science community, for example.
Swift, introduced by Apple, is the language used to create iPhone apps and in general all programs that run on Apple hardware.
I just mentioned those, but there are gazillions more. There's no single "best" programming language. It all depends on the situation, on you and your team knowledge, the hardware it needs to run on, and other things.
Many programmers took CS (Computer Science) at the university. Computer Science is a common degree for programmers, because the "Computer" in the name is very appealing.
But CS is very, very, very different than day to day programming.
A Computer Science course will teach you lots of theory, algorithms analysis and very little actual programming that's required in the industry. It can also be seen more as a branch of mathematics. It's also quite complicated, if you ask me (I've never been great at math).
Programming on the other hand is using the computer to make it do things that are very practical.
I personally took a computer engineering course in the University in Milan, which was more engineering than programming, too. Less theory than CS, but also little practical skills. All I know about programming, I learned by myself as a self-learner. Or on the job.
Here's good news for you.
As you are reading this, you probably didn't take a CS or CE course, and you might assume people that took those courses have a huge advantage over you.
Well guess what? They don't.
It's likely that a university - being a slow moving giant - will teach you an outdated programming language, outdated "best practices", and all sort of things that you will need to unlearn over time - not an easy task.
Of course people going to those schools will learn things on their own, just like you, but you can easily get to the level of a CS student in a reasonable amount of time. Plus, you can avoid learning all sorts of theories that you will not find applicable in real life.
All things that are great to learn, of course, but which have little application in day to day programming.
You don't just have different programming languages.
You also have different programming paradigms.
The most popular and well known ones today are
- procedural programming
- object oriented programming
- functional programming
In procedural programming, code is executed in a linear way from the beginning to the end. You tell the computer what to do, from start to finish, and it does.
The name comes from "procedure". Which literally means doing something, using specific instructions, executed in order.
I think this is the more familiar kind of programming, thinking about it from a beginner's perspective. We all imagine code being something like a list of things you tell the computer to do. C is a good example of a procedural programming language.
Object oriented programming is a different beast. We try to model the domain of the problem at hand using objects. Objects have a type, which is called class. Object oriented programming, or OOP, was made popular by Smalltalk and later by Java. Programs written using an OOP methodology are more abstract than a list of things the computer must do. We think in terms of objects, and the interactions between them. For example you have a Car object, and a Person object. The Person drives a Car, and so on. Objects can maintain their own state, and encapsulate it. This is really mimicking the real world. A person does not need to know how a car works under the hoods. They simply drive it. The car abstracts away all its complexity and offers a specific way to drive it (brakes, wheel, etc).
Those are just 3 of the most popular methodologies. Which is best among them? It always depends on the problem at hand. And the favorite approach can also be influenced by the status quo, or what the market prefers. For example object oriented programming was more or less the de facto standard. Today I see it widely used, but I also see a lot more people talk about functional programming.
Now there's another distinction we must make, and it's between imperative and declarative programming. Imperative means we tell the computer what to do. The exact actions. C is a very imperative programming language. We move X bytes into memory, we retrieve variable Y, and so on. Declarative programming means we describe what we want, and we don't care how the computer does it.
If you are familiar with HTML, HTML is a declarative language. JSX, if you know about it, is a declarative way to define user interfaces.
Which one is best? It depends. Generally speaking, imperative programming is best suited for lower level languages, like C or Go, which we use to create very performant applications. The more far away we are from the actual details of the machine, the less performant our code is. But also, the easier it is to write it. It's a compromise. As always.
We write code into a text file, and this file is processed by the computer.
It can be processed in 2 ways, depending on the programming language used. I already mentioned we can separate programming languages in 2 big groups: compiled and interpreted.
If the programming language is compiled, the program is analyzed by a program called compiler, and it must be compiled before it can be executed.
In this case our program, written to text files, is transformed into an executable file, which is no more text-based. It will be a
.exe file on Windows, for example. It's the case of C, Go, Swift.
In this case there is a program, called interpreter, that has the ability to execute our own program. We skip the compilation phase, and proceed directly to execution.
The difference is quite a big deal. Compiled programming languages are able to intercept and point out errors, bugs or possible issues before execution. With interpreted languages, an error might only be discovered when a particular situation happens (mind you, this will also happen with compiled languages, but with a lower chance).
But interpreted languages feel more lightweight to write. The process is different enough to have fans of each approach.
You can create (and find) many types of programs. We can generally classify them into 2 macro groups:
- programs that have a Graphical User Interface (GUI)
- programs that have a Command Line Interface (CLI)
The difference between the two is significant, and it's the same between DOS and Windows, if you are familiar with those 2 environments.
Or the same between the Linux you run on a server, and the Gnome or KDE based Linux you run on your desktop computer.
GUI applications have an interface you interact with using the mouse, and you can clearly see it on the screen, typically inside a window, along with other GUI applications. Many GUI applications run at the same time.
A CLI application is accessed from a terminal, a text based interface which only allows you to interact with one application at a time.
Generally speaking, CLI applications are great apps for pro users, that know how to access the terminal inside their system (it's available on any kind of computer), and how to use any CLI app. They know how to find the help for the app, and are generally tech savvy.
A CLI application is the most flexible kind of application because the UI does not need to be filled with options - options are typed by the user if needed. They are best suited for one-time tasks. Or even long running tasks, provided you can run multiple terminal windows.
create-react-app, Vim, the C compiler, and more.
One special kind of CLI apps are backend applications that do not interface with the user on the system, but accept interactions across the network. In this case, the CLI is used to start (and stop) the application. Or to provide a way to instruct the system to run the application as a daemon (a long running process).
GUI applications on the other hand need a way to visually offer all the available options on the screen. They are best suited to provide rich interactions for mouse based inputs. Think Microsoft Word or Google Earth or the Chrome browser.
And CLI apps using Node.js, to provide services through network connections.