CompareTo Empty is unreliable as it tests reference equality, not object equality

Software ResiliencyCode Reliability

Why you should care

Using a CompareTo Empty string is considered unreliable as it tests reference equality and not object equality. Generally when programming, one cannot entirely rely on reference equality as it tends to be inaccurate and unreliable which is not ideal for the code.

Business Impacts

It is recommended to avoid CompareTo as it provides inaccurate results which is risky for the code.

Production Risk
5362

CAST Recommendations

References

https://stackoverflow.com/questions/531779/comparing-a-string-with-the-empty-string-java

Style Guide

How we detect

This code insight shows that, in Oracle, an empty string is equivalent to NULL which may not work, as expected, in an equality comparison statement. It is recommended to utilize IS NULL instead.

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.

See featuresHow it works