# Functional Duplications

DevFeed: [Functional Duplications](<https://devfeed.tech/articles/functional-duplications-21797.md>)

Original publisher: [Read original article](<http://blog.cleancoder.com/uncle-bob/2021/10/28/functional-duplication.html>)

Published: 2021-10-28T00:00:00Z

Content type: opinion

Language: en

Sources: [Robert C. Martin](<https://devfeed.tech/sources/robert-c-martin.md>), [The Clean Code Blog](<https://devfeed.tech/sources/the-clean-code-blog.md>)

Topics: [bug](<https://devfeed.tech/topics/bug.md>), [debug](<https://devfeed.tech/topics/debug.md>), [Test-driven development](<https://devfeed.tech/topics/tdd.md>), [test-coverage](<https://devfeed.tech/topics/test-coverage.md>), [Randomizer](<https://devfeed.tech/topics/randomizer.md>)

Tags: [bug](<https://devfeed.tech/tags/bug.md>), [debugging](<https://devfeed.tech/tags/debugging.md>), [random](<https://devfeed.tech/tags/random.md>), [tdd](<https://devfeed.tech/tags/tdd.md>), [test-coverage](<https://devfeed.tech/tags/test-coverage.md>)

## AI overview

The article describes debugging a crash in a Space War game after randomly placed bases were duplicated at the same location. Despite using TDD and having test coverage, the defect appeared during gameplay and was traced by adding runtime checks until the responsible low-frequency function was found.

## Source excerpt

I broke out my old Space War game a few days ago and decided to make a few changes to speed the game up and make it more fun to play. In so doing I discovered a very interesting bug. One of the changes I made was to populate the initial space with a few random bases scattered here and there. This would allow the player some extra resources with which to battle the Klingons while building up a network of more bases. While I was playing the modified game, it crashed. Hard. Now I wrote this with TDD, and I was very disciplined about the cleanliness of the code, and the test coverage. So this was unexpected. So I dug up all my old debugging skills from the pit in which I had buried them, and started to work out what was going on. It wasn't long before I realized that crash was occuring because a transport was being launched between two bases, but the angle of the velocity vector of the transport was :bad-angle. This can only happen if the two bases exist at the exact same location. Bases don't move around in this game, so there's no chance that two bases will accidentally slide on top of each other. There is a very (very) minor chance that the random number generator will put two bases on top of each other at the start of the game; but the odds are so miniscule that didn't worry about it. In any case, this crash happened well into the game I was playing, so initial values could not have been the cause. Fortunately it's pretty easy to hunt and peck around in the game, so I was quickly able to discover that the two bases in question were duplicates of each other. Something in my code was duplicating bases! Well now that should't be too hard to find. So I wrote a litte function that would examine the world and halt with a message if the world contained two bases at the same location. I called this function in the main update loop, and sure enough after 20 minutes of play the program halted with my message. Unfortunately being able to detect that the duplication occurred di