It is definitely a good idea to plan in advance and set up a recovery contact: https://support.apple.com/en-us/102641
And that link is to recovery contacts, not codes. If you’re willing to trust one person you can get back into your Apple account after a disaster.