About programming

When I was 13, programming wasn't really something they taught at school — OK, maybe they bored us for a single period with a lesson about making 'hello, world' appear on screen, but that was it. If you wanted to know anything more, you were on our own. 

These days, things have completely changed. In fact, you've probably already had a go at writing lots of programs in class. 

Maybe you're interested in learning more. If you still haven't learned anything about programming in school, maybe you want to give it a shot on your own. Either way, you're in for a great time. Programming is useful and fun. 

Programming is a creative skill like playing a musical instrument or writing poetry. To get good at it, you need to practice a lot and get used to solving difficult problems. 

But all that said, even if you never become good at programming, it's still worth trying. Just like you don't have to be a famous musician to enjoy jamming with your friends, you don't have to be a professional software developer to have fun writing little programs to make your life easier. 

You can program. Let's look at some of the reasons you might want to learn.

Why you should learn to program 

People who love programming tend to really love it. Any why not? Programming is amazing. Every program you'll ever use is an intricate little machine that contains the expertise of so many people. And programming can make you feel powerful. You can bend the complicated device on your desk to your will! 

If you learn to program, you can be in a better place to understand how the technology around you works and know what it can and can't do. You can share your knowledge with the people around you and help them understand, too.

Programming can be a way for you to help other people. A little program you write for yourself might be useful to thousands of other people (if you release it as free software!). 

There are many reasons people want to program. What are yours? 

Your first programming language

People learning how to program for the first time always think it's very important to choose the right language. But honestly, it doesn't matter that much. Once you know one programming language, another can be learnt very quickly. In fact, most programmers learn lots of languages as they build things or technology changes. You will too. 

But if you want me to tell you what to learn, a good first choice is Python. Python is free software, and it's a real programming language used by heaps of professionals. Because it's so popular, people have produced lots of stuff you can build on to make your own programs.

Other languages that are also good starting points are Racket, JavaScript and Rust. They have many of the same advantages Python does. 

One more thing: even though C and C++ are used in lots of programs, including the majority of free software projects, you don't want to start with them. They're missing some features that make code easier to write and less buggy. For example, they don't delete unused data or check if part of an array exists before you work with it. Even very skilled programmers get in trouble with them; as a beginner, you might find them very frustrating. 

Hitting the books

Unfortunately, I can't tell you specific books you should go out and read or videos you should watch. Most books and videos on programming for beginners aren't libre. Just like software, documentation (which programming tutorials are) should be free. Because there are so few good, libre tutorials suitable for people your age, Libre! will have tutorials on programming in Python starting next issue. 

If you can't wait, it's fine to use non-free tutorials; just don't buy them. Go borrow them for your library: they'll have more than you could possibly want! 

Books on programming for beginners are pretty similar to each other. Most of them take you step-by-step through your language's features. There's probably a chapter on how numbers work, another on loops, and yet another on working with files. Don't bother with choosing the 'right' book. Instead, pick out a few of them. If you don't understand how one book puts something, another might be more helpful. 

Programming books can be scary. They're big, heavy and full of tedious detail. You might wonder how anyone even learns this stuff.

Relax. I have good news. You're not supposed to learn most of what's in the book. A lot of what you find in there is for you to look up later. Instead, focus on the big picture. As you write more code, the little details will stick without you even trying. 

How to learn

So we've chosen a programming language and hauled a bunch of heavy books home. Time to apply some ice to our aching backs and start learning! 

You should learn in front of a computer, with your interpreter open. You can't learn programming just by reading about it; you have to do it. 

To start, read a section in your book then quickly summarise what you just read in your mind. Take the example programs and play with them in your interpreter until you feel like you could have written them yourself. What happens if you change things in the program? Can you guess what'll happen if you break it? Now try to write your own examples without the books. 

After you've worked through the chapter, head for the exercises. You can probably skip the ones that are just asking you to spit out basic information about what you just learnt; if you've played around enough they're a waste of time. The exercises you must do are the ones asking you to solve bigger problems. Those are usually at the end. If you can, pick out these exercises from several books and do all of them. 

