--- title: '開發小技巧' disqus: hackmd --- ###### tags: `system` 開發小技巧 === [TOC] ## 開發小技巧的敘述 常見的開發小技巧 1. 一個東西很複雜的話,可以先在for迴圈外面宣告全域變數的List,之後透過for迴圈去List.add(it),若之後要轉hashmap的話,可以之後再轉Hashmap,缺點是比較費時,但是程式碼比較好懂,也比較容易撰寫。 2. 在本地端要想辦法取得相對的路徑,這樣才能去夾帶檔案,因為部署的server時,你的絕對路徑會有問題 ## 不會被覆蓋 ```java= for (Long eachCid : CustomerID_OptimizationFromAllieList) { // each cid Long eachAccountMCCID = AllMCC_CustomerIdMap.get(eachCid ); // 拿到each CID的MCCID這樣才能打API GoogleAdsServiceClientAgent gasAgent = new GoogleAdsServiceClientAgent(eachAccountMCCID, clientOAuthPropertiesBean); GoogleAdsClient googleAdsClient = gasAgent.giveMeAnUsefulGoogleAdsClient(); List<CampaignBean> SingleCustomerCampaignBeanList = googleCampaignService .getCampaignListFromGoogleAdsApi(googleAdsClient, eachCid );// List<Long> campaignList=new ArrayList<Long>(); for (int i = 0; i < SingleCustomerCampaignBeanList.size(); i++) { Long CampaignID = SingleCustomerCampaignBeanList.get(i).getCampaignID(); List<CampaignPerformanceBean> SingleCampaignPerformanceBeanList = googleCampaignService .getCampaignPerformance(googleAdsClient, eachCid, CampaignID, start_Date, end_Date); if (SingleCampaignPerformanceBeanList.size() >= numbersofDaysCostGreaterThanZero) {// 符合條件就加入 campaignList.add(CampaignID); CustomerIDwhichInAI_HashMap.put(eachCid, campaignList); } // end if check 每個campaign有沒有符合兩個條件 } // end for loop all campaign in single CID } ``` ## 需要使用if敘述來確保不會被覆蓋 ```java for (int i = 0; i < dataListCampaignFromAllie_size; i++) { // each campaign Long campaignID =Long.valueOf( dataListCampaignFromAllie.getResponseResult().get(i).getCampaignId( ) ) ; Long cid = Long.valueOf(dataListCampaignFromAllie.getResponseResult().get(i).getCid()); Long eachAccountMCCID = AllMCC_CustomerIdMap.get(cid ); GoogleAdsServiceClientAgent gasAgent = new GoogleAdsServiceClientAgent(eachAccountMCCID, clientOAuthPropertiesBean); GoogleAdsClient googleAdsClient = gasAgent.giveMeAnUsefulGoogleAdsClient(); List<CampaignPerformanceBean> SingleCampaignPerformanceBeanList = googleCampaignService .getCampaignPerformance(googleAdsClient, cid, campaignID, start_Date, end_Date); if (SingleCampaignPerformanceBeanList.size() >= numbersofDaysCostGreaterThanZero) {// 符合條件就加入 if(CustomerIDwhichInAI_HashMap.containsKey(cid ) ) { List<Long> campaignList=CustomerIDwhichInAI_HashMap.get(cid) ; campaignList.add( campaignID); CustomerIDwhichInAI_HashMap.put(cid, campaignList); }else { List<Long> campaignList=new ArrayList<Long>(); campaignList.add( campaignID); CustomerIDwhichInAI_HashMap.put(cid, campaignList); } } // end if check 每個campaign有沒有符合兩個條件 } ```