Android @NonNull 的用处 [英] Android @NonNull usefulness

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

问题描述

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

如果我尝试调用带有注释为 @NonNullnull 参数的方法,我会看到来自 Android Studio 的非常小的警告.只是警告??

单元测试怎么样?我应该使用空参数测试该方法吗?如果我这样做...我将收到一个 NullPointerException 并且我的测试将失败.>

假设我们是两个开发人员.一个在 API 上工作,另一个在以各种方式测试 API.作为第二个开发人员,我有责任测试所有内容,以便 API 防弹.这就是单元测试的全部意义,对吧?

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

如果其他人使用带有空参数的 API ......那么 API 会抛出一个 NPE.然后他会想:呃,那个 API 糟透了...... NPE !"他是对的.那个不检查他发送的参数是否为空的顽皮开发者应该面临 IllegalArgumentException 因为这是他的错而不是 API 的错!

我错了吗?

我认为这些注释会强制编译器显示错误,例如attempting to call methodName(@NonNull Object object) with null parameter.

更新 1

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

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

假设其他任何人(包括我)都会使用这些公共方法.其中一些采用绝不能为 null 的参数,否则一切都会崩溃.

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

  1. 继续使用 Java 文档支持的契约/注释(@NonNull),规定参数不能为空.不要检查空参数(否则 IDE 会警告我),并以某种方式祈祷我永远不会收到空参数;
  2. 与上面相同,但强制执行空检查(即使 IDE 会警告 condition 将始终为 false)并抛出 IllegalArgumentException 而不是导致 NPE,以防万一接收一个空参数;
  3. 停止使用合同/注释,使用 Java Doc 警告其他开发人员并为所有参数添加手动检查.

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

解决方案

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

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

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??

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.

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 ?

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

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!

Am I wrong ?

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

Update 1

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.

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

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. 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)-

解决方案

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天全站免登陆