简体   繁体   English

适应方法返回true / false

[英]Adapting methods which return true/false

What's the best practise when adapting C-style functions which return a true/false to Java? 适应向Java返回true / false的C样式函数时的最佳实践是什么?

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: 无论如何,如果filename是一个null值,则执行以下操作:

  1. Return a false if filename is null? 如果filename为null,则返回false? If so, how does one go about distinguishing between the case where filename is null and the case where the String or file name doesn't end with a given file extension? 如果是这样,如何区分filename为null的情况和String或文件名不以给定文件扩展名结尾的情况?
  2. Change the return type to the wrapper class Boolean which allows a null value. 将返回类型更改为允许为null值的包装器类Boolean。
  3. Throw an Exception and force the programmer to make sure that a null value is never passed to the method? 引发Exception并强制程序员确保从未将null值传递给该方法吗?
  4. Use another solution? 使用其他解决方案?

You should throw a NullPointerException or an IllegalArgumentException if filename is null. 如果filename为null,则应引发NullPointerException或IllegalArgumentException。 I'll let you decide which is best. 我让你决定哪个最好。 There's a good debate on which to use in the question: IllegalArgumentException or NullPointerException for a null parameter? 有一个关于该问题的辩论: IllegalArgumentException或NullPointerException是否为null参数?

You do what makes sense in the problem domain of your particular application: 您可以在特定应用程序的问题域中执行有意义的操作:

  1. If it makes sense to say that the empty set of filenames ends with any extension, return true. 如果说空文件名以任何扩展名结尾都是合理的,则返回true。
  2. If it makes sense to say that the empty set of filenames ends with no extension, return false. 如果说空文件名以无扩展名结尾是合理的,则返回false。
  3. If it makes sense to say that no one should ever ask this question, let the code throw. 如果说没有人应该问这个问题是有道理的,请让代码抛出。
  4. If it makes sense to have a three-values result, sure, use Boolean. 如果有意义的话,可以使用三值结果,请使用布尔值。
  5. Or make a three-valued enum and return from THAT. 或做一个三值枚举并从那返回。

Most of the time, option 3 is going to be sensible, but no one here can rule out the applicability of the others to your application. 在大多数情况下,选项3会很明智,但是这里没有人可以排除其他选项对您的应用程序的适用性。 If you pass a lot of meaningful null filenames around for a good reason, it might make sense to choose one of the others. 如果您出于充分的原因传递了很多有意义的空文件名,则选择其他文件名可能很有意义。

I would use either 1 or 3. Preferably I would throw NullPointerExceptions or atleast use an assert . 我将使用1或3。最好是抛出NullPointerExceptions或至少使用assert

Returning nullable Booleans usually causes more trouble than they are worth, you have check for nulls etc. Besides fileNameEndsWithExtension() looks like a function that you'll be using only when you know that you have a valid filename. 返回可为空的布尔值通常会带来比其价值更大的麻烦,您需要检查空值等。除了fileNameEndsWithExtension()看起来像一个函数,只有当您知道自己具有有效的文件名时才会使用。

Also do not forget that fileExtension might also be a null. 同样不要忘记fileExtension也可能为null。

  1. return true IFF filename.endsWith( fileExtension ) 返回true IFF filename.endsWith(fileExtension)

I would return false if filename is null, and not bother with the distinction between null and any other non-matching values. 如果filename为null,我将返回false,并且不理会null与其他任何不匹配值之间的区别。

If null filename is a distinct state that needs to be verified and handled specifically, then this should be validated separately, preferably before checking endsWith(), but still retain the null check in endsWith() to prevent unnecessary runtime exceptions. 如果空文件名是需要特别验证和处理的独特状态,则应单独进行验证,最好在检查endsWith()之前进行验证,但仍将null检查保留在endsWith()中,以防止不必要的运行时异常。

The reason that I would choose the behaviour of null = false, is probably due to influence from relational databases. 我之所以选择null = false的行为,可能是由于关系数据库的影响。 The following query would only return rows which match the condition, everything else (nulls and mismatches) would be ignored. 以下查询将仅返回符合条件的行,其他所有内容(空和不匹配)将被忽略。

select * from filenames
 where filename like '&fileExtension';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM