jueves, 23 de febrero de 2017

Understanding the SOLID Principles

First of all, SOLID is an acronym for the it's five principles hidden in each letter of object-oriented programming (Java, C#, PHP, Python, Ruby, etc.) and design. It's objective is to avoid dependencies, because it may cause problems on the code if there are, so the principles are:
  • Single Responsibility Principle (SRP). Basically as the name said the method should have only one responsibility, because the consequences would be at the moment of maintenance, when other programmer change the code and if the class do two things, but the programmer did not check out, the functionality would be compromised.
  • Open / Closed Principle (OCP). It states that a class or function should be open for extension and closed for modification. Meaning that the base classes (functions) should be leave alone, when adding a new behavior and create new inheriting classes. It's about inheritance.
  • Liskov Substitution Principle (LSP). This principle is like OCP and has the intention to keep happy relationships between classes and functions to work well. Meaning that as the previous principle said, avoid modifying the base class with the difference that LSP do not use the inheritance mechanism (because it would be a violation to the principle), instead of that, create an instance of that class. In other words, avoid inheritance by using keywords according the language, for example in Java you can use final keyword an in C# sealed keyword.
  • Interface Segregation Principle (ISP). This principle is about interfaces and avoiding creating too many responsibilities in one interface, making the code less understandable, it's kind of SRP but with interfaces and with the main difference that you should partition interfaces according it's concerns, making the code more readable for other programmers.
  • Dependency Inversion Principle (DIP). "Depend upon abstractions, not upon concretions" Meaning that you shouldn't depend of another class, instead it's better depend from an interface, becoming easier to substitute any class you want.
From my point of view , already having understood the principles gave me a new way of code, because sometime we do not follow a standard when coding, but we may ask Why this is important? Dependencies are always there, but the less we avoid the easier it's maintenance will be.
Vatican City, Rome
For more information visit Understanding the SOLID Principles

No hay comentarios:

Publicar un comentario