How do computers work? What is well-crafted code? How do you write an algorithm? Ana Bell , lecturer in the Electrical Engineering and Computer Science Department at the Massachusetts Institute of Technology, chooses the best books to learn computer science and programming.

Before we start on the books you’ve chosen, since we’re going to talk about learning computer science and programming, I’m curious to know about your own learning path. Did you become interested in computers early in your life, or did you make that choice as you were going to university?

It was pretty early in my life. It was through my dad, who was a software engineer and electrical engineer. When I was about eleven or twelve, he started to build his own computer and had my sister and me help out. Then he showed me the Java programming language, and started teaching me the basics of Java.

Sounds like a great dad! Then you chose computer science as an undergraduate and graduate program, and from there you went on to a PhD in computational biology?

That’s right, at Princeton.

Biology is often chosen for computer science-related PhDs, but people don’t always stay in the field. Is that your case or do you still work on genomics?

I no longer work on genomics. I became interested in the biology applications of computer science as I entered graduate school, so I decided to try to focus on that. But unfortunately I didn’t have the necessary training from undergrad, I had only taken the basic biology courses. So, oftentimes, I was relying on other students in my lab to help out with the domain knowledge part of analysing the machine learning results. I decided that I didn’t want to pursue genomics, but I really enjoyed the teaching that I did in graduate school, so my career path changed.

Let’s start talking about learning programming. Do you have any opinion on the “learning to code” debate? A few years ago everyone seemed to think that we should all learn how to code. In recent years or even months, other people have started saying “actually, it’s an interesting skill to have, but not as important to have as basic counting, writing and reading”. Do you think it can be beneficial to everyone to learn how to program, or does it remain an advanced skill that is interesting only to people who want to pursue it?

It’s a skill that everybody should have. Once you can solve a problem in a computational way, it’s easy to automate and get the computer to do the task for you many times really quickly. The programming part is easy to get, there are many resources online that you can find, and you can learn any language by yourself. It’s a skill you could have and use in your everyday life to improve it.

“Everybody should have the skill of thinking computationally”

But the skill that we’re actually trying to learn here is the idea of thinking in a logical, or computational way. It’s about learning the syntax of a language. So you have maths, you have English, and to be able to excel in both of those fields you need a different way of thinking. Computer science, or thinking in a computational way, falls somewhere in the middle: you do need to have a logical way of thinking, but there’s also a little bit of creativity that’s involved. Everybody should have the skill of thinking computationally.

And I guess it’s the goal of programs like Scratch to let younger students focus on the computational and algorithmic aspects, and not on the syntax?

Exactly, for kids the easiest way is to use visual software. You drag and drop something and immediately you see the results and what changes. That’s a great way to teach and an intuitive way to learn.

You’re teaching undergraduate students, aged around 18 to 20. If we widen this to anybody with an interest in learning how to program, regardless of their age and their higher education background, how hard would you say this is? It’s often said that programming is a skill that lends itself very well to self-teaching, that people who have not studied it at university can pick it up later. Would you agree with that or do you think that pursuing a proper university degree will make you a better programmer?

If you only want to learn how to program, you can definitely do that by yourself. Even young children can learn how to program. The difficult part that isn’t as easy to grasp on your own is to think in a computational way. It’s tough to understand that an algorithm is just a recipe, and even tougher to come up with new recipes yourself. You need to always expose yourself to that, through coding practice, lectures, chatting with somebody, or visually drawing out ideas. I think a course or something more formal would be necessary to understand algorithms.

Another common view is that programming is difficult to learn later in life. Do you have any experience with people managing to learn it in their fifties or sixties?

You can learn it later on in life, but it’s harder. The biological aspect is obvious of course: it becomes harder to learn anything as you grow older. But I run the online version of the MIT introductory course on edX, and we’ve got students as young as 11 taking the course. It’s a hard course, they struggle through it but a few of them actually make it and they feel great. And then we’ve got 70-year-olds taking it, and they can also succeed. There aren’t as many young or old students taking the edX course, but with perseverance they can do great. Biology is one thing, but I think success is more about the person.

Read 1 Code: The Hidden Language of Computer Hardware and Software by Charles Petzold Read

Let’s start talking about the books you’ve chosen. The first is Code: The Hidden Language of Computer Hardware and Software by Charles Petzold. I’d never heard of this one before but it seems very interesting. Is this a nice first step, to check how interested you are in the topic?

