What's the best practise when adapting C-style functions which return a true/false to Java?
Here's a simple method to illustrate where the problem lies.
public static boolean fileNameEndsWithExtension( String filename, String fileExtension) {
return filename.endsWith( fileExtension );
}
Note that there's probably a more elegant way of filtering files (feel free to comment on this). Anyway, if filename is a null
value, does one:
- Return a false if filename is null? If so, how does one go about distinguishing between the case where filename is
null
and the case where theString
or file name doesn't end with a given file extension? - Change the return type to the wrapper class Boolean which allows a
null
value. - Throw an
Exception
and force the programmer to make sure that anull
value is never passed to the method? - Use another solution?