Android @NonNull有用性 [英] Android @NonNull usefulness

查看:165
本文介绍了Android @NonNull有用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几次阅读和提问后,喜欢这个我想知道使用@NonNull Android支持注释是否有意义.

After a few reading and questions like this one I was wondering if there was a point in using @NonNull Android Support Annotation.

如果我尝试调用带有 null 参数的方法,该方法的注释为 @NonNull ,我会在Android Studio中看到一个很小的警告.只是警告?

I can see a very small warning from Android Studio if I attempt to call a method with a null parameter that is annotated as @NonNull. Just a warning??

单元测试呢? 我应该用空参数测试该方法吗?如果这样做...,我将得到一个NullPointerException,而我的测试将失败.

What about Unit Testing ? Should I test the method with a null paramter? If I do ... I will get a NullPointerException and my test will fail.

假设我们是两个开发人员.一种以各种方式使用API​​,另一种则以各种方式测试API. 作为第二个开发人员,我有责任对所有内容进行测试,以确保API不受防弹.这就是单元测试的全部内容,对吧?

Let's say we are two developers. One working on the API and one testing the API in all kind of manners. As the second developer, it's my responsibility to test everything so that the API is bullet-proof. That's the whole point of Unit Testing, right ?

那么……那么使用@NonNull的第一个开发人员有什么意义呢?

So then ... what's the point of the first developer using @NonNull ?

如果其他人将此参数与null参数一起使用...,则该API会抛出 NPE .然后,他会想到:抱歉,该API很烂... NPE!"他会是对的.那个顽皮的开发人员,如果不检查他发送的参数是否为空,应该面对一个 IllegalArgumentException ,因为这是他的错,而不是API的错!

If someone else were to use this API with a null parameter ... then the API would throw a NPE. He then would think : "Urg, that API sucks... NPE !" and he would be right. That naughty dev for not checking if the parameter he sent is null or not should be faced with an IllegalArgumentException because it's his fault not that of the API's!

我错了吗?

我认为这些注释将强制编译器显示类似attempting to call methodName(@NonNull Object object) with null parameter的错误.

I thought these annotations would enforce the compiler to show errors like attempting to call methodName(@NonNull Object object) with null parameter.

好的,谢谢大家的评论.如果可能的话,我想总结一下问题".我正要面对这里.以抽象的方式.

Alright thank you all for your comments. I'd like to summ up if possible regarding the "issue" I am facing here. In an abstract way.

我写了一些代码(一个API,一个库,一个类,等等),并用提供功能的公共方法包装了私有内部代码.

I write some code (an API, a Library, a Class, whatever) with private internal code wrapped by public methods that provides features.

假设这些公共方法将被其他任何人(包括我)使用. 他们中的一些人接受的论点绝不能为 null ,否则所有地狱都会崩溃.

Assume these public methods will be used by anyone else (including me). Some of them take arguments that must never be null or else all hell breaks loose.

在阅读您的评论时,我面临以下选择:

Reading your comments, I am facing the following options :

  1. 继续使用Java文档规定parameter must not be null支持的合同/注释(@NonNull).不要检查null参数(否则IDE会警告我),并且以某种方式祈祷我永远不会收到null参数;
  2. 与上面相同,但是强制执行空检查(即使IDE会警告condition will always be false)并抛出IllegalArgumentException,而不是在我收到空参数的情况下引发NPE;
  3. 停止使用合同/注释,使用Java Doc来警告其他开发人员,并手动检查所有参数.
  1. Keep on using contracts / annotations (@NonNull) supported by Java Documentation stipulating parameter must not be null. Don't check for null parameters (or else IDE will warn me) and, somehow, pray that I will never receive a null parameter ;
  2. Same as above, but enforce a null check (even though IDE will warn that condition will always be false) and throw IllegalArgumentException instead of causing an NPE in case I receive a null parameter ;
  3. Stop using contracts / annotations, use Java Doc to warn others dev and add manual check for all parameters.

最后,对于单元测试……我知道我不能使用防弹代码,但是我喜欢对它们进行猴子测试".我的代码尽可能地防止我的代码出现意外行为,并验证过程(我相信这是单元测试的基础)-

Finally, for unit testing ... I know I can not have bullet-proof code, but I like to "monkey-test" my code as much as possible to prevent unexpected behavior from my code as well to validate the process (which is at the base of Unit Testing, I believe)-

推荐答案

其主要目的是为您的同事提供信息.一个人永远不会是一个大型项目的唯一程序员.使用NotNull会告诉其他程序员,该函数的约定意味着您永远无法向其发送null,因此他们不会这样做.否则,我可能会做出合理的假设,即调用setFoo(null)将清除Foo,而API无法处理没有Foo的情况.

Its main purpose is informational to your coworkers. One person is never the sole programmer of a large project. Using NotNull tells other programmers that the contract of the function means you can never send a null to it, so they don't do it. Otherwise I may make a logical assumption that calling setFoo(null) will clear Foo, whereas the API can't handle not having a Foo.

这篇关于Android @NonNull有用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