[flaviocopes] The Programming Basics Course - Introduction to Programming

Introduction to the programming fundamentals. Start here to enter this brave new world

Learn programming to control your computer. Phone. Watch. Whatever

This is an introductory course to programming.

In general.

If you’re new to programming, the goal of this course 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

and more!

Those are general concepts that you can apply to any language.

Once you are done with the course, I recommend picking the JavaScript Fundamentals course and a practical one like the Node.js or React course. So you can expand the theory and apply it to a real project.

Why should you become a programmer?

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.

What you need to become a programmer

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:

  • patience
  • dedication
  • time
  • consistency

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 :slight_smile:

How does a computer work? An overview

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 19th 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!

What is a program

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 phone are programs.

The apps on your computer, of course, 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.

Like in the case of JavaScript, which is run by the browser. In this exact moment, there is some JavaScript code I wrote, being executed by the browser you are using.

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 the question of programming.

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.

What is a programming language?

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.

Some examples? JavaScript. Python. Swift. Ruby.

Every one of those languages solves a problem in a different domain or is particularly well suited in some situations.

This Club is particularly centered around JavaScript, because that’s the main language of the Web. It’s the only language that browsers can execute inside Web pages.

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.

We’ll get more into the details of the JavaScript language in the JavaScript Fundamentals course.

Computer Science vs Programming

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. I’m sure people relying on school for teaching real-world programming were not satisfied with the outcome, but 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.

Which has its own large set of problems and complexities, as you’ll soon find out.

The different programming paradigms that exist

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).

Functional programming takes an approach to programming that derives from math functions. But don’t be afraid - it’s nothing complex. Actually, I think it’s the simplest way to tackle a complex problem and get to a solution. And JavaScript is perfectly equipped to perform functional programming (FP). In FP, you organize code in little functions, and every function returns a result that is always the same, given the same inputs. Same inputs = same outputs. This is an essential point. Functions can be composed, passed as arguments, and function operations like recursion allow us to solve all the problems without creating complex structures for our abstractions.

Those are just 3 of the most popular methodologies. Which is best among these? 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, I mentioned several programming languages which can be associated to those methodologies, but many programming languages can allow different styles. JavaScript for example allows all 3 styles.

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.

Compiled vs interpreted

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.

With an interpreted programming language, like JavaScript, Python, Ruby or PHP, you don’t need this step. Sometimes you’ll see those languages referred as scripting languages , but I find this term a bit …offensive to the power of those languages.

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.

What is a bug?

Every day we wake up, have a nice breakfast, head to the computer and there we sit, to write perfect code that does exactly what we want.

Then we wake up.

That would be ideal, right? But it was a dream.

As good as you can become, there’s no way you can write bug-free code. Code has bugs. By definition.

A bug is a problem that you did not see or anticipate when you set out to write the code.

A bug might be discovered only when you release your program to users, and that’s the worse situation.

A bug can be discovered by yourself when you test the program, and it might even happen when things - working perfectly previously - starts to break because you changed one line.

Those are called regression bugs.

Debugging

How do you deal with bugs?

Well, first by trying to avoid them as much as possible, by carefully thinking about how your program should work, even before you write a single line of code.

Then, by analyzing every single line of code you wrote for possible issues or side effects or unconsidered things.

And… once you know there’s a bug, how do you solve it? Well, the hardest part is always identifying the bug. Then the second hardest part is figuring out why this bug happens. Solving the bug is generally easy once you know all of the above.

Generally we can do two things to solve the bug.

One technique is very basic and involves trying to figure out the values of the state (the content of the variables), and the flow of the program, and printing the to the logs, or to the output of your program. It’s easier with CLI apps.

Another technique is to use the debugger. The debugger is a tool that can be either be provided by your programming language compiler, or by the tooling that’s built around it. For example the Visual Studio Code editor by Microsoft provides a great JavaScript debugger. Another debugger is provided inside the Chrome browser. Same goes for Firefox.

Using a debugger you will be able to stop the running of the program at any time you want, watch the content of the variables, execute any code you want, and step through the program execution one line of code at a time. It’s very important to debug programs that don’t work as you expect.

I won’t go into more details. Just know that there are tools that can help you fight bugs.

Testing is another one of such tools.

2 симпатии