Answer the question
In order to leave comments, you need to log in
How to split the code?
I am writing a library in native es6 without dependencies, I need to somehow divide the code into logical "modules".
There are several functions that work with the hash, there are several functions that work with events, scatter them around the house. The problem is the stringing of such code. Now 200 lines of code have been written, and you are already starting to get confused in it, since the functions are mixed up, and not in the order of execution, and it’s not realistic to put them in order, since some functions are independent of each other.
For example, the functions responsible for hashing and scattering events around the house are independent of each other. But in the end they refer to the same function.
Tell me how such code will be structured more correctly. The problem is that if you take it out, it's all in separate files, you will have to connect quite a lot.
I thought it was to be packaged in some big functions, with functions nested inside, in which there will already be executable code.
And another question. I believe that arguments and all variables should be prefixed with some kind of prefix to make it easier to understand where the data comes from.
My variable or argument can come from another function inside the library, be passed by the programmer, or, it was generally taken from the user, for example hash. Inside the library, this needs to be somehow divided in order to understand where in the function this data came from.
People who wrote their own libraries, or something similar, experienced programmers, tell me how best to write so that the code does not turn into shit that even you yourself are not able to read!?
Answer the question
In order to leave comments, you need to log in
Now 200 lines of code have been written, and you are already starting to get confused in it, since the functions are mixed up, and not in the order of execution, and it’s not realistic to put them in order, since some functions are independent of each other.
There is no problem in dividing into files and directories and including these files. If it bothers you - you need a normal editor / IDE with navigation and learn how to use it. There are many ways to structure the code - decompose into files, the easiest. You can also do everything in the spirit of OOP if it makes sense.
For users of your library, there is no difference how it is arranged inside as long as everything works, the API that you provide is important to them, so you need to think it through well. No one is interested in what prefixes the variables have there, and no one except you will remember which prefix what it means either.
If you want to better structure everything very clearly and understand what it came from and what it is, you can take Typescript.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question