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.

Each file answers one question: what it does.
The signWhat it attests
Nothing can be foundThe file is too large
A change touches many thingsThe roles are mixed
Nothing can be reusedThere 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

  1. The file reached five hundred lines.
  2. It is cut in the middle into two.
  3. Each half refers to the other constantly.
  4. Neither can be understood on its own.

Example 2 — splitting by role succeeds

  1. Gather everything that reads data.
  2. Move it to one file with a fitting name.
  3. It can be explained in one sentence.
  4. And also used in another program.
If it is hard to say which problem the split solves, it is probably not needed yet.