There was an accident today. The investigation continued and a piece of old code was found.
The code logic is very simple:
<?php
$a = 1;
$b = 8;
if ($a & $b) {
var_dump('same');
} else {
var_dump('not same');
}
What is the purpose of this code? Determine whether a and b are equal.
Why can it be judged? In other words, under what conditions does this logic take effect?
Smart you may have the answer, when both a and b are 2^n.
In the bitwise AND operation, only the same a&b can be greater than zero. Otherwise, each bit must have a 0, and the final result is 0, which means it is not equal.
This is a relatively clever way of writing (there are few people writing this way), and in theory, this operation is faster than directly judging equality.
But it hides two huge problems
- The value will increase very quickly, quickly reaching the range of crossing the boundary, after all, it will increase exponentially
- If this logic has always been maintained by the same person, it’s okay, knowing that the assignment must be 2^n. If the code is handed over to another group or a new person is changed, it is very likely that you don’t know this, and the index rule is not followed when assigning. It's a tragedy
Murphy's law is a question of probability. I feel that nothing in the world can escape statistics, and then it really is a tragedy. For the system, how much an & is worth, may be worthless, or it may be a sky-high price.
Later I thought about it, there are at least two problems at the code level
- If you take over someone else’s code, you must read it again. In addition, you must follow up on the places that are not in line with common sense. These places are often the hiding places of the law.
- When writing code, easy-to-understand is the most important thing. Don't show off your skills, and don't bury pits just to show off your skills. As far as the code in the example above is concerned, that is, every number is not fully utilized, and errors are likely to occur. In addition, the performance cannot be improved much. Optimizing the logic and changing the algorithm will not improve the performance much than this?
One final word:
<?php
$a = 1;
$b = 8;
if ($a == $b) {
var_dump('same');
} else {
var_dump('not same');
}
Isn't this way of writing fragrant?
At last
If you like my article, you can follow my official account (Programmer Mala Tang)
My personal blog is: https://shidawuhen.github.io/
Review of previous articles:
technology
- Spike system
- Distributed system and consensus protocol
- Service framework and registry of microservices
- Beego framework usage
- Talking about microservices
- TCP performance optimization
- Current limit realization 1
- Redis implements distributed locks
- Golang source code bug tracking
- The realization principle of transaction atomicity, consistency and durability
- Detailed CDN request process
- Common caching techniques
- How to efficiently connect with third-party payment
- Gin framework concise version
- A brief analysis of InnoDB locks and transactions
- Algorithm summary
study notes
- Agile revolution
- How to exercise your memory
- Simple logic-after reading
- Hot air-after reading
- The Analects-Thoughts after Reading
- Sun Tzu's Art of War-Thoughts after reading
Thinking