项目代码优化(一)

来源:互联网 发布:数据分析ppt分享 编辑:程序博客网 时间:2024/05/22 02:07

项目代码优化(一)

a.正确使用Integer

  • 场景
    Map<String,Object> pp = new HashMap<>();    pp.put("subscriberCode", sn);    pp.put("currentTime", UTCTimeUtil.getUTCDateTimeStr());    pp.put("durationBalance", new Integer(0));
  • 解释
  • 英文
com.snt.crm.business.service.impl.SubTrafficCDRServiceImpl.saveVsatCdr(JSONObject) invokes inefficient new Integer(int) constructor; use Integer.valueOf(int) instead    Performance - Method invokes inefficient Number constructor; use static valueOf instead    Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) allows caching of values to be done by the compiler, class library, or JVM. Using of cached values avoids object allocation and the code will be faster.Values between -128 and 127 are guaranteed to have corresponding cached instances and using valueOf is approximately 3.5 times faster than using constructor. For values outside the constant range the performance of both styles is the same.Unless the class must be compatible with JVMs predating Java 1.5, use either autoboxing or the valueOf() method when creating instances of Long, Integer, Short, Character, and Byte.findbugs:DM_NUMBER_CTOR Efficiency > Processor use 


  • 中文

1.对于(-128,127)之间的数值,使用Integer.valueOf(int);方式比new Integer构造函数凡是执行效率快3.5倍。

2.除非必须兼容jdk1.5编译器,否则对于Long,Integer,Short,Character,Byte尽量使用自动装箱和valueOf()方法来获取目标数值代替使用new方式来创建新的实例,因为jvm中缓存了这个范围内的值(这个值可能由编译器完成的类库或JVM缓存)

b.执行除法时正确保留精度

  • 场景
paramMap.put("chargeBeginTime", chargeBeginTime); int countCustomer=customerInfoService.listPageCount(new HashMap());//取得总循环次数int countPage=(int) Math.floor(countCustomer/SystemConstants.BATCH_PROCESS_NUM);
  • 解释
  • 英文
Dodgy - int division result cast to double or floatThis code casts the result of an integer division operation to double or float. Doing division on integers truncates the result to the integer value closest to zero. The fact that the result was cast to double suggests that this precision should have been retained. What was probably meant was to cast one or both of the operands to double before performing the division. Here is an example:int x = 2;int y = 5;// Wrong: yields result 0.0double value1 =  x / y;// Right: yields result 0.4double value2 =  x / (double) y;This rule is deprecated, use S2184 instead.findbugs:ICAST_IDIV_CAST_TO_DOUBLE Reliability > Instruction
  • 中文

1.执行2/5时,在java语法中,其实是取整操作,结果为0,但是真实的需要结果并不是这个,所以此处需要保留其原有的精度。

2.可以使用double values = 2 / (double) 5;方式来计算,得到结果为:0.4

c.空指针判断

  • 场景
SubscriberInfo si=siMapper.findSubscriberInfoByCode(subscriberCode);CustomerInfo customerInfo=ciMapper.load(si.getCustomerId());
  • 解释
  • 英文
    Nullcheck of si at line 283 of value previously dereferenced in com.snt.crm.business.service.impl.SubscriberInfoServiceImpl.updatePassword(String, String, String)打开  Debt: 1h规则  修改日志    Correctness - Nullcheck of value previously dereferenced    A value is checked here to see whether it is null, but this value can't be null because it was previously dereferenced and if it were null a null pointer exception would have occurred at the earlier dereference. Essentially, this code and the previous dereference disagree as to whether this value is allowed to be null. Either the check is redundant or the previous dereference is erroneous.findbugs:RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE Maintainability > Understandability
  • 中文
1.此处si对象可能为空,所以此处可能抛出空指针异常情况2.处理:做空判断if(si!=null)

d.异常捕获未处理

  • 场景
try{    mailBusinessIntf.sendMail(customerInfos.get(j).getCustomerEmail(),"当月账单",paramModel,true,null,EmailConst.HISTORY_BILL);//尊敬的客户您好,截止当前(或本月  号),您本月已消费**元,其中当月套餐费用共**元,套餐外消费**元。套餐使用情况:套餐流量为**M,已使用**M,剩余**M;流量包流量为**M,已使用**M,剩余**M。}catch(Exception e){}
  • 解释
  • 英文
    Exception is caught when Exception is not thrown in com.snt.crm.business.intf.impl.HistoryBillsIntfImpl.saveHistoryBill()打开  Debt: 1h规则  修改日志    Dodgy - Exception is caught when Exception is not thrown    This method uses a try-catch block that catches Exception objects, but Exception is not thrown within the try block, and RuntimeException is not explicitly caught. It is a common bug pattern to say try { ... } catch (Exception e) { something } as a shorthand for catching a number of types of exception each of whose catch blocks is identical, but this construct also accidentally catches RuntimeException as well, masking potential bugs.findbugs:REC_CATCH_EXCEPTION Maintainability > Understandability
  • 中文
1.对于这种情况try-catch捕获异常,但是当异常产生后,在catch中并未处理,这时系统会抛出RuntimeException异常,但是这种报错就像告诉你,系统有异常,并不能快速定位到根本原因。2.建议处理如下:方式一:catch(Exception e){    log.error(e.getMessage());//打印日志到控制台}方式二:catch(Exception e){    log.error(e.getMessage()+":"+"自定义错误提示");}
0 0
原创粉丝点击