天天看点

让你的代码变得更优美一些

1.变量集中:

      我们在写类的时候一般都是将变量写在类的最前面,后面再写方法,这样写是便于代码阅读。同理方法的写法也一样,我们应该在写方法时,将方法中用到的变量全都在方法的最前面集中申明或赋值,

而不是以结构化的编程思维流程式地哪里要用变量了再写在哪里。

修改前:
               StockChange change=new StockChange();
		DataBaseConnection h2Conn=null;
		Logger.info("更新股票的估值增值");
		try {
			String priceSumsql="select sum(t.n_price)priceSum from FACT_FUND_HLDDETAIL t where t.s_type='E' and t.f_code='"+fundCode+"'";
			String priceUpdatesql="update FACT_FUND_HLDDETAIL a set a.n_price=(select t.n_amount*f.f_newest_price n_price from FACT_FUND_HLDDETAIL t, fu_stock_market f where t.s_symbol=f.vc_symbol and a.s_symbol=t.s_symbol) where a.f_code='"+fundCode+"'";
			 h2Conn = new DataBaseConnection(DataBaseConnection.getH2Connection());
			//更新前市值
		    h2Conn.setResultSet(h2Conn.getStmt().executeQuery(priceSumsql));
		    String pricePreSum="";
		    String priceLastSum="";
修改后:
DataBaseConnection h2Conn = null;
        String pricePreSum = "";
        String costPreSum = "";
        String priceLastSum = "";
        String costLastSum = "";
        ......
           

2.常量集中:

   有时,在程序中我们会有大量的类似的字符串或其他类型散布各个方法里面,特别是sql语句,sql语句其实我们可以看成是基本不变的“常量”,因此我们可以把这些分散的sql以类成员变量的形式都集中写在类的最前面。这样处理代码结构会更加清晰,便于查看的同时也便于集中修改,每个方法不也不会因为一些长的字符串sql而显得极其难看。

修改前:
public class ValuationBo {
	private ErrorsDeal errors;
	
	/**
	 * 更新股票的估值增值
	 * @param fundCode
	 * @return
	 */
	public StockChange updateStockPrice(String fundCode){
		StockChange change=new StockChange();
		DataBaseConnection h2Conn=null;
		try {
			String priceSumsql="select sum(t.n_price)priceSum from FACT_FUND_HLDDETAIL t where t.s_type='E' and t.f_code='"+fundCode+"'";
			String priceUpdatesql="update FACT_FUND_HLDDETAIL a set a.n_price=(select t.n_amount*f.f_newest_price n_price from FACT_FUND_HLDDETAIL t, fu_stock_market f where t.s_symbol=f.vc_symbol and a.s_symbol=t.s_symbol) where a.f_code='"+fundCode+"'";
			 h2Conn = new DataBaseConnection(DataBaseConnection.getH2Connection());
			//更新前市值
		    h2Conn.setResultSet(h2Conn.getStmt().executeQuery(priceSumsql));
		    String pricePreSum="";
		    String priceLastSum="";
		    while(h2Conn.getResultSet().next()){
		    	pricePreSum=h2Conn.getResultSet().getString(1);
		    }
			
			//更新市值
			h2Conn.getStmt().executeUpdate(priceUpdatesql);
			//更新后市值
			h2Conn.setResultSet(h2Conn.getStmt().executeQuery(priceSumsql));
			while(h2Conn.getResultSet().next()){
				priceLastSum=h2Conn.getResultSet().getString(1);
			}
			if(pricePreSum!=null&&!"".equals(pricePreSum)){
				change.setPrePrice(Double.parseDouble(pricePreSum));
			}
			if(priceLastSum!=null&&!"".equals(priceLastSum)){
				change.setLastPrice(Double.parseDouble(priceLastSum));
			}
		} catch (Exception e) {
			Logger.error("更新股票的估值增值失败:"+e.getMessage());
			errors.insertFutursErrors("1","更新股票的估值增值失败",e.getMessage(),new Date());
		}finally{
			 DataBaseConnection.closeConnection(h2Conn.getConn(), h2Conn.getStmt(), null, h2Conn.getResultSet());
		}
		return change;
	}
	
	/**
	 * 更新期贷的市值
	 * @param fundCode
	 * @return
	 */
	public StockChange updateFutures(String fundCode){
		StockChange change=new StockChange();
		DataBaseConnection h2Conn=null;
		String pricePreSum="";
		String costPreSum="";
		String priceLastSum="";
		String costLastSum="";
		try {
			String priceSumsql="select sum(t.n_price)priceSum,sum(t.n_cost)costSum from FACT_FUND_HLDDETAIL t where t.s_type='E' and t.f_code='"+fundCode+"'";
			String deletesql="delete FACT_FUND_HLDDETAIL t where  t.s_type='E' and t.f_code='"+fundCode+"'";
			String futruesInsertsql="insert into FACT_FUND_HLDDETAIL (t_date,f_code, s_symbol,s_subcode,s_type, s_type_assist,  n_price, n_cost,n_amount,s_exchange,VC_ISRB201,N_INTEREST,N_YDISCOUNT) select sysdate,t.vc_futures_code,t.vc_exchange_code,'','FU',decode (t.vc_bs_flag,'1' ,'B','2','S'),t.f_newest_price*decode (t.vc_bs_flag,'1', t.f_buy_position,'2',t.f_sale_position),'-1','-1','-1','-1','-1','-1' From Fu_hold_detail t where  t.vc_exchange_code!=''";
			 h2Conn = new DataBaseConnection(DataBaseConnection.getH2Connection());
			//更新前市值
		    h2Conn.setResultSet(h2Conn.getStmt().executeQuery(priceSumsql));
		    while(h2Conn.getResultSet().next()){
		    	 pricePreSum=h2Conn.getResultSet().getString(1);
				 costPreSum=h2Conn.getResultSet().getString(2);
		    }
			
			//删除记录
			h2Conn.getStmt().executeUpdate(deletesql);
			//更新期贷数据
			h2Conn.getStmt().executeUpdate(futruesInsertsql);
			//更新后市值
			h2Conn.setResultSet(h2Conn.getStmt().executeQuery(priceSumsql));
			while(h2Conn.getResultSet().next()){
				 priceLastSum=h2Conn.getResultSet().getString(1);
			     costLastSum=h2Conn.getResultSet().getString(2);
			}
			if(pricePreSum!=null&&!"".equals(pricePreSum)){
				change.setPrePrice(Double.parseDouble(pricePreSum));
			}
			if(costPreSum!=null&&!"".equals(costPreSum)){
				change.setPreCost(Double.parseDouble(costPreSum));
			}
			if(priceLastSum!=null&&!"".equals(priceLastSum)){
				change.setLastPrice(Double.parseDouble(priceLastSum));
			}
			if(costLastSum!=null&&!"".equals(costLastSum)){
				change.setLastCost(Double.parseDouble(costLastSum));
			}
		} catch (Exception e) {
			Logger.error("更新期贷的市值失败:"+e.getMessage());
			errors.insertFutursErrors("1","更新期贷的市值失败",e.getMessage(),new Date());
		}finally{
			 DataBaseConnection.closeConnection(h2Conn.getConn(), h2Conn.getStmt(), null, h2Conn.getResultSet());
		}
		return change;
	}

	public ErrorsDeal getErrors() {
		return errors;
	}

	public void setErrors(ErrorsDeal errors) {
		this.errors = errors;
	}
       .......
}
改后:
    private static final String deleteContractSql = "delete  from fu_futures_contract";
    private static final String insertContractSql = new StringBuilder().append("insert into  fu_futures_contract(Vc_exchange_code,").append("Vc_exchange_name,Vc_futures_code, Vc_futures_name,").append("F_hand_number,F_speculate,F_maintain,").append("D_last_day,F_max_hand,F_max_position,Vc_kind_code,").append("Vc_kind_name,F_min_unit)").append("values(?,?,?,?,?,?,?,?,?,?,?,?,?)").toString();
    private static final String deleteMarginRatioSql = "delete from fu_marginrate_ratio";
    private static final String insertMarginRatioSql = new StringBuilder().append("insert into  fu_marginrate_ratio(Vc_fund_code,").append("Vc_futures_code,Vc_user_name,Vc_buy_marginrate_money,").append("Vc_sale_marginrate_money,Vc_buy_marginrate_hands,").append("Vc_sale_marginrate_hands,Vc_buy_maintain_money,").append("Vc_sale_maintain_money,Vc_buy_maintain_hands,Vc_sale_maintain_hands)").append("values(?,?,?,?,?,?,?,?,?,?,?)").toString();
  ..........
           

继续阅读