While working at REDACTED, I reported the following issue.
It’s perfectly valid to program by copy-pasting code from one place to another. This looks like a bold statement, but it’s sometimes very difficult to come up with a cleaner alternative. Of course I’m not talking about breaking the DRY rule. For example,
if (x && y && !z) {
// ...
}
// later
if (x && y && !z) {
// ...
}
// later
if (x && y && !z) {
// ...
}
is a break of the DRY rule, and each programmer should replace that with
const condition = () => x && y && !z; // a function guarantees re-evaluation
if (condition()) {
// ...
}
// later
if (condition()) {
// ...
}
// later
if (condition()) {
// ...
}
What I’m talking about is using a block of many lines of code as a template for generating a similar functionality at another place in the application. For example, I could have already programmed some page using two files: some-page.html
and some-page.ts
. Later on, when I need to program another-page
, which is very similar to some-page
, I can copy-and-paste the latter and apply the little changes needed to get the former.
This practice is very popular all around the world and there is no shame in doing it. And nothing stops you from writing a real code generator. However, copy-paste-adapt is all you need if you keep adapt at a minimum. How do you do so? Dead easy: refrain from using specific identifiers in code or move them from the code domain to the data domain.
For example, better names for that pair of files would be some/page.html
and some/page.ts
. This would allow me to copy the files in the some
directory to the new another
directory to immediately get another/page.html
and another/page.ts
files which preserve the structure without requiring any adaptation (like renaming).