Are you a programmer who writes debuggable code?

Are you a programmer who writes debuggable code?

[[121646]]

All programs should be logged in some form, so that we can know what is happening right now. And once an exception occurs, its importance becomes even more obvious. One of the reasons why we divide programmers into different levels is that a great programmer will write logs and debugging tools so that we can debug the program once a problem occurs.

If the program works fine, then it may not make a difference whether or not to log it. However, if the program crashes or produces an incorrect result, it will immediately become clear whether the programmer is good or not.

Example 1: "Let's make a debug version first."

For example, a tester came to me and told me that a function call was not working. We checked the logs and found that the cause might be in an adjacent module. The call to this module returned a series of null values. When we checked the log records of the adjacent module and re-ran the test, we did not get any useful information. We had no idea why null was returned - we didn't know whether the parameters were wrong, the external system failed, or there was a bug in the adjacent module. Who knows?

When we consult the developer responsible for the code, their answer is: "That's easy, let's make a debug version first." In the end, it often doesn't work! Because we don't know where the problem is or what the problem is. And if it has already reached the production stage, adding a debug version means adding more extra work. The code log must contain enough information so that we can find the key to solving the problem once a problem occurs.

Example 2: Tell me what we should do

One of the functions of our product is to find the most cost-effective route for SMS (text messages) and convey the route to the corresponding mobile phone. Due to the current location of the mobile phone and the different operators to which the user belongs, there are many possible routes in theory, and each route has a certain cost and corresponding advantages and disadvantages. In addition, there will be various accidents that will cause some routes to be banned or weighted more. The system searches for the most cost-effective route by given constraints and then returns it to the SMS.

Now, suppose a message is suggested to be sent using route A, but we think route B is better, then how is route A chosen? Without the log data, I would go crazy looking at the hundreds of possible routes, their costs and the complex algorithms. Fortunately, the logs give the reason why route A was chosen. Thank Godness!

The log contains all possible routes, sorted by cost. Even routes that were eliminated due to conditions are listed in the log, along with the reasons why they were eliminated. In short, the log will clearly describe how all the information is input into the algorithm to reach a conclusion, so that we know why the corresponding path was chosen.

Why don't you write debuggable code?

Given the huge benefits, why don't all programmers write debuggable code? There are three reasons:

1. You must have enough self-awareness to realize what will happen if your program fails. However, it takes a long time for many programmers to realize that "I am not the best in the world".

2. Thoroughly test the code to ensure that it works properly in various scenarios. And logs should be written for each scenario. If you don’t test them all, how do you know where to add logs?

3. Many programmers often fail to keep improving their code. If the system has problems during the live demonstration, but the logs do not show why, it is best to add something to the logs to prevent similar situations next time.

Is your code debuggable?

Of course, there will be such a situation, that is, the log is well written, but you still don't know the key cause of the problem. In this case, you may still have to create a debug version. But your log should at least give you clues to the problem.

***, I would like to ask everyone, is your code debuggable? When there is a problem with the code, can your log tell you what happened?

Link to this article: http://www.cocoachina.com/programmer/20141025/10038.html

<<:  The Operator Terminal and Application Innovation Cooperation Conference was grandly held on October 21

>>:  Nokia brand, worth $55.2 billion in 2008, now discontinued

Recommend

Notice! Douyin, Baidu, and Toutiao's future marketing guidelines revealed~

Looking at the market, from the initial portal er...

The secret of Kuaishou Express Ads

As the saying goes, "The food and fodder mus...

Come, let’s explain in simple terms what CPA and OCPC are.

Ever since Toutiao launched the CPA and OCPC mode...

The mini program is launched amazingly, but why do I think it is bad?

In the early morning of January 9, the WeChat min...

Can Alibaba really monopolize the “Overseas Shopping” trademark?

More than 88% of “sea shopping” trademarks are ap...

Gartner predicts 10 strategic technology trends for 2016

[[152040]] Gartner recently presented the results...

Raw peanuts VS cooked peanuts, which one is more nutritious?

Peanuts are a popular nut. No matter it is a snac...