Android Notes思码逸问题处理记录

Beth ·
更新时间:2024-09-20
· 75 次阅读

目录

前言

Static non-final field names should comply with a naming convention

Jump statements should not be redundant

Deprecated code should be removed

Unused method parameters should be removed

[p3c]To judge the equivalence of floating-point numbers, == cannot be used for primitive types, while equals cannot be used for wrapper classes.

THK

前言

从年初开始,我司便开始将思码逸作为了 OKR 必选项,从开始的抵触,到现在的坦然,也算是被锤之路...

简单附上对应问题解决的思路或者方式,以便于小伙伴类似直接操作。

个人对于思码逸的看法,其实多属于还是基于阿里一些规范,虽说有点繁琐,甚至有些许难受,长远来看,未尝不是一件好事儿。

点滴做起,基于思码逸,一起来改善自己代码吧~

先附上阿里编码指南:

p3c

Static non-final field names should comply with a naming convention

提示如下:

大概的意思就是静态非 final 命名不规范,这里直接追加 final 即可。

Jump statements should not be redundant

问题代码如下:

提示如下:

Jump statements such as return and continue let you change the default flow of program execution, but jump statements that direct the control flow to the original direction are just a waste of keystrokes.

Fix:

独立出来,不满足条件直接 return 即可。

Deprecated code should be removed

问题代码如下:

提示如下:

此规则旨在用作跟踪标记为已弃用的代码的一种方式。弃用的代码最终应该被删除。

Fix:

历史包袱,只能看具体代码,具体分析,如果有替代则使用推荐的。(真是一句废话...

Unused method parameters should be removed

问题代码如下:

提示如下:

未使用的参数具有误导性。无论传递给此类参数的值如何,行为都是相同的。

附上对应代码图:

当然以下是一些例外情况:

that are annotated with @javax.enterprise.event.Observes

in overrides and implementation methods

in interface default methods

in non-private methods that only throw or that have empty bodies

in annotated methods, unless the annotation is @SuppressWarning("unchecked") or @SuppressWarning("rawtypes"), in which case the annotation will be ignored

in overridable methods (non-final, or not member of a final class, non-static, non-private), if the parameter is documented with a proper javadoc.

[p3c]To judge the equivalence of floating-point numbers, == cannot be used for primitive types, while equals cannot be used for wrapper classes.

问题代码如下:

提示如下:

浮点数之间的等值判断,基本数据类型不能用 == 来比较,包装数据类型不能用 equals 来判断

如何 Fix?

本质理解为一个精度问题,例如某些情况下 0.1 和 0.111 是相等的。

只需要将原本的 == 替换为 Float.compare(a,b) == 0 即可。

Others 知识点回顾:

回顾基本数据类型和包装类型:

基本类型包装类型
booleanBoolean
charCharacter
intInteger
byteByte
shortShort
longLong
floatFloat
doubleDouble

而它们之间区别如下:

基本类型均有默认值,而包装类型初始化则是 NULL。也就是说基本类型可以用直接使用,而包装类型必须实例化后才可以使用;

包装类型实际是对象的引用,new 一个包装类型的时候,实际上生成了一个指针指向此对象,而基本类型则事直接存储数据值;

这里额外回顾下 == 和 equals 区别吧:

== 对于基本类型来说是比较值,对于引用类型来说则是比较引用地址;

equals 默认情况下是比较引用地址,只是很多类重写了 equals 方法,比如 String、Integer 等把它变成了比较值,所以说一般情况下 equals 比较的是值是否相等。

THK

你真的懂 == 和 equals 的区别吗?

以上就是Android Notes思码逸问题处理记录的详细内容,更多关于Android Notes思码逸问题处理的资料请关注软件开发网其它相关文章!



Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号