您的位置:首页 > 编程语言

代码规范

2018-01-13 09:25 190 查看
反例:

/**
*
* (根据配置id查询游戏配置信息)
* @Title getGameConfigById
* @param configId
* @return RetResult返回类型
* @author SZY
* @date 2017年12月21日下午5:17:43
* @throws 查询异常
*/
@RequestMapping(value = "/getGameConfigById", method = RequestMethod.POST)
public @ResponseBody RetResult getGameConfigById(Long configId){
String code = CommConstant.GWSCOD0000;
String message = CommConstant.GWSMSG0000;
GwsLogger.info("查询单条游戏配置信息开始:code={},message={}",code,message);

//参数校验
if(configId < 1 || null == configId){
GwsLogger.error("查询单条游戏配置信息入参ID为空:code={},message={},configId={}", code, message,configId);
return RetResult.setRetDate(CommConstant.GWSCOD0003, CommConstant.GWSMSG0003, configId);
}

GameConfig gameConfig = null;

try{

gameConfig = gameConfigService.getGameConfigById(configId);

}catch(Exception e){

code = CommConstant.GWSCOD0001;
message = CommConstant.GWSMSG0001;
GwsLogger.error("查询单条游戏配置信息异常:code={},message={},e={}", code, message, e);
}

GwsLogger.info("查询单条游戏配置信息结束,code={},message={}", code, message);
return RetResult.setRetDate(code, message, gameConfig);
}


反思:这段代码虽然没有大的毛病,但是还有可以优化的地方,1.参数校验时应该先判断为空,再判断configId是否小于1; 2.try代码块应该是属于一个整体,不应该用多个空格分开。

正例:

/**
*
* (根据配置id查询游戏配置信息)
* @Title getGameConfigById
* @param configId
* @return RetResult返回类型
* @author SZY
* @date 2017年12月21日下午5:17:43
* @throws 查询异常
*/
@RequestMapping(value = "/getGameConfigById", method = RequestMethod.POST)
public @ResponseBody RetResult getGameConfigById(Long configId){
String code = CommConstant.GWSCOD0000;
String message = CommConstant.GWSMSG0000;
GwsLogger.info("查询单条游戏配置信息开始:code={},message={}",code,message);

//参数校验
if(null == configId || configId < 1){
GwsLogger.error("查询单条游戏配置信息入参ID为空:code={},message={},configId={}", code, message,configId);
return RetResult.setRetDate(CommConstant.GWSCOD0003, CommConstant.GWSMSG0003, configId);
}

GameConfig gameConfig = null;
try{
gameConfig = gameConfigService.getGameConfigById(configId);
}catch(Exception e){
code = CommConstant.GWSCOD0001;
message = CommConstant.GWSMSG0001;
GwsLogger.error("查询单条游戏配置信息异常:code={},message={},e={}", code, message, e);
}

GwsLogger.info("查询单条游戏配置信息结束,code={},message={}", code, message);
return RetResult.setRetDate(code, message, gameConfig);
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 代码规范