天天看點

讓你的代碼變得更優美一些

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();
  ..........
           

繼續閱讀