Chapter 6. Declarative Code

Behavior is the most important thing about software. It is what users depend on. Users like it when we add behavior (provided it is what they really wanted), but if we change or remove behavior they depend on (introduce bugs), they stop trusting us.

Michael Feathers, Working Effectively with Legacy Code

6.0 Introduction

Declarative code is a type of programming code that describes what a program should do, rather than specifying the steps that the program should take to accomplish a task. The code focuses on the desired outcome (what), rather than the process of achieving that outcome (how). Declarative code is easier to read and understand than imperative code, which specifies the steps that a program should take to accomplish a task. The code is also more concise and focused on the ultimate outcome, rather than the specific details of how that result is achieved.

Declarative code is often used in programming languages that support functional programming, which is a programming paradigm that emphasizes the use of functions to describe the computation of a program. Examples of declarative programming languages include SQL, which is used for managing databases, and HTML, which is used to structure and format documents for the web.

Software development carries an inertia from times where you needed to write software in low-level languages due to restrictions of time and space. This is no longer the case as modern compilers and virtual machines are ...

Get Clean Code Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.