Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Duplicate Functions: Run Same Inputs to Find Them

· 5 min read · Home Tech

In this article
  1. What Does “Duplicate Function” Mean?
  2. Why Same-Input Testing Works
  3. How to Run the Same Inputs
  4. Choosing Good Test Inputs
  5. Common Pitfalls and Misconceptions
  6. Alternatives to Same-Input Testing
  7. Costs and Benefits
  8. Frequently Asked Questions

Yes, running two functions on the same inputs is a direct and reliable way to detect duplicates, but only if the functions are pure—meaning they always produce the same output for the same input and have no side effects. For impure functions or those that depend on external state, identical outputs on a few test cases do not guarantee duplication, so you need additional checks.

What Does “Duplicate Function” Mean?

A duplicate function is a piece of code that performs the same task as another function, often with a different name, different implementation, or in a different location. Duplicates can be exact copies, near copies with minor changes, or functionally equivalent but written from scratch. They arise from copy-paste, multiple developers working independently, or refactoring that was never completed. Duplicates increase maintenance cost, because a bug fix or improvement must be applied to every copy, and they can lead to inconsistent behavior if the copies diverge over time.

Why Same-Input Testing Works

For two functions to be duplicates, they must agree on the output for every possible input. Testing them on the same inputs is a practical way to check that agreement. If you feed both functions a set of inputs and the outputs differ, then they are not duplicates. If the outputs match for all tested inputs, they might be duplicates, but the confidence depends on how representative your test inputs are. The more inputs you test, the more likely the functions are truly equivalent, but you can never be 100% certain unless you test every possible input (which is usually impossible) or you formally prove equivalence.

How to Run the Same Inputs

You can run two functions on the same inputs in several ways, depending on your environment:

  • Write a test script that calls both functions with a list of inputs and compares the outputs.
  • Use a unit testing framework (e.g., JUnit, pytest, Mocha) to create parameterized tests that feed the same values to both functions.
  • Use a command-line tool like diff on the output files generated by running each function on the same input file.
  • In an interactive shell (Python, Node, etc.), manually call both functions with a few representative inputs.
  • Choosing Good Test Inputs

    The quality of your test inputs determines how much confidence you get. A few random numbers are not enough. You should include:

    • Boundary values – e.g., 0, 1, -1, empty strings, null, maximum integer.
    • Typical values – the kind of data the function is expected to handle in production.
    • Edge cases – inputs that might cause division by zero, overflow, or other special behavior.
    • Invalid inputs – to see if both functions handle errors the same way.

    If the functions take multiple arguments, test combinations of these categories. For example, if a function takes a list and a number, test an empty list with 0, a large list with a negative number, etc.

    Common Pitfalls and Misconceptions

    One common mistake is assuming that if two functions return the same output for a few inputs, they are duplicates. That is only true if the functions are pure. A function that reads a global variable, a file, the current time, or a random number can return different outputs for the same input, so matching outputs on one run does not prove duplication. Another pitfall is ignoring side effects. Two functions might return the same value but one of them also writes to a database or modifies an input list. Those are not duplicates in a behavioral sense, even if the return value matches. Also, be careful with floating-point numbers: two implementations might produce slightly different results due to rounding, so you should compare with a tolerance, not exact equality.

    Alternatives to Same-Input Testing

    Same-input testing is not the only way to find duplicates. Other approaches include:

    • Static analysis tools that scan code for similar patterns without running it.
    • Code review – a human looks for functions that seem to do the same thing.
    • Formal verification – proving mathematically that two functions are equivalent.
    • Mutation testing – modifying one function and seeing if tests still pass, which can hint at redundancy.

    Each method has trade-offs. Same-input testing is fast and easy, but not exhaustive. Static analysis can find near-duplicates even if they have different behavior on some inputs, but it may produce false positives.

    Costs and Benefits

    Testing functions on the same inputs is cheap to implement and run, especially if you already have a test suite. It can catch many duplicates early, saving maintenance costs later. However, it requires you to think about what inputs to use, and it does not guarantee that the functions are duplicates for all inputs. If the functions are complex, the number of possible inputs is huge, so you might miss differences. In those cases, a combination of testing and code review is more effective.

    Frequently Asked Questions

    Can I use random inputs to test for duplicates?

    Yes, random inputs can help, but they are not sufficient on their own. Random testing might miss edge cases that cause differences. It is better to combine random inputs with a curated set of boundary and typical values.

    What if the functions have side effects?

    If the functions have side effects (e.g., writing to a file, updating a database), then comparing only the return values is not enough. You need to also compare the side effects, or isolate the functions to be pure before testing.

    How do I compare floating-point outputs?

    Use a tolerance, such as abs(a - b) < 1e-9, instead of exact equality. This accounts for small rounding differences that are normal in floating-point arithmetic.

    Is it enough to test only the inputs the function is designed for?

    No, because duplicates might differ on inputs that are outside the intended domain but still possible. Testing only happy paths can miss bugs. Include invalid and unexpected inputs to see how both functions handle them.

    Can same-input testing prove that two functions are identical?

    No, it can only prove that they behave the same on the tested inputs. To prove full equivalence, you need formal verification or exhaustive testing, which is usually impractical.

    What if the functions are in different programming languages?

    You can still test them by running each with the same inputs and comparing the outputs, as long as you can invoke both from a common driver (e.g., a script that calls both and compares results). This is common in cross-language migration projects.