使用Switch,减少圈复杂度java [英] Struck with Switch, reducing cyclomatic complexity java

查看:629
本文介绍了使用Switch,减少圈复杂度java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用java时我遇到了一个奇怪的情况,让我们先从代码开始。

while working with java i was stuck in a weird situation, lets start with code first.

public static String constructMessage(final String reason, final String xId,
        final String yId, final IonStruct metadataStruct) throws DataSanityException {
    String message = "";
        switch (reason) {
            case "NOT_ACTIVE":
                if (xId != null) {
                    message = String.format("Y %s X %s not active: status is inactive%n", yId, xId);
                } else {
                    message = String.format("Y %s not active: status is inactive%n", yId);
                }
                break;
            case "PRICE_GONE":
                if (metadataStruct.containsKey("applied")) {
                    final String applied = metadataStruct.get("applied");
                    if (applied.equals("single")) {
                        message = String.format("X %s is not active. price for the X is gone. "
                                + "Y price %s for an X should not be gone by %s.%n", xId,
                                metadataStruct.get("yPrice"), metadataStruct.get("zPrice"));
                    } else {
                        if (metadataStruct.get("gonePeriod").equals("gonegone")) {
                            message = String.format("Y price is %s whereas the gone price is %s."
                                    + " Y price for an X should be at least %s%% lower than gone.%n",
                                    metadataStruct.get(yPrice), metadataStruct.get(zPrice), metadataStruct.get("xyzPrice"));
                        } else {
                            message = String.format("Y price %s for an X should be at least %s%% lower than gone price %s in last %s days.%n",
                                    metadataStruct.get(yPrice), metadataStruct.get("mpqPrice"), metadataStruct.get(lpzPrice),
                                    metadataStruct.get("numberOfDays"));
                        }
                    }
                } else {
                    message = String.format(
                            "X %s in Y %s is not active. is %s%%, min mpux is %s%%. ", xId,
                            yId, metadataStruct.get("lpux"), metadataStruct.get("mpxPrice"));
                }
                break;

         and so on ----------- around 20 cases.
         default:
                message = "";
                break;
}

在整个交换机中有20个这样的情况,具有不同的消息和不同的变量用于每个案例。
在一些开关中ifs的深度也达到3。

there are 20 cases like this in the complete switch with different messages and different variables used in each of the cases. And the depth of ifs is also upto 3 in some of the switches.

因此圈复杂度将达到29(太高),所以我想要重构具有低圈复杂度和更好可读性的代码。

So the cyclomatic complexity is coming 29(too high), so i want to refactor the code with low cyclomatic complexity and better readability.

任何建议?

推荐答案

来自Martin Fowler的重构书

From Martin Fowler's Refactoring book

我可以建议以下重构方法:

I can suggest following refactoring method:

import java.util.HashMap;
import java.util.Map;

public class Refactoring {

    private static class IonStruct {};

    private static interface MessageBuilder {
        String  build(final String xId,final String yId, final IonStruct metadataStruct);
    }


    private static class NotActiveMessageBuilder implements MessageBuilder {

        private static final String YX_NON_ACTIVE_FMT = "Y %s X %s not active: status is inactive%n;";
        private static final String Y_NON_ACTIVE_FMT = "Y %s not active: status is inactive%n";

        @Override
        public String build(String xId, String yId, IonStruct metadataStruct) {
            return (xId != null) ? String.format(YX_NON_ACTIVE_FMT, yId, xId) : String.format(Y_NON_ACTIVE_FMT, yId) ;
        }

    }

    private static enum MessageTypeChine
    {
        instance();
        private final Map<String,MessageBuilder> builders;
        private MessageTypeChine() {
            this.builders = new HashMap<>();
            this.builders.put("NOT_ACTIVE", new NotActiveMessageBuilder());
        }

        public String constructMessage(final String reason, final String xId,
                final String yId, final IonStruct metadataStruct) {
            MessageBuilder builder = this.builders.get(reason);
            if(null != builder)
                return builder.build(xId, yId, metadataStruct);

            throw new UnsupportedOperationException("Message type is not supported");
        }
    }


    public static String constructMessage(final String reason, final String xId,
            final String yId, final IonStruct metadataStruct) {
        return MessageTypeChine.instance.constructMessage(reason, xId, yId, metadataStruct);
    }

}

这篇关于使用Switch,减少圈复杂度java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