A problem was found when writing a script that was not complex. To start with the code, its main function is to use PHP to determine whether to generate a section of Javascript, and use cookies to record state
/* PHP Code * *
Header ("Content-type:text/javascript");
if (!havecookie (' cookiename ')) {
.. do something
?>
/* Javascript Code */
if (' undefined ' = = typeof document.cookie[' CookieName ']) {
Setcookie (' CookieName ', 3600);
}
.. do something with Javascript
}
?>
The thick-looking code is impeccable, and our dear pony has found the problem. Is that the judgment in Javascript is always true.
if (' undefined ' = = typeof document.cookie[' CookieName ']) {
// ...
}
Because this code is on the PHP side has a premise, is
if (!havecookie (' cookiename ')), it will be displayed on the client. Then, when this condition is not met, this code will naturally not be thrown to the client. This might seem a bit general, so if you put aside the Javascript code, we simply use the PHP code to describe
Header ("Content-type:text/javascript");
if (!havecookie (' cookiename ')) {
if (!havecookie (' cookiename ')) {
Setcookie (' cookiename ');
}
}
?>
This makes it a lot clearer, and it's easy to spot the problem-we inadvertently make one more judgment, although that's what Javascript does on the client.
In summary, I think of some nonsense from this code:
1. The longer the code, the higher the efficiency.
2. Write as many judgments as possible without affecting the logic and process
3. Try to put the function of low complexity before judgment
4. Too much judgment is easy to reduce the efficiency of the program, in the judgment of the use of high time complexity of the function should pay special attention to
5. If you find that if nesting too much, you have to reconsider the process and algorithm
6. Robust code is not guaranteed by excessive judgment.
7. After simplifying the code, you will find many problems that have not been discovered.
8. Too much judgment, another angle of understanding, is lack of confidence in the code