Chapter 7. Correctness Checking

In this chapter, we will explore D's correctness checking and documentation features. We'll look at the following recipes:

  • Using assertions and exceptions
  • Using static asserts
  • Using template constraints and static if
  • Preventing memory corruption bugs with @safe
  • Leveraging const-correctness
  • Avoiding side effects of pure functions
  • Verifying object invariants and pre- and post-conditions
  • Unit testing your code
  • Documenting your code with Ddoc
  • Writing platform-specific code (versions) and per-client configuration modules

Introduction

D is designed to help make programs with fewer bugs. Its built-in support for assertions, contract programming, and unit tests help you verify your program at runtime. D's exception based error model helps us to ensure no runtime errors go unnoticed, without requiring return code to be checked after every function call.

D also provides numerous features to help you to prove correctness at compile time, from built-in function annotations to custom compile checks with static assert, which is capable of running D code with compile-time reflection to implement complex checks.

We'll also look at D's built-in support for documentation and platform versioning, which helps you to communicate that the code does what you intended, not just what you programmed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.166.255