DEV Community

Eulis
Eulis

Posted on

Importance of DRY & KISS For Software Developer

  • In my previous blog post abstraction helpers in rails you might have seen the word DRY, you might have looked it up and might know what it means or not, but not worries Here I would do my best to give you a quick explanation like if it was for a 5 years old kid.

DRY (Don't Repeat Yourself)

  • DRY mean Don't Repeat Yourself, are set of principle that we are to adopt as a developer. At it Core, the Purpose of Keeping it DRY is to reduce the amount of repetition of information. As stated in the DRY principle, --"Every piece of knowledge or logic must have a single, unambiguous, representation within a system".

  • Main Reason Why it's important to keep it dry is because if there is a problem with the code, and you need to make a change, you do not have to change the same code in multiples places. Whenever developing software with a mindset of keeping it KISS and DRY.

  • This a good Way to keep dry, try to divide your codebase into blocks or pieces of code and logic to be as reusable as possible, so we can call them where we need them.

Below is an Example of a SessionsHelper module, The methods listed below are to be used in different places through the App.

module SessionsHelper
    # Logs In The given user.
    def log_in(user)
        session[:user_id] = user.id
    end
    # Returns the current logged-in user (if any).
    def current_user
        @current_user ||= User.find_by(id: session[:user_id])
    end
    # Returns true if the user is logged in, false otherwise.
    def logged_in?
        !current_user.nil?
    end
    # Log Out the current user.
    def log_out
        session.delete(:user_id)
        @current_user = nil
    end
    # Redirect to Root if not logged in.
    def redirect_if_not_logged_in
        redirect_to root_path if !logged_in?
    end
end
Enter fullscreen mode Exit fullscreen mode
  • Any change made to this method will be kept in sync and update automatically through the application.

  • Allow us to write less code, save time reduce bugs and make it easier to maintain the codebase.

Checkout This Blog On Ruby on Rails Abstraction for another perspective

KISS (Keep It Simple, Stupid)

  • To Keep it short the KISS Principle at it core aim to keep the clean and simpler, thus making it easy to consume and understand.

  • Remember Programming language are made for us humans to understand, hence computer only knows Binary or 1 and 0 110011011. So with this in mind we need to keep the code simple and straightforward, keeping all the methods small and concise, Somewhere I've Read that we should aim to keep Each method around 40-50 lines of codes.

  • To put it into even more simple terms, we need to make code readable to others humans, so we must use descriptive names for methods, variable, function, etc. this make sure us or someone else looking your repo on GitHub can understand and read your code in the future.

  • You can perform a Quick Search about DRY or Kiss To Get more in-depth article related to this area.

  • NOTE: This is one of 30 blogs that I would be attempting to write over the next 30 days to improve my writing skills.

Top comments (0)