The Daily WTF
Follow
Representative Line: Springs are Optional
Optional types were introduced to mitigate null reference exceptions, often called the "billion dollar mistake." They work by wrapping a value, ensuring the Optional itself always exists. This allows developers to safely check for the presence of an actual value. Well-designed Optional implementations offer helpful methods for value manipulation. However, the concept can be misused, as demonstrated by the creation of static Optional constants for TRUE and FALSE. This practice is questionable, especially in languages that already provide such boolean constants. Creating constants like Optional.of(Boolean.TRUE) essentially equates to TRUE = TRUE. Such implementations suggest underlying design flaws. The author encountered this in a Spring web API application, where custom filters were implemented instead of using framework capabilities. The example of static Optional TRUE and FALSE declarations highlights a broader issue of reinventing existing solutions. It implies a misunderstanding of the original problem and the tool intended to solve it. This unnecessary complexity can lead to confusing and inefficient code.