Software Design Principles

           Many times I came across the term "Design Principles" in many discussions, interviews & many more places. Today I finally decided to write all this collectively at one place with intention of helping out people like me. Till Date i came across so many abbreviations & their different full forms. Here i am gonna present the extract of what i learnt till date.

           Most basic terms are DRY, KISS, SOLID & GRASP. These all (terms or suggestions or whatever you call it) make your code better. There is another term "STUPID" which your code should not be. Lets have a closer look to all these terms -

DRY - Don't Repeat Yourself
         Never repeat code, break your code in functions, make as many calls to the functions.

KISS - Keep It Simple Stupid !
         While our attempts of applying our enormous knowledge on OO Design, most of the times we end up making code way too complex for others to understand. Try to save yourself from this, keep asking my code still very simple??

SOLID - It is a combination of 5 different terms -

Single responsibility -
         One code snippet (Ideally one function) should perform one & only one job. One class should have one & only one group of functionalities, clearly divide all operations to groups. One module should not intermingle with other ones unless it becomes a "Show Stopper".

Open-closed -
         Be open for extensions & at the same time, be closed for modifications.

Liskov substitution -
        This is an extension to open closed principle. newly created extensions/subtypes should be directly replaceable in system without any alteration of already existing "working" code.

Interface segregation -
        Try not to have large interfaces. Lets say we are creating an interface for workers, then have a "takeLunchBreak()" method won't be applicable for Robots (a type of worker). have your interfaces splitted.

Dependency inversion -
        Always depend upon the abstractions, the interfaces, direct dependency on concrete classes ends re-usability of your higher level code/module.

GRASP General Responsibility Assignment Software Principles
          It is actually a set of patterns, few of which are based on many other principles & design patterns. The principles are -
  • Information Expert
  • Creator
  • Controller
  • Loose Coupling
  • High Cohesion
  • Polymorphism
  • Protected Variations
  • Indirection
  • Pure Fabrication

        The word itself is offending, we should always try NOT to have the following appearing in our code at any point of time. having them is always a mess. Lets see what STUPIDity we should not be doing -
  • Singleton
  • Tight coupling
  • Un-testability
  • Premature Optimization
  • Indescriptive Naming
  • Duplication

There is a lot more in this mighty world to learn. Never feel that this is it !!!

No comments:

Post a Comment