In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the defensive programming skills of python code". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the defensive programming skills of python code.
1 what is defensive programming?
As the name implies, defensive programming is a meticulous and careful programming method. In order to develop reliable software, we need to design each component of the system to "protect" itself as much as possible. By explicitly checking the assumption in the code, this is an effort to prevent our code from being invoked in a way that will show error behavior.
Defensive programming allows us to identify smaller problems as soon as possible, rather than waiting for them to develop into a major disaster.
The following summarizes some objections and proponents of defensive programming:
Opponents:
It reduces the efficiency of the code; even a small amount of extra code requires some extra execution time. It may not matter for a function, but for a system of 100000 functions, the problem becomes serious.
Each defensive approach requires some extra work
Supporters:
Defensive programming can save a lot of debugging time so that you can do more meaningful things.
Writing code that works properly, but with some slowness, is far better than code that works most of the time but sometimes crashes.
Defensive programming avoids a large number of security problems.
Defensive programming skills
Use good coding style and reasonable design
Use a good coding style to prevent most coding errors. Such as:
Const keyword:
The keyword const can convey very useful information to people who read your code. For example, adding the const keyword before the formal parameter of a function means that the parameter is not modified in the function body and is an input parameter.
At the same time, the reasonable use of the keyword const can make the compiler naturally protect those parameters that do not want to be modified, prevent them from being inadvertently modified by code, and reduce the emergence of bug.
Volatile keyword:
The volatile keyword is used to prevent compilation optimization before hardware registers (such as status registers) of some parallel devices, global variables accessed in interrupt service subroutines, and variables shared by several tasks in multithreaded applications.
Static keyword:
The scope of the static variable in the function body is the function body. Unlike the auto variable, the memory of this variable is allocated only once, so its value remains the same on the next call.
Static global variables within the module can be accessed by all functions within the module, but not by other functions outside the module.
The static function within a module can only be called by other functions within this module, and the scope of use of this function is limited to the module that declares it.
In bit operations, use operators such as, &, and | as much as possible, and /,%, and * operators as little as possible.
The naming of variables and functions should be meaningful, and try to make sure that a function does only one thing.
The object-oriented idea is often used to write code.
It is also critical to consider the general design before putting it into coding.
Don't write code hastily.
Haste makes waste. Every time you type a word, you have to figure out what you want to type. Think twice before you write each line. What kind of mistakes might occur? Have you considered all the possible branches of logic? Slow down, methodical programming may seem mundane, but it's a good way to reduce defects.
For example, in C language programming, a common problem for programmers in pursuit of speed is to input the wrong "=" as "=", and some compilers will not warn, which will cause problems.
Don't trust anyone.
This means to look at all inputs and results with skepticism until you can prove that the code is correct.
The goal of coding should be clear, not concise.
Simplicity is a kind of beauty, so don't make your code too complex. That is, the code must be logical and readable.
Turn on all warning switches at compile time
If any warning message is generated in your code, you should correct the code immediately. You know, warnings always happen for a reason. Even if you think a warning doesn't matter, don't ignore it.
Use secure data structures
Some of our most common security concerns are probably caused by buffer overflows. Buffer overflows are caused by incorrect use of fixed-size data structures. For example, the following code:
Char * unsafe_copy (const char * source) {char * buffer = new char [10]; strcpy (buffer,source); return buffer;}
If the data in the source is more than 10 characters long, it can cause other problems. We can change it to the following form:
Char * safe_copy (const char * source) {char * buffer = new char [10]; strncpy (buffer,source,10); / / replace strcpy with strncpy to protect this snippet return buffer;}
Check all return values
If a function returns a value, he must have a reason to do so. Check the return value. If the return value is an error code, you must identify the code and handle all errors. Don't let errors invade your program quietly; most imperceptible errors occur because programmers don't check the return value.
Handle memory carefully
Any resources acquired during execution must be completely released.
Initialize all variables at the declaration location
If you accidentally use an uninitialized variable, your program will get different results each time it runs, depending on what spam is in memory at that time. This will cause a lot of random behavior and bring a lot of trouble to the search. Therefore, you need to initialize each variable when it is declared.
At the same time, we should pay attention to some details when coding.
Provide default behavior: the execution of default case is explicitly indicated in the Switch statement. Similarly, if you want to write some if statements without an else clause, stop and think about whether you should deal with this logical default
Check the upper and lower bounds of numeric values: make sure that numeric variables do not overflow each time, that is, data types should be used with caution
Pay attention to whether the cast is reasonable
By declaring a variable, you can make the declaration position of the variable as close as possible to where it is used, thus preventing it from interfering with the rest of the code
Add reasonable exception handling and log files
Set the constant correctly
A good program should do this:
Care about whether the code is robust
Ensure that each idea is explicitly reflected in the defensive code
I hope the code will behave correctly for the input of useless information.
Think carefully about the code you write when you are programming
Write code that protects yourself from the stupidity of others.
At this point, I believe you have a deeper understanding of "what are the defensive programming skills of python code?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.