How to write something that really works in a most efficient way?

As the project goes larger and larger, I can never get rid of the fear of the code will go out of my control. However, I need to face it. I can't being super clear at every time about how all the code is working, in fact, it is impossible for a person to just understand how a huge work is working at every step. Even if you write the whole project on your own. It may be possible for you to understand the code when you just finish that piece of the part of the code, nevertheless, after some time, you will always confused about your former codes. So how can I solve this problem? Is there a way suitable for me to control the whole big project?

Possible cause for the problem

In order to solve this problem, we need to first find out what is the cause for this problem. Why is it so difficult for a person to remember all the work flow at any moment? I got an answer here, the short term memory for a person is limited.

maximum number of figures we can remember in the short term memory is \(7\pm 2\).

Hence, I guess, the maximum size of logic that can be handled by any human beings is also limited. So, It looks like an tragedy that we can never handle the huge project easily. Is that really the truth?

Solution

Focus on small number of stuffs at a time

Though, the reality seems to be an disaster, there is always a way to solve it. For me, the secrete is built deep inside the most fundamental thoughts inside our brain, Dynamic programming, recursion, black box model, and unit test. Here we got the constraint on our mind, we can only focus on a small number of stuffs at one time. So, what if, we just admit it, and just do a small number of stuffs at one time? Which means that we should never think about more then a small number of logic flows. For example, all of the things we do every time is just always something that is super easy like plus and multiply, etc.

Rule 1: always focus on a small number of functions.

have faith in the existing code

However, if we only build such small stuffs, how can us ever build something has tens of thousands of lines of code to work? Secretes here is that, we need to learn to "believe". Every time we build a new function, we are always just assemble some small stuffs and made a little more action, And just BELIEVE that all the small part we use is perfect on there functions, they will always generate the input we need for our model.

Rule 2: every exist things should be treated as a perfect black box.

Make sure everything works correctly by unit test

No way, how can I ever believe other's code? If the existing code is wrong, I will never figure out the bug by just testing my own code. I need to inspect the existing code anyway. If you got such a question, then, what you really need is unit test. If you don't believe in the code, just add more test. If the existing code pass all the test case, and you still got bug. Then, just check you own code. One best thing here is that the unit test is something that can be done automatically, hence it will definitely boost you development.

Rule 3: trust the existing code via unit test.

Use dynamic programming idea to boost your project

In fact, most of the people knows the above three rules, however, just with the above two rules is not enough for you to handle a huge project efficiently, though it can help you find a way to handle the huge project correctly. So, here comes another question, how can we handle the huge project efficiently? The secrete here is to try you best to reuse the works that you have done before. It is quite similar to the thoughts of the dynamic programming, If we can cache every intermediate output then it would be great for us to build up huge project.

Rule 4: name your intermediate output in a general way, then use them like dynamic programming.

Consider the whole structure via recursion

Finally, in order to make the whole stuffs above works. You need to keep something deep in your mind every time, i.e. recursion. If you think every thing in a recursion way, then it would be easy for you to achieve the above goal.

Rule 5: think in a recursion way.


Jamworld

Never Give Up, Never End!