Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

Find out more

Introduction to Week 2

Learn how to use the compiler and other tools to detect code problems in an earlu stage
10.9
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.
69.1
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.”
117.3
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

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now