I picked it because it tells you how a computer works. A lot of times, people think that the monitor is the computer. I don’t know how common of a misconception that is, but it is definitely one! This book talks about all the chips and the gates, and it goes from a very low level all the way up. It gets you to use your imagination to virtually build a computer. It’s easy to read, you can lie down on the couch and enjoy it—it’s not so much of a textbook. It demystifies the magic of a computer and what it is. There is no theory involved, rather it presents the components and how they come together.

So you would recommend it to anyone with an interest in programming, even if they don’t want to start coding themselves?

Exactly. If you don’t want to learn how to code but still want to learn what a computer is and a little bit of history behind it, this is a very good book. It goes all the way from the chips that make up the computer, up to the peripherals like the screen and keyboard. The details of how we build computers haven’t changed since Petzold wrote the book. But the size of things has changed!

And then, after this introductory book on programming, if a reader finds they really are interested and want to learn programming, this would be a good point to choose a language. Is that right?

Yes, the first book tells you about the hardware. Then if you want to learn how to program, you’ll need to learn a programming language. To do this, you don’t have to use a book, there are many tutorials online.

For complete beginners, one of the inevitable questions is, ‘which language should I start with?’ Why is Python so often recommended? Ten or fifteen years ago, students of computer science used to start with lower-level languages such as C/C++ or Java, and there has been a big shift in recent years, even though Python has been around since the 1990s. Do you think there’s a particular reason for that?

The first reason is that more people want to learn how to program, and languages like C/C++ and Java are cumbersome, so the syntax is not as easy to understand. What makes Python interesting and easy to learn is that it’s like a rough translation of English into code. You’re speaking this sort of broken English, and if you read Python code out loud, you can often tell what’s going on, which is nice. In Python you also don’t have to declare types of variables, as you’d have to do in C/C++ or Java; that makes it even more accessible. You don’t have to think about that aspect, the language does it for you. Having eliminated these annoying little things is what makes Python enjoyable to everyone.

“It’s tough to understand that an algorithm is just a recipe, and even tougher to come up with new recipes yourself”

The other reason is the push towards data science, and generally working with a lot of data. It makes Python nicer to use, because it’s close to languages like Matlab or R. With Python, you can write easy and quick scripts, without too many pesky language details. A lot of people who are not computer scientists or programmers want to get into this field of data science and analyse the data that’s coming their way. Usability makes Python a very attractive language.

So Python would definitely be your obvious recommendation, or do you see any alternatives out there for beginners?

Python is the easiest to learn, and is enough if you’re not going to work in the field of computer science and programming. Beyond that, Java is the other big one that I would recommend, and then C++. You have a lot more control in those languages and your code becomes much more efficient. If you’re dealing with a critical application like flying an airplane, you would not want to write that software in Python!

You’ve recently written your own introduction to programming, Get Programming: Learn to code with Python. You must have gathered a lot of insights from your experience teaching hundreds of students at MIT. Is there anything in particular that you do in the book, that is sometimes done wrong when it comes to teaching programming?

There’s no right or wrong way to teach programming, but one specificity is that I don’t actually get into code before chapter 4. I focus on getting the reader to understand what computer science is, and getting their head around that way of thinking about things.

“What makes Python interesting and easy to learn is that it’s like a rough translation of English into code”

One of the earliest chapters is “principles of learning a programming language”, the fact that you only do what you’re told to do. As humans we can infer things; if someone tells you to make dinner, you know that means going to the kitchen, taking pots and pans, using ingredients, etc. But if you said “make dinner” to a computer, it wouldn’t know where to start. So I’m trying to get readers to understand that a computer needs very specific commands, because it doesn’t know how to infer anything. Then the book goes through learning Python, while continuing to stress those points along the way.

One final question before we delve into the rest of your selection. Of course at Five Books we’re very interested in people’s favourite books, but there are so many resources online to learn programming. Do you think there are still merits to learning computer science with books?

It’s funny you should say that. When I started thinking about this interview and the books I would choose, my first reaction was that you almost don’t need to read books anymore. It’s much easier and quicker to search online. When you have a question, chances are that several people already asked and answered it on sites like StackOverflow. Books can’t compete with that. What you can get out of programming books is the thought process behind the ideas. Language takes a back seat to the concepts presented, in the sense that ideas in a book should be universal to any programming language. They should be relevant to beginners as well as people who have been programming for 20 years, and as relevant today as when people started programming. I tried to focus on books that present this: not books that are language-specific, but books that present ideas you should be aware of, no matter your programming background and what year this is.

Read 2 Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin Read

Your second book is Clean Code by Robert C. Martin. Its subtitle is A Handbook of Agile Software Craftsmanship. Can you tell us what it’s about?

