Skip main navigation

Introduction to Week 2

Learn how to use the compiler and other tools to detect code problems in an earlu stage
In the last week’s session about defensive programming we talked about best practices for writing clean code and producing high quality documentation. However, your code will contain bugs. You have a tool that can actually help you to catch those bugs early, and it may surprise you, it is your compiler. A compiler has a lot of options and flags that will warn you about suspicious code. However, you need to know how to interpret the warning messages generated by the compiler and hence, how to fix your code accordingly. For some programming languages additional tools are available which capabilities go beyond that of compilers. For instance, for C and C++ there is a Cppcheck which we’ll cover here as well.
Donald Knuth once said, “Beware of the above code; I have proved it’s correct but I’ve not tried it.” To paraphrase, “Code that has not been tested does not work.” It’s very important to write good tests for your code, both unit tests and functional tests. We’ll talk about best practices and also about frameworks that help you to write those tests easily and to run them conveniently. However, you may wonder whether you have tested your entire code base. Now, let’s say that the answer is probably “no.”
You can use a code coverage tool in order to check which parts of your code have been executed and which have not been executed and adapt your unit tests in order to get a better coverage. In summary, in this week’s section we’ll focus on catching bugs early before they can do any real damage.

Week 2 learning goals

During this week, you’ll learn about

  • Unit testing,
  • Code coverage tools,
  • Using the compiler to warn about suspicious code,
  • Static code analysers.
This article is from the free online

Defensive Programming and Debugging

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education