Dear readers, the PVS-Studio team is here to bring you an interview with Anders Schau Knatten, the author of "C++ Brain Teasers: Exercise Your Mind". In this article, you'll get an inside look at his new book on C++, discover engaging ways to learn the language standards, and explore some common mistakes C++ developers make—along with a few insights into Anders' background. Hope you enjoy the read!
Could you please introduce yourself and provide an overview of your professional background?
I have a quite varied background in programming. I started out a bit in a high school in the late nineties after discovering the demoscene. Randomly, I picked up a book on Pascal, which I haven't really touched since. Since then, I've used a bunch of different languages, both on a hobby basis and professionally. For work, it's mainly been C++, Python, Java, C#, Uno, and PHP.
Picture 2. Anders (bottom right) at his first full-time job about twenty years ago
Currently, I work at Ascenium, an Oslo-based startup that's designing a new and more performant CPU. I work mainly on the performance simulator and am involved with the memory model. But I've also done computer vision, seismic, flow simulations, a graphics programming language/IDE, web development, and all sorts of side projects.
CppQuiz is an online quiz that enables you to test your knowledge of the C++ programming language. The quiz consists of 160 questions, each includes a C++ code fragment. The dev's task is to examine the code fragment and determine what the program will output. Sometimes the program will not compile, and that's important to consider. The quiz has three difficulty levels.
Picture 3. The example of the CppQuiz question
Before we delve into the main topic of the article, would you mind discussing your CppQuiz project first? It appears that the book somehow relates to it. Have you got any interesting facts or observations related to CppQuiz? Is there a backstory behind the quiz? Why does the quiz target the knowledge of language standards?
My interest in C++ Quizzes is thanks to my fellow Norwegian, Olve Maudal (who eventually wrote the foreword for my book). I went to a couple of his conference quizzes, and one late night at ACCU 2013, I decided that I had to make a web version of this. The first version was coded in my hotel room that night after a few too many beers and on the train and plane home the next day. So, the source code of the site itself is a mess, but the project itself turned out to be a success!
Picture 4. Anders and Olve Maudal giving a C++ quiz at the NDC TechTown 2023 conference
The reason I target a specific version of the standard is simply because the explanations refer to the standard, and then we need to decide on a particular version to refer to. For some questions, the answer even changes with new standards, such as temporary lifetime extensions for range-based for-loops and the removal of trigraphs. We are, in fact, right now in the process of porting all the questions and explanations to C++23. I'm getting great help from the community, and if you also want to help, check out here. I really appreciate all the help I can get, big or small, and you can even win a copy of my book.
There is also a CppQuiz mobile app. Sergey Vasilchenko wrote the app all by himself! All I had to do was create a JSON export of the questions on the site.
At the moment of writing, the Quiz had questions on the C++17 standard. But Anders has since added C++23, check it out here!
Also, enjoy quizzes from the PVS-Studio team!
The book features 25 short C++ programs, and the goal is to guess the output and understand why the language behaves that way. The puzzles are carefully selected to be coherent and relevant to real-world uses, and the explanations include lots of practical tips to write better and safer code. So, the book can be read just for fun or as a deeper learning opportunity.
Picture 5. Anders is happy to introduce his book
The book is a part of the Brain Teasers series from The Pragmatic Programmers. They were looking for someone to do the C++ book in that series, and Frances Buontempo introduced us, knowing my interest in C++ quizzes.
Picture 6. Brain Teasers series from The Pragmatic Programmers
What C++ developers is your book aimed at—beginners or advanced?
To read the book you should have a little bit of experience with C++. It is not an introduction to the language. But if you've learned it in school or worked with it for a couple of months at work, you should be able to understand all of it. If you're a very experienced programmer, you might get all or most of the answers right, but there might be something new still to learn from the detailed discussions following each question.
Picture 7. The book extract "Aristotle's Sum op Parts".
How would you recommend readers approach your book to get the most out of it?
It is really important to make a proper guess at the answer before reading the explanation. I also recommend thinking about why the answer is what it is. Which mechanisms in the language are involved? Why does it work that way? This approach helps create the necessary "hole in your brain" to store the information from the explanation. I find that learning is much more efficient that way.
What are the main problems and contentious topics explored in the book?
I'm focusing on the main mechanisms of the language, rather than the esoteric features and corner cases that unfortunately are a bit too common on the website. So expect to see things like overload resolution, template argument deduction, integer promotions, lifetimes, initialization order, implicit conversions, etc. I also try to give intuitions about how these things are implemented and how they came to be. Finally, I always give tips about warnings, linters, sanitizers, libraries, techniques, etc. that can help you avoid the ambiguities and problems from the question.
My favorite question is: does the book contain any Easter eggs? I think I might have found one while reading, but I won't reveal it.
Yes, one of the sections that called "Hack the Planet" is a reference to one of my favorite movies, "Hackers" (1995).
Picture 8. The book extract "Hack the Planet".
In your experience, what common mistakes C++ programmers do often make?
For most of my career, I've been working at startups and small teams, so I don't really have as much experience as people who have been working in larger organizations. Some things off the top of my head:
- Good old misunderstandings, unclear requirements, and solving the wrong thing
- The lack of focus on testing, linters, and tooling
- Not understanding the consequences and tradeoffs of "investing in quality". A lot of time can be wasted on nitpicks, reviews, and polishing that doesn't really matter. Conversely, a seemingly innocent "temporary" solution/fix/hack can have big consequences for others working in the same code base. Telling these situations apart takes experience and attention.
As developers of a tool for finding errors and vulnerabilities in code, we couldn't agree more! We've been investigating C++ code errors for 16 years, and issues caused by carelessness and good old copy-pasting are still among the most common—and sometimes the hardest for developers to spot.
You've done a lot for the community through books and talks. Let's talk about C++ education. I totally agree with you that learning by asking "how it works" and "why" is the best way. What other learning resources, besides books and conferences, do you think are most useful?
I love going to conferences like NDC TechTown, ACCU, C++ on Sea, and Meeting C++. And just doing interesting stuff with great colleagues, that's my favorite way to learn.
Picture 9. NDC TechTown
Picture 10. C++ on Sea
I more often read specialized books for something very particular I need to learn. But a good one I read recently which is not very niche is "The Art of Writing Efficient Programs" by Fedor G. Pikus.
Picture 11. "The Art of Writing Efficient Programs" by Fedor G. Pikus
As an experienced conference attendee and speaker, could you tell about the most interesting talk you have attended? What made it memorable for you?
I can't really think of one that stands out as the most interesting talk ever. But the one that springs to mind as the most memorable right now is Ólafur Waage's talk from NDC TechTown this year. You know the thing where people try to run Doom on all sorts of weird machines? He did it on an actual satellite in space.
Picture 12. DOOM in Space - NDC TechTown
By the way, our team has also recently touched DOOM. One of our C++ developers wrote an article analyzing errors found in GZDoom. You can read the stylized adventure through the bugs in our blog.
You have contributed to the computer science field as a computer engineer and to the C++ community as a conference speaker, book author, and the developer of many pet projects like CppQuiz. What are you most proud of, and why?
In retrospect, I think I'm most proud of the first time I gave a lightning talk at the ACCU conference. I had never spoken at a conference or meetup before, but got up and talked in front of a lot of people, including many of my idols. I honestly think the talk itself was not very good, but it wasn't as scary as one might think, and I see it as the start of my "career" speaking at conferences. If you're at a conference that has lightning talks, I recommend you try!
What advice would you give to yourself at the beginning of your journey of learning C++ and computer science in general?
Don't be afraid to ask questions! When you're new (to programming or simply at a new job) it can be a bit scary to ask, but remember that people expect you to not know a lot of things yet. And if you worry about "seeming smart": you seem a lot smarter if you ask clarifying questions than if you just assume things and make uninformed decisions. At my previous job, I had a colleague who was straight out of university, but was never afraid to ask "how does this thing actually work" or "what do you mean by that". It just made her look confident and intelligent, and like someone who would grow quickly in her job.
What a good point to end the interview on! Awesome! Thank you very much for the insightful talk and for the great book that will enrich every C++ programmer's library. Best of luck to you in your goals. Wish you clean code!