2 Separation between code and data

 

A whole new world

This chapter covers

  • The benefits of separating code from data
  • Designing a system where code and data are separate
  • Implementing a system that respects the separation between code and data

The first insight of DOP is that we can decrease the complexity of our systems by separating code from data. Indeed, when code is separated from data, our systems are made of two main pieces that can be thought about separately: data entities and code modules. This chapter is a deep dive in the first principle of DOP (summarized in figure 2.1).

Principle #1

Separate code from data such that the code resides in functions, whose behavior doesn’t depend on data that is somehow encapsulated in the function’s context.

Figure 2.1 DOP principle #1 summarized: Separate code from data.

In this chapter, we’ll illustrate the separation between code and data in the context of Klafim’s Library Management System that we introduced in chapter 1. We’ll also unveil the benefits that this separation brings to the system:

  • The system is simple. It is easy to understand.
  • The system is flexible and extensible. Quite often, it requires no design changes to adapt to changing requirements.

This chapter focuses on the design of the code in a system where code and data are separate. In the next chapter, we’ll focus on the design of the data. As we progress in the book, we’ll discover other benefits of separating code from data.

2.1 The two parts of a DOP system

2.2 Data entities

2.3 Code modules

2.4 DOP systems are easy to understand

2.5 DOP systems are flexible

Summary

sitemap