chapter two

2 Narrative code

 

This chapter covers

  • Structuring code through storytelling
  • Discovering the five fundamental plots
  • Organizing methods by narrative level

We’ve explored the idea that code can and should be beautiful and divided that beauty into eight dimensions. Now, let’s focus on the first: storytelling. Every program tells a story, and the clearer that story is, the easier it becomes for others to understand, maintain, and extend. Let’s look at a quick Java example that we’ll revisit later in this chapter:

  validateMandatoryField(lastName, "Last name");
  validateBirthDate(birthDate);
  validateEmail(email);
  saveUser(firstName, lastName, email, birthDate, city);
 

The story is clear and easy to follow at a glance: validate and save a user. Stories organize complexity into coherent, memorable sequences. Like any good narrative, code should have clear characters, a well-defined plot, and a meaningful conclusion. This is the essence of a unique literary genre we’ll explore throughout this chapter: narrative code.

We’ll first explore the five fundamental plots of programming, each representing a recurring story pattern you’ll recognize in your daily work. Then you’ll learn how to present and structure your code narratively, helping readers follow the plot effortlessly and grasp its intent at a glance. Along the way, you’ll discover techniques to make your code as enjoyable to read as it is to write.

2.1 Writing code as storytelling

2.2 The plot

2.3 Code typography

2.4 High-level storytelling

2.4.1 A long and boring story

2.4.2 Understanding the story

2.4.3 Simplifying the story

2.4.4 Levels of narrative: Action, scene, chapters, and table of contents

2.4.5 Choosing where to break the story

2.5 The characters

2.6 The ending

2.7 Summary