Answer the question
In order to leave comments, you need to log in
What is the correct decomposition of javascript code?
Good evening!
I came across the topic of unit testing and realized that my code is not as good as it should be. Dependencies, a lot of code in one component / class, not all methods are built in a functional style (input / output) - all this made me understand that I need to rewrite my code and straighten my arms.
But the question arises: what should be taken out into separate modules, and what should not? If everything has been clear with view components for a long time, then here's how to break the logic - not completely. Take a class/component as an example: which methods should be left inside, and which should be put into separate modules, so that the necessary modules can be imported inside this class/component and used inside? I hope for a sincere answer and for advice that will help when writing broken testable code. Thank you!
Answer the question
In order to leave comments, you need to log in
Learn the principles of SOLID by heart. Seriously. Unit tests will go like clockwork. Find out more about separation of concerns - this is also about breaking down into modules. In general, if you make decisions guided by the rules of SOLID and constantly think about the separation of concerns, then things will get better. Just don't expect a quick insight, please. You will have to purposefully practice this for a year or two. I'm speaking from personal experience, but to be honest, I'm a bit dumb. Maybe you have a chance to master this case faster.
You can also get acquainted with what temporal coupling is. And in general coupling. It's about how not to do it.
You can also learn the mantra "low in coupling and high in cohesion" by heart.
You can also try to write tests first, and then code. But only those who are able to overcome pain for a long time can do this. But then you don't want to do it any other way. Yes, and it doesn't work.
UPD
I forgot to say, try to write such code that you are not ashamed to post in npm. If you manage to write a module that can be connected to any project via npm, then this is a good sign that the module was written according to all the rules. And just do open source greatly helps in pumping "modular thinking".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question