Github Copilot and the DRY Principle

Does Copilot help support or violate the "Don't Repeat Yourself" principle?

Github Copilot and the DRY Principle

The answer is unfortunately the latter. I recently stumbled upon this article that talks about a study that found a correlation between Copilot-generated code and code churn. A lot of code is being reverted or updated within two weeks of their initial commit.

The article specifically mentions the introduction of duplicate code as one unfortunate trend.

I did experiment a little bit after reading it. I found that Copilot is able to reuse an existing method within the same class. I could not, however, make it reuse a method from another class, much less, from another jar, confirming what the study found. Has anyone had any success?

Regardless, as cool as AI is (I love chatting with ChatGPT!), developers cannot just take whatever it gives us. I know that's obvious to many, but it apparently needs to be emphasized to some.

Case in point, I was using ChatGPT the other day to come up with a couple of Python scripts for manipulating Excel spreadsheets. The scripts came out almost perfect. I had to ask it to refine them a few times. After all that, there was still a bug that I fixed by swapping two statements. I also ran into an error message. ChatGPT apologized and gave me an alternate (working) version. I forgave and thanked it, of course :).

Admittedly, I have yet to ask Copilot for new code. I've only used it as a more intelligent auto-complete feature. When it offers to help, I quickly review what it's proposing, and accept or decline it as appropriate. It has mixed up my variables in some cases.

💡
More than ever, developers need to have excellent code-reviewing skills.

No more just-skimming-through that some people tend to do. This rule applies to both the developer that's introducing the AI-generated code and the teammates reviewing that code.

Back to duplicate code...

💡
As much as possible, all developers should be aware of existing functionality not just within the application they're working in, but also elsewhere in your codebase.

Think twice before asking Copilot to generate new code for you. It warrants some due diligence.

For however many years we've all been in this industry, we have been making sure that we reuse as much code as practically possible, instead of copying and pasting. Don't let blind reliance on AI undo all that work on engineering excellence in the blink of an eye.