KISS Principle

KISS stands for Keep It Simple, Stupid —the fundamental idea behind the KISS principle in software development.

From the developers’ perspective, complex coding leads to various risks and complications. It not only means wasted efforts but also makes it extremely difficult to implement changes.

When writing methods, it would be always good to follow the below guidelines,

  1. Keep your methods small,
  2. Each method should never be quite 40-50 lines.
  3. Each method should only solve one small problem, not many use cases.
  4. If there are tons of conditions within the method, break these out into smaller methods. This will ease the developers to read and maintain.
  5. Find bugs faster.

Just have a look at the codes shown below. Both methods are doing the same thing, but which one would you use?

class Program {
    static void Main(string[] args) {
        var saturday = DisplayWeekDayByNumber_WithSwitch(6);
        var sunday = DisplayWeekDayByNumber_WithArray(7);
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="day"></param>
    /// <returns></returns>
    private static string DisplayWeekDayByNumber_WithSwitch(int day) {
        var _day = string.Empty;
        switch (day) {
            case 1:
                _day = "Monday";
                break;
            case 2:
                _day = "Tuesday";
                break;
            case 3:
                _day = "Wednesday";
                break;
            case 4:
                _day = "Thursday";
                break;
            case 5:
                _day = "Friday";
                break;
            case 6:
                _day = "Saturday";
                break;
            case 7:
                _day = "Sunday";
                break;
            default:
                throw new InvalidOperationException("day must be in range 1 to 7");
        }
        return _day;
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="day"></param>
    /// <returns></returns>
    private static string DisplayWeekDayByNumber_WithArray(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];
    }
}

Different forms of KISS

The KISS principle offers two other forms – for those who are fragile to use the word “stupid”

  • Keep it short and simple
  • Keep it simple and straightforward

Thank you for reading my article. Please leave your comments in the comment box below.


Next Recommended Reading SOLID Principles