代码规范

反例:

	/**
	 * 
	 * (根据配置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);
	}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值