Is Assert Evil: Weighing the Pros and Cons
The Go language designers have dismissed assertions as evil for encouraging the avoidance of proper error handling and reporting. This begs the question, do these arguments hold water for C and C programmers who rely on assert() for debugging?
Pros of assert()
Cons of assert()
Distinguishing Assertions and Error Handling
The distinction between assertions and error handling is crucial:
Conclusion
Assert is not inherently evil, provided it is used appropriately. For catching code execution errors during debugging, it serves its purpose well. However, for handling expected runtime errors, error handling mechanisms are the preferred approach.
The above is the detailed content of Is `assert()` Evil? A Weighing of Pros and Cons for C and C. For more information, please follow other related articles on the PHP Chinese website!