EAFP or LBYL? Rethinking the Philosophies of Error Handling in Programming
How to choose between the ‘Easier to Ask for Forgiveness than Permission’ and ‘Look Before You Leap’ strategies in coding?
Imagine two different persons, Elon and Yang, both planning a road trip across America.
Elon, the adventurous one, prefers to drive without meticulously planning the route, relying on GPS and roadside assistance for any unforeseen issues. If Elon encounters a roadblock or gets lost, he simply reroutes or calls for help.
This is the “Easier to Ask for Forgiveness than Permission(EAFP)” style.
Yang, on the other hand, is cautious and plans every detail of the trip in advance, checking maps, weather forecasts, and road conditions before setting out. Yang ensures there are no surprises along the way.
This is the “Look Before You Leap(LBYL)” style.
In the world of software engineering, we also need strategies to handle unexpected issues and situations, so-called error handling techniques.
The EAFP and LBYL are two common ways for this purpose:
- EAFP: Do something first and handle errors after they happen