I have an application that is modifying some XML files. Two of the files have a similar structure, with similar business logic needed to be implemented on them.

I've realized that there are some identical - or near identical - files in their domains aggregate roots.

The files aren't value types. I don't see them changing independently, but they could.

Is it worse to have duplicate code (Not very much, they're small classes), or to put them in a common directory and have the aggregate roots share them?

(Yes, this project is over-engineered,but I'm having fun)

  • 1
    Is modifying xml files the purpose of this application? That is the domain you’re working on? If not, then it’s not relevant because it’s infrastructure code which should be separated from the domain. – Rik D Dec 23 '18 at 19:51
  • @RikD well, the data source is xml files, but there is business logic associated with how to to modify the data source, whatever it happens to be, which in this case, happens to be xml files, so it def. belongs in the domain. The thing I was unsure about was, I had a repo for each XML file, to return a domain model. Some of those domain models turned out to be identical, and combinging repo's and domain models would make it less readable, but would get rid of code duplication – Steve G Dec 24 '18 at 4:03

Define Identical

Having identical source, or near identical source does not make it identical code.

Sure right now these two sections operate similarly, they have similar definitions, and some cut/paste checkers report them as copies. This is just a code smell.

  • Should I do something about it? Probably.
  • What should I do about it? Ask questions.

Ask Questions Like:

  • Do these two pieces of code serve the same purpose?
  • Do these pieces of code live on the same shearing level (do they change at the same time)?
  • Is there a reason why the piece of code in one system will change, and not the other piece of code?
  • Does the source code repository show evidence of similar changes?
  • Is there a speed/resource limit that one piece of code is operating in that the other is not? (ie mobile vs. mainframe)
  • Are the tests near identical?

If the answers are mostly they are similar then, yes probably (near-)identical code.

Then ask:

  • Are there other instances of this duplication?
  • Is this code stable, and likely to remain so?
  • How malleable is the code currently?
  • Is it sensible to add another dependency to each project?
  • Who is responsible for maintaining this new library?
  • Who is being subjugated to the whims of an external maintainer?
  • Would deduplicating this code make any project less readable?
  • Would deduplicating this code make any project less changeable?

There are other question you might ask based on the context, but these are good for starting the conversation.

The conversation is the important part here. Developing software is a team sport, in a game that only ends when the software is abandoned. The important part is working with that team to make the code better. Even if you are a sole-developer, your team also includes anyone who writes a plugin for your system, any of the systems users, the system admins, etc...

It may not make sense to discuss code deduplication with a system user, but their needs shape what the system needs to be, and reveal the forces acting on this "duplicate" code. With that knowledge you can see the ramifications of deduplication.

  • If you deduplicate this code, only for another team-member to split it out again because a change has to happen here, but no where else. You have made created an obstacle which has made work out of thin air.
  • On the otherhand, if by deduplicating this code allows that other team member to fix a bug in that area easily. You have removed obstacles to that work.
  • I went ahead and combined them, and combined their repo's into one. It is less readable, and less straight forward because I had to come up with a new name to encapsulate the two things. I just can't imagine them changing independently, and I was copy/pasting like 4 different classes. The only real thing that changes is some node names, which I decided to inject – Steve G Dec 24 '18 at 3:59
  • 1
    Fair enough. Hope it works out for you. – Kain0_0 Dec 24 '18 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.