averbraeck / djutils

Delft Java Utilities for statistics, stochastics, data analysis and serialization
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Throw.whenNull() without "may not be null" #25

Open WJSchakel opened 11 months ago

WJSchakel commented 11 months ago

It is un-progammer-like to write may not be null for the frequent null-checks that are performed with Throw.whenNull(). For example in Point2d.closestPointOnSegment():

        Throw.whenNull(segmentPoint1, "linePoint1 may not be null");
        Throw.whenNull(segmentPoint2, "linePoint2 may not be null");

Note also that the names are not correct in the provided messages. It is not possible in java to know what the name of a variable was at the level of the calling class. That is, Throw cannot know about the names segmentPoint1 and segmentPoint2. Therefore, simplifying this to Throw.whenNull(segmentPoint1) does not suffice.

What we can do is the following:

        Throw.whenNull(segmentPoint1, "segmentPoint1");
        Throw.whenNull(segmentPoint2, "segmentPoint2");

This makes it very clear whether the name is correctly given to a programmer. Throw can figure out whether to add " may not be null" by checking whether there is a blank in the provided message. Specifically using a regular expression pattern matching with \s. Note that this is only done when in fact a NullPointerException will be thrown. More elaborate messages, that should contain a \s, are still possible in this way and will not be appended with " may not be null".