We often need to verify whether the data exists, the format is correct, if each function processes these, resulting in a large number of code in the function to verify the data, then the following rules should be followed, do not only meet the system needs, A large number of check code:
1. Convert data check, such as: User input data, data queryed in the database, etc. If you can do centralized verification data, you will be able to worry-free, effort. If you can't do it, please follow Article 2.
2. If you are using, what data is used, what data is used, you need to check, especially those who determine whether or not there is or other.
3. What data is verified:
Basic rules, verify the data that you can't control
A :) User's input, try to allow users to enter less data, use menu, select, checkbox, radiobox, etc., let the user choose, if you really need user input, then check it. Including: On the interface, check it directly, or in the program, set it.
B :) From the result of the query from the database, if the database will only enter data from its own code, then you don't have to check, if the data comes from a third party or database format, then check it ( Hard work).
C :) Third-party data Source: Other procedures, others 'data, others' WebService. . . . As long as others, check it. When you use it, you have to check, don't be lazy, who knows that others will not pass the elephant, but when the ant is ...
Data you can control, how to do it in use, pass, there is an error, if so, it is best to throw an exception, correct the error.