A Module Owns Its Memory
Chapters
- What a module is
- Who can touch the memory
- A refusal changes nothing
Transcript
What a module is
A neighborhood library lends tools. Branch three has four drills, and three are on the shelf. Someone borrows one. We need a count that is always right. We give the shelf to one module. Its public functions are how other parts of the app use it. Its private memory holds the count. The module's code connects the two. In the project, a module is a folder. The functions and the code are in mod dot r s. Everything the module saves is declared in its memory folder, in a numbered version file. This one is version one.
Who can touch the memory
The call arrives at a public function. The module's code checks the shelf, then uses its key to open its own memory and write. Three becomes two. Now some other code tries to set the count directly. It cannot. Outside the module, this memory is a sealed handle. Other code can hold the handle and pass it along. Only the module's own code holds the key. So when the count is wrong, there is one place to look. The seal is a rule the compiler enforces between modules. It keeps the app's own code honest. The rules inside the module are still ours to get right.
A refusal changes nothing
Someone asks for the ladder. There is one, and it is out. The module checks, and answers no. Nothing in memory moved. This order is a design rule, and you should ask for it by name. Decide every refusal before the first write. If code writes and then returns an error, the write stays.