簡體   English   中英

如何避免使用過多的 if 語句/重構此方法?

[英]How can I avoid using too much if statements / refactor this method?

它看起來很可怕,但我不明白我該如何分解它?
我想過創建小的 boolean 方法,但我認為它不會改變太多,總會有很多 ifs?

private String getFolderValue(TableRow row) {
        String cote = row.getCellValue("B");
        String typologie = row.getCellValue("G");
        String description = row.getCellValue("Q");
        if (cote.startsWith("DE")) {
            return "Dessins";
        }
        if (cote.startsWith("PH")){
            return "Photographies";
        }
        if(cote.startsWith("CA")) {
            return "Catalogues";
        }
        if(cote.startsWith("PU") && typologie.contains("affiche")){
            return "Publicité###Affiches";
        }

        if(cote.startsWith("PU") && typologie.contains("flyer")){
            return "Publicité###Flyers";
        }

        if(cote.startsWith("PU") && description.contains("presse")){
            return "Publicité###Presse";
        }

        if(cote.startsWith("PU") && (description.contains("Facture") || description.contains("devis"))){
            return "Documents###Vente";
        }

        if(typologie.contains("Emballage")){
            return "Visual Merchandising###Flyers";
        }

        if(typologie.contains("PLV")){
            return "Visual Merchandising###PLV";
        }
        if(description.contains("Correspondances")){
            return "Documents###Correspondances";
        }

        return null;

    }

使用 hashmap 存儲數據並檢索數據。

一般來說,一種稱為 責任鏈的設計模式可以幫助降低在您的代碼需要處理大量案例時出現的問題所帶來的復雜性。

簡而言之:而不是許多 if 語句,您將鏈接許多 java 對象(“接收器”)。 每個人都會檢查是否:

  • 他們對當時的情況負責
  • 返回他們的結果。

如果他們不負責,他們會將處理傳遞給下一個接收者(即他們的繼任者)。

最終,該鏈應包含至少一個負責提供答案的接收者。

每個接收器/處理程序只包含一個 if 語句。

所以這個模式的作用,基本上,是將復雜性划分為多個類。

用 UML 描述的責任鏈

圖片由Vanderjoe 拍攝——許可證: CC BY-SA 4.0

好的,首先讓我們注意到你的一個條件被重復了很多次。 讓我們嘗試使用嵌套的“if”並使用 StreamAPI。

    private String getFolderValue(TableRow row) {
        String cote = row.getCellValue("B");
        String typologie = row.getCellValue("G");
        String description = row.getCellValue("Q");
        if (cote.startsWith("DE")) {
            return "Dessins";
        }
        if (cote.startsWith("PH")) {
            return "Photographies";
        }
        if (cote.startsWith("CA")) {
            return "Catalogues";
        }
        if (cote.startsWith("PU")) {
            final var topologyContains = Stream.of("flyer", "presse", "affiche").anyMatch(typologie::contains);
            if (topologyContains) {
                return "Publicité###Affiches";
            }

            final var descriptionContains = Stream.of("Facture", "devic").anyMatch(description::contains);
            if (descriptionContains) {
                return "Documents###Vente";

            }
        }
        
        if (typologie.contains("Emballage")) {
            return "Visual Merchandising###Flyers";
        }

        if (typologie.contains("PLV")) {
            return "Visual Merchandising###PLV";
        }
        if (description.contains("Correspondances")) {
            return "Documents###Correspondances";
        }
        return null;
    }

我不會在這里過多地進行重構,因為這是一個簡單的策略模式,並且在某些情況下,它越“通用”並且喜歡你 go,最終它就越糟糕,因為它必須易於閱讀。

編輯:讓我們分成功能。 你需要測試它,因為我很着急,但你應該明白要點:

   private String getFolderValue(TableRow row) {
        String cote = row.getCellValue("B");
        String typologie = row.getCellValue("G");
        String description = row.getCellValue("Q");

        return Stream.of(
            parseFromCote(cote),
            parseFromCotePU(cote, typologie, description),
            parseFromTypologie(typologie),
            parseFromDescription(description)
          )
          .filter(Objects::nonNull)
          .findFirst()
          .orElse(null);
    }

    private String parseFromCote(String cote) {
        if (cote.startsWith("DE")) {
            return "Dessins";
        }
        if (cote.startsWith("PH")) {
            return "Photographies";
        }
        if (cote.startsWith("CA")) {
            return "Catalogues";
        }
        return null;
    }

    private String parseFromCotePU(String cote, String typologie, String description) {
        if (cote.startsWith("PU")) {
            final var topologyContains = Stream.of("flyer", "presse", "affiche").anyMatch(typologie::contains);
            if (topologyContains) {
                return "Publicité###Affiches";
            }

            final var descriptionContains = Stream.of("Facture", "devic").anyMatch(description::contains);
            if (descriptionContains) {
                return "Documents###Vente";

            }
        }
        return null;
    }

    private String parseFromTypologie(String typologie) {
        if (typologie.contains("Emballage")) {
            return "Visual Merchandising###Flyers";
        }

        if (typologie.contains("PLV")) {
            return "Visual Merchandising###PLV";
        }
        return null;
    }

    private String parseFromDescription(String description) {
        if (description.contains("Correspondances")) {
            return "Documents###Correspondances";
        }
        return null;
    }

如果您使用的是 Java8,請將 final var 替換為正確的類型。 如果你喜歡懶惰地做,你需要在那里傳遞對功能接口的引用,但我認為即使是“急切”的評估看起來也不錯;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM