Android 代码规范之Inspection 定制

本文主要讲述两个方面:

1)为何制定Radio_Inspections 规范及实操

2)IDE Inspection 使用 

第一部分 :为何制定Radio_Inspections 规范及实操

一 、制定Radio_Inspections 规范的目的

    (1)写代码时发现并解决不规范的书写或潜在的bug (其他工具未扫描出来)

    (2)解决部分代码性能问题

二、Radio_Inspections 能够解决的问题

       当前虽然IDE Inspection 能够提供实时提示,但是往往开发者没太注意,原因主要有以下几点:

    (1)IDE Inspection 默认检查项过多,扫描结果过多,处理费时,不愿处理;

    (2)IDE Inspection 提示级别多为warning ,让人容易忽视(有时往往会产生致命np问题);

    (3)IDE Inspection 部分检查项,默认关闭的(规则太多,开发者也不会一一浏览过滤);

          针对上述三个问题,freddy想出相应对策,来制定符合自身项目的Inspection 规则(Radio_Inspections) :

(1)浏览相关规则,结合平时开发,将部分默认未开启有意义的检查项开启;

(2)将提示级别设置为error ;

(3)优先处理error 级别问题。


Radio_Inspections新增 规则如下(暂时未引入阿里插件检查项):

(1)Class structure

        1) Utility class without ‘private’ constructor

             工具类 建议添加 私有构造方法,防止实例化

        2)Class with only ‘private’ constructors should be declared ‘final'

              工具类 建议 添加final 修饰符

(2)Code style issues 

        1) ‘expression.equals(“literal”) rather than “literal”.equals(expression)

               防止潜在np

(3)Control flow issues

        1) Unnecessary ‘null’ check before ‘instanceof’ expression  

(4)Declaration redundancy

        1) Access static member via instance reference

(5)Imports

        1)Static import

(6)Java language level migration aids 

     1) Identical ‘catch’ branches in ‘try’ statement (语法糖)

     案例:

图 案例
图 建议写法

    同时,对应关闭 “Multiple-catch can be split into separate catch blocks” 

(7)Memory issues

      1)Inner class may be ‘static’

(8)Probable bugs

        1)Return of "null"

        public 方法返回 null ,建议添加 @Nullable (强制添加,不然提示)

       2) equals( ) and hashCode( ) not paired 

  (9) Inheritance issues

           1) @Overridate

 (10) Numeric issues

         1)Divide by zero

         2) 'long' literal ending with "l" instead of "L'

(11) Serialization issues 

      1)Serializable class without 'serialVersionUID'

       2)‘serialVersionUID’ field not declared 'private static final long'

       3)Transient field in non-serializable class

             防止 transient 关键字 和 @Transient 用混


另外,有一些比较好的建议(暂时未添加):

(1)Performance issues 

      1)Collection without initial capacity (638+)

            设置初始容量

(2)  Magic number  (1273 个)


建议关闭的默认规则 :

(1)Javadoc issues  

        直接checkStyle 就可以

(2)General  Default File template   (模板)

         This inspections reports usage of the default file template for File Header.

(3)Syntax error inspection

          Allows you to see syntax errors in the results of batch code inspection.

(4)Spelling 

(5)Android Lint :Internationlization

        1) Using left / right instead of start/end attributes (1640+)


第二部分  :IDE Inspection 使用 

一、默认情况  (Project Default)

1)Specify Inspection scope 

      选择Inspection scope  以及 Inspection profile 

图 Specify Inspection scope 

       Inspections profile 主要有两种存储方式  :IDE (选择IDE即可)  和 project,建议使用IDE 存储,切换分支不会发生变化;

图 Inspections profile 存储选择方式

二、规则挑选过滤  

       Inspection 检查 主要 分为 :Error 、Blocker 、Critical、Major 等等  。可以通过左侧漏斗按钮进行相关过滤。

图 规则过滤选择

三、过滤机制  

(1)检查结果相关点

图 过滤方式选择

        右侧对应Suppress 方式 (分别针对 method/class/ all inspections for class),尽可能选择粒度较小的方式,如method 。

(2)具体Method过滤 

图 方法过滤实例

(3)检查项不配置,则对整个工程都不会检查


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • 原文:https://legacy.python.org/dev/peps/pep-3333 PEP:3333标题...
    老周_o_o阅读 2,429评论 0 5
  • 故地密竹新,慌急刻字寻。 山盟依旧在,不见意中人。 注:以“人不风流枉少年”任意平声字做韵脚。 (中华新韵)
    云淡风轻110阅读 695评论 14 31
  • 遇见另外一个你 22的结束,100天新的征程开启 目标一 减到100斤 1 晚上不吃饭(聚餐少吃)...
    A我愿阅读 324评论 2 1
  • 当你决定不再去打扰她的时候, 内心有太多的纠结和不舍。 因为只有你自己知道, 为了这个决定, 你是用了多大的勇气,...
    丹华君阅读 297评论 0 0