This book is going to show you how to write code that is readable by yourself in the future, or by somebody else. Martin wrote it in a very approachable way, and what I liked when I read it is that it starts talking about code right away. It actually starts with the most basic thing, which is how to name objects in your code. When you’re programming, you’re working with objects all the time; so to make your code readable, you need to use proper names for your variables. That’s the first chapter, and this is often overlooked so I was happy to see that.

Get the weekly Five Books newsletter

The tone of the book is quite conversational. Again, you can sit on the couch and read it; you don’t need to code. You can actually enjoy it if you don’t know how to program at all. Each chapter begins with a little scenario, often a conversation between two people, and it sets the scene for what the chapter will be talking about. That’s very helpful, and it makes it a very nice book to sit down with and read through.

This idea of readable code is a traditional piece of advice, especially from teachers to students. But readability, and especially commenting, are often seen as one of the most boring parts of programming. How do you convince your own students that it’s not a waste of time and that clean code will be valuable to both other people who read their programs, but also to their future self?

Well, I still haven’t been able to convince my students that it’s important! I don’t know if it’s possible. Sometimes even I don’t do it and regret it later. It’s a matter of not doing it, and then coming back to your code weeks later and not remembering what you did, and learning from those experiences! Two of the books I chose focus on making your code readable; so yes, it’s very important.

The idea that there can be such a thing as “code craftsmanship” is often difficult to grasp for beginners or people who don’t program. Can you explain what is usually understood—or your own definition—of what well-crafted code is?

First, well-crafted code is readable code. This means having comments that describe the thought process and what the code is trying to do—not how it’s doing it. Good variable names would also fall under that category. The other part is organising your code. That means using the right amount of classes and functions, and using them in the right away. You don’t want to write too many and over-engineer your code, but you shouldn’t write too few either so that the few you have are complex and not easy to understand. You want to have the right amount to make them useful and reusable.

Support Five Books Five Books interviews are expensive to produce. If you're enjoying this interview, please support us by donating a small amount.

Those are the two main conditions to perfectly-crafted code. The balance between them is something that you can’t learn on you own so well. That’s where you would need a lot of practice, or working with somebody else, or reading somebody else’s code. Those are great ways to discover bad pieces of code, but also programs that you’ll find perfect and completely understandable. This craftsmanship part is where the creativity comes into programming, what makes it different from being only about math. It’s closer to learning English in this way.

Read 3 Code Complete: A Practical Handbook of Software Construction by Steve McConnell Read

Your third book is Code Complete: A Practical Handbook of Software Construction, by Steve McConnell. Is this going further into the same idea, or does this book add something different?

It’s a pretty long book. It’s like Clean Code but it goes further. Clean Code gives examples in Java; this one gives examples in several different languages, to emphasize that the concepts it’s teaching aren’t language-specific and should be applicable to any language. It doesn’t teach you the code, it teaches you the software construction: commenting, classes, debugging, refactoring…

While Clean Code teaches best practices to individuals who are writing code for themselves, Code Complete is for people working in industry, writing software for companies, which means they’re giving their code to other people. It even has little checklists at key points in the book; if you are in industry, you can make sure that your code is readable and debugged by going through these checklists and making sure you’re touching upon all aspects. So it’s definitely more geared toward industry. If you have an interest in going into that, this would be the book to read. It’s a pretty old book, but it’s still very relevant. I remember my dad reading it.

Do you think that the concepts presented in these two books are what makes you go from someone who is able to program, to someone who programs very well?

Code Complete goes a step beyond cleaning your code, and makes sure that it is also deployable, or works at a larger scale in the context of a company or an industry. But having clean code would be enough to be a pretty good programmer.

Read 4 Algorithms by Robert Sedgewick & Kevin Wayne Read

Let’s go on to your fourth book, on a different topic: it’s called Algorithms, by Robert Sedgewick and Kevin Wayne. This is an exhaustive book on algorithms. What can you tell us about them?

The book presents some very basic algorithms, using Java. It’s written by two authors I’ve worked with in grad school at Princeton. There are two main books on algorithms used in universities, and this is one of them. They both present the same important algorithms that you’ll need to know, but this one does it in a much more approachable way. Every algorithm you see in the book is also implemented in Java, so if you want to run it, you can copy the code and execute it yourself. I like that it’s so practical. It doesn’t bog you down in mathematical terminology and proofs, which I also like.

