哪种方式在KISS原则中更有可能被使用?

huangapple go评论96阅读模式
英文:

Which is more likely to be used in terms of KISS?

问题

我有两个函数。第一个:

public String getWeekDay(int day) {
    switch (day) {
        case 1:
            return "Monday";
        case 2:
            return "Tuesday";
        case 3:
            return "Wednesday";
        case 4:
            return "Thursday";
        case 5:
            return "Friday";
        case 6:
            return "Saturday";
        case 7:
            return "Sunday";
        default:
            throw new InvalidOperationException("day must be in range 1 to 7");
    }
}

第二个:

public String getWeekDay(int day) {
    if ((day < 1) || (day > 7)) throw new InvalidOperationException("day must be in range 1 to 7");
    String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    return days[day - 1];
}

我认为这两者都很简单,且完成相同的功能,但在遵循KISS原则方面,哪个更好呢?

英文:

I have two functions. The first one:

public String getWeekDay(int day) {
    switch (day) {
        case 1:
            return &quot;Monday&quot;;
        case 2:
            return &quot;Tuesday&quot;;
        case 3:
            return &quot;Wednesday&quot;;
        case 4:
            return &quot;Thursday&quot;;
        case 5:
            return &quot;Friday&quot;;
        case 6:
            return &quot;Saturday&quot;;
        case 7:
            return &quot;Sunday&quot;;
        default:
            throw new InvalidOperationException(&quot;day must be in range 1 to 7&quot;);
    }
}

And the second one:

public String getWeekDay(int day) {
    if ((day &lt; 1) || (day &gt; 7)) throw new InvalidOperationException(&quot;day must be in range 1 to 7&quot;);
    String[] days = {&quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;, &quot;Sunday&quot;};
    return days[day - 1];
}

In my opinion both are simple and do the same thing but which is better to be used when it comes to KISS principle?

答案1

得分: 5

以上都不是,你将会使用 enum 来处理这种情况,而且在Java 8中已经存在这些内容。

链接:https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html

英文:

None of the above, you would use an enum for that and these already exist within Java 8.

https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html

答案2

得分: 2

你应该考虑使用枚举(Enum),我认为它更适合你的解决方案。

enum WeekDays {
    MONDAY(1), TUESDAY(2),  // 填充其它的天...

    private int day;

    WeekDays(int day) {
        this.day = day;
    }

    public int getDay() {
        return this.day;
    }
}

然后检查你的日期是否存在于枚举中,如果不存在则抛出异常,类似于以下代码:

try {
    int dayToBeChecked = // 你的日期值;
    boolean exists = false;

    for (WeekDays days : WeekDays.values()) {
        if (days.getDay() == dayToBeChecked) {
            exists = true;
            // 在这里处理逻辑
            break;
        }
    }

    if (!exists) {
        throw new IllegalArgumentException("指定的日期在枚举中不存在");
    }

} catch (IllegalArgumentException e) {
    // 处理异常
}
英文:

You should consider using an Enum, I think it fits more to your solution

enum  WeekDays
{
    MONDAY(1), TUESDAY(2),  //fill the rest days....

    private int day; 

    public String getDay() { 
        return this.day; 
    } 
}

And then check if your day exists in enum otherwise throw the exception, something like the below code

  for (WeekDays days : WeekDays.values()) {
        if (days.getDay() == dayToBeChecked){
            // your logic here
        }
    }

答案3

得分: 2

第二个更合适。对于最新的Java版本,switch表达式也是不错的选择,因为它更紧凑。

然而,最好在英语区域设置中使用Java时间API。

/**
 * 返回星期几。
 * @param day 1=星期一,直到7=星期日(ISO标准)。
 */
public String getWeekDay(int day) {
    return DateTimeFormatter.ofPattern("EEEE", Locale.US).format(DayOfWeek.of(day));
}
英文:

The second is more apt. For the newest java the switch expression would be nice too as it is more compact.

However better use the java time API for an English Locale.

/**
 * Return the weekday.
 * @param day 1=Monday till 7=Sunday (ISO standard).
 */
public String getWeekDay(int day) {
    return DateTimeFormatter.ofPattern(&quot;EEEE&quot;, Locale.US).format(DayOfWeek.of(day))
}

huangapple
  • 本文由 发表于 2020年1月30日 19:58:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59985555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定