写点什么

Sonar 使用的初次思考

作者:andy
  • 2022-10-28
    北京
  • 本文字数:1820 字

    阅读完需:约 6 分钟

1、问题一:添加私有构造方法替换隐藏公共构造方法


阐述:代码规范要求工具类添加私有构造方法。

吐槽:代码规范强制要求工具类添加私有构造函数,是对 JavaBean 的构造方法理解具有偏颇。

第一、私有构造方法,主要应用于经典的单例设计模式。

第二、如若实体类中不编写显式的构造方法,则 Java 虚拟机编译时会自动生成默认的构造函数。

第三、工具类访问静态方法,执行方法体,不一定要进行对象实例化,因此,代码中不需要刻意编写构造函数。

最后,SonarQube 代码规范属于规范管理系统,由人为配置控制,但配置此代码规范之人对于构造方法和访问权限的理解太过古板,不得不吐槽。


问题呈现:

Java 工具类:


package org.fuys.own.util;import java.util.UUID;public class FilesUtil {		/**	 * create new file name to save	 * @param fileName	 * @return	 */	public static String createFileNameToSave(String fileName){		if(fileName == null || "".equals(fileName)){			return null;		}		if(!fileName.contains(".")){			return UUID.randomUUID().toString();		}		return UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));	}}
复制代码


限制说明:


Add a private constractor to hide the implicit public one.Utility classes, which are a collection of static members, are not meant to be instantiated.Even abstract utility classes, which can be extended,should not have public constructors.Java adds an implicit public constructor to every class which does not define as least one explicitly.Hence, at least one non-public constructor should be defined.
复制代码


2、问题二:过于严格控制代码复杂度


阐述:代码规范过于严格控制代码复杂度

吐槽:在实际的代码开发中,不可能完全做到控制在一定层次内的复杂度。同时,规范将 try、if 与循环判断等同,这就在一定程度上增加了自我认定循环深度,因此,是不正确的。实际开发中,循环深度不应超过三层是合理要求,但是把非循环认定为增加循环复杂度,是不合适的。


问题呈现:


Java 业务判断代码:


GiftBook book = new GiftBook();		book.setAuthor("Peter Thiel");		List<GiftBook> bookList = MyBatisSqlSessionFactory.getSqlSession().selectList("org.fuys.owndb.vo.mapping.Gift.selectGiftBookList", book);		TestCase.assertNotNull(bookList);		if(bookList!=null){			for(int x=0;x<bookList.size();x++){				Gift gift = bookList.get(x);				if(gift instanceof Gift){					logger.info("gift --> " + x);					if(gift.getName()!=null){						try {							if(Pattern.compile("\\d+").matcher(gift.getName()).find()){								logger.info("Contains numbers");							}						} catch (Exception e) {							logger.error("Exception --> ", e);						}					}				}				if(gift instanceof GiftBook){					logger.info("giftBook --> " + x);				}				if(gift instanceof GiftSouvenir){					logger.info("giftSouvenir --> " + x);				}			}		logger.info(bookList.toString());		}
复制代码


限制说明:


Refactor this code to not nest more than 3 if/for/while/switch/try statements.

Nested if, for, while, switch, and try statements is a key ingredient for making what's known as "Spaghetti code".

Such code is hard to read, refactor and therefore maintain.


3、问题三:代码复杂度错误限定


阐述:代码规范错误的判断代码复杂度

吐槽:代码规范针对方法名进行复杂度限定,但是实际上产生代码复杂度的位置在于方法体中,从提示规范而言,也应该优化代码,而不是简单改变方法名。


问题呈现:

Java 源代码:


public void save(ProceedingJoinPoint point) throws Throwable{		// 通过ProceedingJoinPoint对象进行参数的传递		logger.info("++++++++ args is " + Arrays.toString(point.getArgs()));		Object obj = point.proceed(point.getArgs());		logger.info("Result is " + obj);	}
复制代码


限制说明:


The Cyclomatic Complexity of this method "save" is 13 which is greater than 10 authorized.

The cyclomatic complexity of methods should not exceed a defined threshold.

Complex code can perform poorly and will in any case be difficult to understand and therefore to maintain.


用户头像

andy

关注

还未添加个人签名 2019-11-21 加入

还未添加个人简介

评论

发布
暂无评论
Sonar使用的初次思考_andy_InfoQ写作社区