IllegalArgumentException: Array cannot be null in BinarySearch
22K reputation · 23 May 2021, 14:05 UTC
Current Behavior
The BinarySearch implementation in thealgorithms throws IllegalArgumentException("Array cannot be null") when the caller passes a null array reference. The repository’s README and inline comments do not clarify whether this exception is intentional or an oversight.
Unresolved Decision
Because no unit tests cover the null‑input case and no fallback strategy is documented, the library’s public contract for handling a null array remains ambiguous. The choice between maintaining the current exception, providing a more descriptive message, or returning a sentinel value (e.g., -1) is unresolved.
Key Questions
- Should BinarySearch continue to throw an
IllegalArgumentExceptionwhen the input array isnull, or should it return a sentinel value such as-1to indicate “not found”? - If the exception is retained, would a more detailed message improve developer experience without breaking existing consumers?
- What impact would adding a null‑check and sentinel return have on backward compatibility and existing unit tests that expect the exception?