Why does a lot of people always judge anti-logic when writing php?
Source: Internet
Author: User
Functionget_post ($ index, $ xss_cleanFALSE) {if (! Isset ($ _ POST [$ index]) {return $ this-& gt; get ($ index, $ xss_clean);} else {return $ this-& gt; post ($ index, $ xss _ function get_post ($ index = '', $ xss_clean = FALSE)
{
If (! Isset ($ _ POST [$ index])
{
Return $ this-> get ($ index, $ xss_clean );
}
Else
{
Return $ this-> post ($ index, $ xss_clean );
}
}
For example, the get_post function in the ci source code is not directly written as follows:
If (isset ($ _ POST [$ index])
{
Return $ this-> post ($ index, $ xss_clean );
}
Else
{
Return $ this-> get ($ index, $ xss_clean );
} Reply content: when loading a page, you must determine the validity of the parameter. If it is invalid, an error message is printed and the result ends. Therefore, the code in an invalid situation is much shorter than that in another case. In addition, the code can be written directly (outside else) to avoid multi-layer nesting because it has exited due to invalid conditions.
In the previous picture, the subject feels like:
This is a defense-style programming method. Normal Defensive Programming has no else. In this example, I tend to write the statement as described by the landlord. But if there are too many exceptions, don't use else. If an error occurs, the system returns the result. There is a style called defensive programming, which is one of the basic practices. Because it is not a pre-compiled language, the exception state is written before the code. When an exception occurs, end the script as soon as possible. For more information, see section 9th "refactoring: improving the design of existing code"-Simplified conditional expressions
Replace Nested Conditional with Guard Claused (Replace Nested Conditional expressions with Guard statements) Thank you for your answers. This question mainly involves two aspects: programming mode and human thinking. There are two main expressions:
If (isset ($ _ POST [$ index])
{
Return $ this-> post ($ index, $ xss_clean );
}
Else
{
Return $ this-> get ($ index, $ xss_clean );
}
There are also the following defensive methods:
Function get_post ($ index = '', $ xss_clean = FALSE)
{
If (! Isset ($ _ POST [$ index])
{
Return $ this-> get ($ index, $ xss_clean );
}
Return $ this-> post ($ index, $ xss_clean );
} I personally think it is a question of thinking.
.. Otherwise ..
It may also be related to people's expectations.
======
The code here is mainly about priority. The post priority is higher than get, so let's first check whether there is a post. 1. In order to reduce indentation as much as possible.
2. Defensive Programming.
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.