SOLID – Open/Closed Principle

I liked this explanation of Open/Closed principle from SOLID
https://adevait.com/software/solid-design-principles-the-guide-to-becoming-better-developers

Open/Closed Principle

Open/Closed Principle is the most confusing one of the 5 principles when you judge by the name. How can something be open and closed at the same time? However, it has a fairly simple idea behind it and it’s the one that can save you the most development time if you apply it correctly.

The Open/Closed Principle states that a module should be open for extension, but closed for modification. That means you should be able to extend a module with new features not by changing its source code, but by adding new code instead. The goal is to keep working, tested code intact, so over time, it becomes bug resistant.

Applying the Open/Closed Principle is like working with an open source library. When you install an open source package in your project, you don’t edit its code. Instead, you depend on its behavior but you write your code around it. At one point, the library gets tested so much that it becomes very stable. This is the approach you should take with your own code: add new features as new pieces of code that cannot break what’s already working.

How to make sure your code follows the Open/Closed Principle?

Let’s say you need to implement an e-commerce module with multiple payment options. You can create a class Pay and add different payment options as separate methods. This would work, but it would result in changing the class whenever we want to add or change a payment option.

Instead, try creating a PayableInterface that will guide the implementation of each payment option. Then, for every payment option, you will have a separate class that implements that interface. Now the core of your system is closed for modification (you don’t need to change it every time you add new payment option), but open for extension (you can add new payment options by adding new classes).

And it finally clicked for me, the above example:

You can achieve Open/Closed principle by extracting an interface over your behavior.
The interface will specify the what  a needs to be done, and by doing this your code is closed for modifications, I mean… you rarely change the rules once you have extracted them. And having classes implementing the interface, the concrete implementation contains the how it is done.
So you can add different implementations – and thus you achieve the open for extension

Here is another great link on the subject
https://khalilstemmler.com/articles/solid-principles/solid-typescript/#OCP

TDD – great practical ideas

I have found this great excerpt about TDD, Original articles:
https://khalilstemmler.com/articles/software-professionalism/developer-principles/

TDD (Test-Driven Development)

One of the most controversial trends to hit the industry has been TDD. It’s worked for some, and failed for many others. But the reason I think it fails is because sometimes we try to treat TDD as law.

Fallacy of a strict Red-Green-Refactor Loop

In TDD, there’s a concept of a Red-Green-Refactor loop. It goes like this.

Red: Write a failing test

Green: Write the code to make the test pass

Refactor: Refactor the code when needed in order to improve the design.

Don’t treat this as a dogma. You don’t have to execute TDD in this order to feel like you’re doing it properly.

That’s not important. You don’t have to stick to the loop by the letter.

What’s important is that you write the tests while you code (or before you code) in order to reap the benefits of TDD.

The primary benefit of doing this is that you know right away if your code is testable or not. And if it’s not, you can take action right then and there before you pour some concrete that is going to be hard for yourself and anyone else to change in the future.

Knowing how to write testable code can be taught. That’s one of the benefits of the SOLID principles, and it aims to address that.

Sockets, Threads, Connection Pool

Today one of our applications built on spring boot, has restarted itself ?!

2019-06-06 07:12:13.343 WARN 28148 --- [-AdminTaskTimer] c.m.v.a.ThreadPoolAsynchronousRunner : com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@2977f7fe -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!

We suspect that due to DB Connection Pool missconfiguration, it somehow run out of threads… threads pilled up waiting and the java process crashed…

So, here are some references related to this story

https://dzone.com/articles/threads-stuck-in-javanetsocketinputstreamsocketrea

What does SocketInputStream.socketRead0() API in Java do?


– I liked this article because it shows that DB Connections, HTTP Calls, RMI invocations… boiled down to Socket connection and a thread might be block/waiting on that connection

MySQL Connection Handling and Scaling


– this is such a nice article explaining the mysql connections, etc…