Skip to main content

Week 10 [17.12-23.12.18] Is your code STUPID or SOLID?


Is your code STUPID or SOLID?

Source: https://blogs.msdn.microsoft.com/cdndevs/2009/07/15/the-solid-principles-explained-with-motivational-posters/
These are just acronyms that describe certain errors and principles in writing a code. From the name, it can be understood that the first approach is bad and should be avoided. At the same time, the second one is correct and experienced programmers stick to it. But what are they?

I propose to begin with determining the acronyms.

Firstly, we have STUPID:
       S - Singleton
       T - Tight Coupling
       U - Untestability
       P - Premature Optimization
       I - Indescriptive Naming
       D - Duplication

So here is SOLID:
       S - Single Responsibility
       O - Open / Closed
       L - Liskov Substitution
       I - Interface Segregation
       D - Dependency Inversion

You will often have questions about the SOLID during the recruitment. Well, if you are asked what a STUPID or SOLID is, then you can already answer it or at least determine it. But it is worth delving into each point of each approach in order to understand what it is and why it is worth being a SOLID developer, not a STUPID one.

Let's go in order, namely, let's start with the STUPID points.

Singleton

A singleton is a design pattern that guarantees that there is only one single instance of a class in an entire application. Of course, in certain cases, it can be convenient, but singletons often make testing difficult, and with illiterate use, they can even lead to memory leaks.

Tight coupling

Strong dependencies are evil that we should avoid. Initializing the dependencies in the constructor or immediately in the variable leads to closing the possibility of easy substitution of objects. A good example is the data source. For example, usually the application uses data from the network, but in tests, we want to simulate this source locally. The solution to this problem is the Dependency Inversion described in one of the SOLID principles.

Untestability

Everything is simple – a badly written code is always difficult to test. This is partly due to improperly created dependencies, as it was described in the point above.

Premature Optimization

Premature optimization, as a rule, complicates code readability. Therefore, you should avoid this.

Indescriptive naming

Everything is also clear here - think up readable and understandable names for classes, methods, and variables so that you will not waste your time trying to figure out what it is for.

Duplication

Many programmers forget about code reuse and they duplicate parts of it, sometimes even classes. One should always try to use the already existing methods and, if possible, make genetic classes.

Here we have just understood the bad approach - STUPID. Let's now take a look at the SOLID.

Single Responsibility Principle

Source: https://blogs.msdn.microsoft.com/cdndevs/2009/07/15/the-solid-principles-explained-with-motivational-posters/
This principle suggests that each class should be responsible for one specific functionality only. Thanks to this approach, we have the opportunity to make changes to the class, without fear of the impact of changes on other objects.

Open / Closed Principle

Source: https://blogs.msdn.microsoft.com/cdndevs/2009/07/15/the-solid-principles-explained-with-motivational-posters/
The principle implies that objects should be open for extension, but closed for changes. This means that the developer must organize the class in such a way that in order to adjust it to specific conditions, it will be enough just to extend the class and override some functions.

Liskov Substitution Principle

Source: https://blogs.msdn.microsoft.com/cdndevs/2009/07/15/the-solid-principles-explained-with-motivational-posters/
Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. That’s how this principle is described. This suggests that if the developer decides to extend the class, it will not disrupt the work of the application. For example, if the developer overrides the method from the base class with errors, then only some part of the application will be affected.

Interface Segregation Principle

Source: https://blogs.msdn.microsoft.com/cdndevs/2009/07/15/the-solid-principles-explained-with-motivational-posters/
Have you encountered situations when you need to implement an interface in order to use only a few of its methods? I think yes. In this case, we are forced to implement unnecessary methods. Of course, a pair of empty methods will not greatly affect the readability of the class. But what if the interface has, for example, 10 methods, and we need only 1-2? Actually, this principle describes the solution to this problem. He says that it is necessary to avoid thick interfaces by dividing them into smaller ones.

Dependency Inversion Principle

Source: https://blogs.msdn.microsoft.com/cdndevs/2009/07/15/the-solid-principles-explained-with-motivational-posters/
This principle serves to create loosely coupled entities, what greatly simplifies testing and modifying the already existing code. The description says that top-level modules should not depend on lower-level modules, but they should depend on their abstractions. Abstractions are independent of the details, while the details must depend on the abstractions. This means that we should not create instances of objects inside a class, we must pass them through constructs.

Conclusion

From my personal experience, I can say that using SOLID in a project is good and necessary practice. Yes, at first it may seem that this is unnecessary, especially when a project is small. But when it comes to testing and extending, problems start to appear and the programmer needs to do refactoring of the code, rewriting old code fragments, thereby losing his time. When I first started programming, I neglected these principles, which soon sometimes led to almost impossible code content.

Sources

Questions

       Do you use any of SOLID principles?
       Do you have any of the mistakes mentioned in the STUPID approach?
       What can you suggest we should do to make the code more maintainable?

Comments

Unknown said…
I always try to follow SOLID principles, but I do startups, and sometimes it's tough to be satisfied with your code because of the rush and pressure from the client. As you mentioned in the article, I used to have the same pessimistic approach to this topic at the beginning, but with some experience, I have learned that it's useful and saves many hours on the long term.

Tight Coupling. I don't write many tests because of my job specificity, so I sometimes it takes lots of time when client change his mind, and we have to refactor tons of code to support testing.

We should encourage developers to read more about different design patterns and teach them how to do a proper code review. Writing good code is crucial for the success of the product. Having lots of tests is a handy thing thinking of maintainable. A simple way to check if you write a decent code is to try to go back to projects you wrote +2 months ago an try to add something to them. If it's easy, it means that you have written a decent code.
Artem Lipovatyi said…
Yeah, problems with clients are well-known among programmers. How often do you have problems with your clients? Would you try to explain them what kind of problems can appear in case of bad written code?

Popular posts from this blog

Week 1 (09-15.03) VOD

http://www.vod-consulting.net/wp-content/uploads/2012/08/1.jpg

Week 11 [03-09.06.2019] The problem with ecological cars emission in UK

The problem with ecological cars emission in UK Since the adoption of the European Emission Allowance Directive in the European Parliament, all car makers have tried to submit. Since 1992, the Euro I standard has been in force, which limited the emission of carbon monoxide to the atmosphere. The Euro VI standard currently applies, which limits the series of exhaust gases. These include: hydrocarbons, nitrogen and carbon oxides, and dust.   The most significant change was brought by the Euro IV standard. For the first time it introduced the limitation of nitrogen oxides, which are responsible for the harmful compounds of smog.   What is smog?   Smog consists of sulfur oxides, nitrogen and carbon. In addition, solid substances such as suspended dust (PM). Dust suspend in atmospheric aerosols may be in liquid and solid form. These can be particles of sea salt, clouds from the Sahara and artificial compounds made by people. These compounds often come fr

Week 4 [06-12.11.2017] This is what happens when you reply to spam email.

James Veitch is a British comedian. In today’s Ted Talk James with characteristic for himself a sense of humor shows how he deals with spam emails and why responding to junk messages may be sometimes dangerous. Questions: What do you think about James’s  way of dealing with spam? Why are junk messages legal, even though it sometimes may be a fraud? Dou you have a problem with spam? How do you deal with with it?