I wouldn’t recommend sitting on the couch with this one, because you’ll need to be a bit more proactive. But as far as algorithm books go, this one is pretty approachable. I would say it’s also helpful if you’re going to go for programming interviews. They usually ask you to either write some algorithm or piece of code, and reading this book beforehand would be very useful to get ready.

The word “algorithm” seems to have taken on a huge significance in the last few years, in the media discourse about people’s fears of black boxes and manipulation. I find this very curious because when you learn computer science, algorithms are actually anything but black boxes. They’re very clear, step-by-step programs where you go from a problem to a detailed solution. Do you think that educating people about what algorithms are would be beneficial, and make us less scared of them?

That’s a great point. Algorithms sound like something mysterious, but they’re not. They’re recipes, with a series of steps to go from an input to an output. It’s very simple, and there is nothing mysterious or malicious about that. If you’re going to learn computer science, or if you want to start programming, you should learn something about an algorithm at some point. And even the general public should know that there’s nothing crazy going on with algorithms, even though that word is being thrown around without much meaning attached to it. It’s very straightforward, in the same way that you would bake using a recipe, or build a piece of Ikea furniture.

Read 5 Types and Programming Languages by Benjamin C. Pierce Read

Finally, your fifth book is Types and Programming Languages, by Benjamin C. Pierce. This seems to delve deeper into some of the mathematical theory behind programming. Can you try to explain to our readers what type theory is, and why it’s important to understand it?

This final book is pretty advanced. It’s about programming language design. The best way to explain it is to draw a parallel between the physical world and programming languages. If you think about the physical world around us—objects, things in nature, etc.—we have come up with mathematics to help us describe what’s happening, in a systematic and logical way. Computer science is the same thing, but for what’s going on inside a computer. And around this idea of computations, you can build type systems. Type systems are the maths we’ve come up with; they help us describe computations or what’s going in a program, what operations are happening. They help us reason about what a program is doing, and often help programmers avoid mistakes when writing code. For example a type system might be able to say: ‘you’re trying to multiply a word by a decimal number, that doesn’t make any sense.’

“You do need a strong grasp of maths, but not the traditional maths people think of ”

It’s not a book for beginners, but it’s a good book if you’re interested in the theory behind programming languages. In the first book you’ve learned about the hardware, and in the next books you’ve learned about good code, and then algorithms. Now you can learn about programming languages themselves: how to write one, and how they do what they’re supposed to do.

This leads me to another aspect we haven’t talked about yet: people often say that to study computer science, you need to be good at maths. Do you still think that’s the case today, when computers hide so much of the computational theory behind layers of abstraction, or do you think that to program well, you still need a strong understanding of maths?

That’s a great question. You do need a strong grasp of maths, but not the traditional maths that people think of. When people think of maths, they think of operations, adding things, doing calculations. The mathematical grasp that you need is more on the logical side: thinking about binary choices—something that’s ‘true’ or ‘false’. This logical way of thinking, which is the way a computer thinks, is also a branch of mathematics. Unfortunately you only study it in undergraduate education, but you encounter it in life in a lot of ways. When you’re doing an online quiz where you follow a flowchart to find what kind of dog you are, you’re using logic to figure out something. That kind of maths is very useful, and that’s what gives you the mind of a programmer. But you have to change your thought process to think of statements in a programming language instead of pictures of dogs!

To come back to computer science in general, it’s a field that is infamous for its lack of diversity, with very low proportions of women and people of colour. Have you noticed any improvement in the last few years? Your own role in teaching computer science to undergraduate students seems like a step in the right direction.

There’s definitely improvement at many levels of education. A lot of women are taking my introductory course in computer science, because they recognise that it’s a useful skill to have. In my class we have about 40% women, which is pretty high. Our staff is somewhat large—we have 15 teaching assistants—and the majority of them are actually women. A lot of times, female students will take the course, they’ll love it, they’ll major in computer science, and towards the end they’ll want to give back. And I love that, because they often want to be teaching assistants, and they’re very dedicated and they do a very good job. And because 80% of our staff is female, then that cycle starts: the students taking the class see that the majority of TAs are women, so they think ‘I can do this too!’, and that helps push rates even higher. Of course the push towards general computer science education in high school will definitely help as well.

That’s a great transition to my last question: what piece of advice would you give to high school students thinking of starting a career in computer science?

I would say just go for it. Pick a language, stick with it, and learn as much as you can. It doesn’t matter what language, even if you start with a visual language like Scratch, that’s awesome. It starts to get you thinking about computer science, and the logical way that computers think. That’s a huge step towards succeeding in computer science post-secondary school.