Grade 8 Computer Science · Modules and reuse
When one file stops being enough
Three signs it is time to split, and along what lines.
An excellent function you cannot take with you
You wrote a function that cleans data perfectly, and now another program needs it. The only way is to copy it, and from that moment there are two copies that will need to change together, and will not. And what if the fix is a new file, but it is cut in the wrong place?
A program of fifty lines lives excellently in one file. A program of a thousand lines in one file is a misery — not because of the file’s size but because nothing can be found in it, and every change feels dangerous. The fix is the same idea already applied to functions, now one level up.
| The sign | What it attests |
|---|---|
| Nothing can be found | The file is too large |
| A change touches many things | The roles are mixed |
| Nothing can be reused | There is no clear border |
Along what lines to split
The rule is to split by role, not by size. A file created because the previous one reached five hundred lines is not a module but an arbitrary half, and it usually produces two files neither of which can be understood on its own. A file holding everything to do with reading data is a module, and what it does can be explained in one sentence. This is exactly the same question we asked about functions — can this be given a short and exact name — only that this time the name is a file’s name.
Example 1 — splitting by size fails
- The file reached five hundred lines.
- It is cut in the middle into two.
- Each half refers to the other constantly.
- Neither can be understood on its own.
Example 2 — splitting by role succeeds
- Gather everything that reads data.
- Move it to one file with a fitting name.
- It can be explained in one sentence.
- And also used in another program.