The exercise that nearly broke me: what to do when you get stuck

Programming can be frustrating. At some point, you're going to meet with what feels like an impossible problem. 

When I was learning to program, there was an exercise in a book that went like this: 

You have four integer variables. Print the largest and smallest values. You should be able to do this in no more than four comparisons.

Sounds simple enough, right? Well, it wasn't. No matter what I did, it always took me 6 or 7 comparisons to find the smallest and largest numbers. 

I felt awful. This is such a simple little exercise, so why can't I solve it? I began to think that maybe I just wasn't bright enough for all this programming stuff. Clearly, I'm not like the other people at school that find programming so easy. 

Still, I couldn't resist trying again. Hours later, it hit me: if I solved parts of the problem, the rest fell into place. I wrote the program, et voilà, it worked! Better yet, I looked over my code again and realised I didn't need four comparisons. I only needed three. 

I was so proud of myself when I solved this problem, but I could have saved myself a lot of pain by handling my frustration in a better way. 

This is what you should do when you find yourself getting upset over a problem or a bug:

• Take a moment to breathe and relax your body. 

• Put the problem in perspective: finding things hard is normal when you're learning. 

• If you're very upset, take a break and go do something else for a while. 

• When you're ready to return to the problem, look for easy mistakes. Does your code have any misspellings or typos? Are you using your language right? Check against a book or someone else's code. Trivial typos are the most common mistakes, but they can cause disaster: in 2014, an accidentally doubled line of code left the iPhone wide open to attack

• Do an internet search for any error messages that come up. An error message can be caused by many different things, but maybe you'll find something that gives you a hint about what might be causing your problem. 

• Pull out pen and paper and run your program yourself as if you were the computer. Draw what's going on in the program as you go. It can also help to describe your program step-by-step to someone who knows very little about computers. 

• For stickier problems like the one earlier in this section, spend some time exploring the problem with a curious, non-judgemental attitude. Play with the problem; draw it; answer it in the wrong way. Think about how it's similar or different to other problems. Brainstorm as many possible solutions as you can, no matter how stupid. Break the problem into smaller parts and and see if you can solve those. Maybe you might be able to put the pieces back together again?

I dare you to solve the problem that nearly broke me. It might be frustrating for you. I hope you'll handle it better than I did. 

What's next? 

Once you've learnt the basics, you're probably itching to write a larger program. Real software is made by building on the works of others. No one can possibly know everything they'd need to write any program from scratch.

If you want to write programs that you can use with a mouse, you'll want to learn something like GTK or Qt. Glade and QtCreator can be used to design how your program looks before you write any code. 

If you're a would-be game developer, have a look at pygame and Godot. Pygame is great for 2D games and is very easy to use. Godot is harder to learn, but can be used for both 2D and 3D games. If you're not artistic, you might also want to take advantage of https://opengameart.org to fill out the world you create.

Web development is far too complicated to cover in this article, but I can give you some tips. You'll want to learn a little HTML, CSS and JavaScript to begin with and understand a bit about how the internet works. Frameworks like Flask, AngularJS or React make producing webapps much easier than if you were doing it all yourself. 

There are some Python libraries you might find useful to learn as you solve bigger problems. If you're doing anything involving the internet, learn requests if you want to keep things simple. Many APIs use JSON or XML to transfer data. There are Python modules, just called json and xml, that give you an easy way to use those formats. If your programs are going to be handling a lot of structured data, you might want to use a database. sqlite is a great way to embed databases in smaller programs.

Finally, writing big programs is an art. Fortunately, you can learn from the best. There are billions of lines of free software waiting for you to explore. Take advantage of it! 

I hope you'll find a passion for programming, or at least enjoy your time spent trying it out. Maybe soon you'll be showing up to hackathons like the one for FSF40 (details right after this article). If you've written a cool program, tell me about it at carmen@missionlibre.org. Good luck!

Next: Join the FSF40 Hackathon