martes, 1 de marzo de 2016

The SOLID principles

Today I will talk about the SOLID principles and the article "Understanding the SOLID principles" by the book“Ace the Programming Interview: 160 Questions and Answers for Success”.

The song of today's blog will be "Across the overspass" by "The Solids".

The SOLID principles talk mostly about how to do clean code in object oriented programming.

"S" stands for "single responsability", which means that classes should have the logic to handle a specific task or set of tasks, it can be summarized as "if class A handles A and B, you should delegate B to a class B". The reason for this is simple, if you have to modify something related to activity A, it is great to open class A and see only the code that handles it. 

"O" stands for open/closed principle, which means that classes should be designed in a way that they can be extended, but not modified. The reason behind this is that if you have to modify a class, you will most likely have problems on other classes that use the class. If you need to add some functionality to the class add it, but don't try to fix something that isn't broken, or you'll end up redesigning the whole house just because you needed to add a lamp.

"L" stands for "Liskov sustitution", which basically means that if class B and class C inherit from class A, the methods for class B and class C should be interchangeable, that of course limits the power of inheritance, but also brings peace between child classes, if a class should implement some funky chunk of code, it is always better that it gets it by composition rather tan inheritance, or trust me, things will become messy.

"I" stands for "Interface segregation" which is sort of a variation of "S" applied to interfaces, it basically means that instead of having an interface for a big chunk of logic, you apply "divide and conquer" and make every interface define a specific behaviour, and let classes choose to implement the number of interfaces that handle the logic each class needs. Sounds simple.

"D" stands for... "Dependency inversion" (what else did you think?) which means that code should have minimal dependency, so instead of having a class A that has an object of class B that has a set of behaviours you define an interface B and a class B and BAlternative, then, on class A you can just define a method that calls the logic of Interface B (which can come from class B or class BAlternative). That way, the behaviour of class A and class B isn't coupled.

Object oriented programming is extremely powerful for some programming problems, however, it has to follow design principles to void messy code, a problem on object oriented programming is that if you do not design well your solution, everything depends on everything, and a minor bug on class A can cause the whole thing to crash, then becoming the debugger's nightmare (where each time you clean a bug you uncover other 10 bugs on other classes, recursively, until you get a debuggerOverflow exception and everything goes bad).

Thats all for now, wololo.

No hay comentarios:

Publicar un comentario