I talk about defensive programming.

Source: Internet
Author: User

Cnblogs really lively, I like the atmosphere here, today to share their own " defensive programming " view of it!

All along, our project has been deeply influenced by NullPointerException. Because of the problem caused by the null reference, from the day the project was born, there is no broken, error logs often burst java.lang.NullPointerException. I've talked to other colleagues about whether this is a data problem or a procedural problem? Some people think this is a data problem, if it is because the data from the database is empty, then the database table data some fields to be not empty, the processing of the data on the line. Some people think this is a procedural problem, the procedure to do non-empty judgment, to determine the object you want to use is not empty, the program level to do a firm, to avoid nullpointerexception, in the process of making judgments will cause a controversy, is not where to do non-empty judgment? So is it all over the boring duplicate code of if (Object! = null)?

In the beginning, I also simply think that the core of the problem is to analyze whether it is data or procedural problems, because we can run the software, the essence is to use the logic of the program to do the data processing. But with the increase in experience, I realize that this may not be an either/or issue. After reading some of the programming methodology books, I realized that the program should have a certain degree of fault tolerance, need to do "defensive programming." "Defensive programming" has a number of technical tools, such as assertions, exception handling, or some validation frameworks, which are not discussed here. I talk about the purpose of doing this: put the bad in the outside and put the trust in. with the "Walking Dead" analogy, I likened Rick's team to our program. The students who have seen the show should know that in the bad environment of the later stage, food and ammunition are scarce, there are rotting zombies everywhere, and more horrible people, yes, people. In this environment, SAC kings everyone to protect themselves, can not understand the human heart, we must do a good defense. So there are three questions about Rick's new interview: How many walkers have you killed, how many lives have you killed, and why did you kill or not kill? These three questions are like the technical means of our defense, and through a strictly screened person can enter Rick's team. And the Rick team to pass the test of new people, that really is no reservation as their own, mutual help, full trust. So as our program is also, at the outset, we have a high degree of vigilance and suspicion of external data, to set up layers of interception, verification through to enter the interior of our program. Once we have verified the data, we will trust him completely, no longer check the basic types of data or references that are passed in from outside, my subroutine will be easier to deal with these trustworthy friends. Summing up here, I think of kingdoms in a word: "The World Heroes, only married and the". Caocao this sentence, in posterity, it is indeed true, because Cao and Liu Bei as the Three Kingdoms period of the outstanding leaders, in the employment of almost all used to eight words can be summed up a practice, my this blog, also use these eight words to finish: suspect do not use, employing not suspect.

I talk about defensive programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.