You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从KISS原则考量,两个getWeekDay函数实现方案哪个更适用?

Which getWeekDay Implementation Follows the KISS Principle Better?

Great question! Let's break down these two Java implementations through the lens of the KISS (Keep It Simple, Stupid) principle—all about prioritizing simplicity, readability, and minimal redundancy.

The Two Implementations

Switch Statement Version

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"); 
    } 
}

Array-Based Version

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 Principle Analysis

First, let's be clear: both implementations are functional and correct—they do exactly what's required. But KISS is about more than just working; it's about keeping things as simple and maintainable as possible.

Switch Statement Pros & Cons

  • Pros: It's super explicit—you can immediately see which integer maps to which weekday without any extra mental work.
  • Cons: It's repetitive. Every case has its own return statement, and if you ever needed to tweak a weekday's name (or hypothetically reorder them), you'd have to edit each individual case block instead of a single source of truth. This redundancy adds unnecessary lines of code and raises the risk of typos if you ever need to make changes.

Array-Based Version Pros & Cons

  • Pros:
    • No redundancy: All weekday labels live in one array, so updates or tweaks only require a single change.
    • Clean, linear flow: The validation check is upfront, followed by a straightforward lookup. The day - 1 logic is intuitive once you realize we're converting a 1-based input to a 0-based array index—this is a common pattern most developers will grasp instantly.
    • Concise but readable: The whole function is shorter than the switch version without sacrificing clarity.
  • Cons: The only minor "gotcha" is the day - 1 adjustment, but this is such a standard trick that it barely counts as a downside.

Final Verdict

The array-based version is the clear winner for following the KISS principle. It eliminates redundant code, centralizes the weekday mapping, and stays fully readable—all while doing the exact same job as the switch version. It's simpler to write, simpler to maintain, and avoids unnecessary repetition.

内容的提问来源于stack exchange,提问作者Pathis Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 13:22:33