Rules for writing Robust Stand Alone Scripts
As a software engineer, it is crucial to treat stand-alone scripts as individual products in their own right. By adopting a quality mindset, you can ensure that your scripts are robust and reliable. Here are five best practices to consider:
Implement Try-Catch Blocks: To ensure that your script exits gracefully in case of errors, include try-catch blocks in all functions. If an error occurs while calling a third-party API, consider logging the error information in a file rather than immediately exiting the script.
Use a Global Test Variable: Before running the script, you may want to perform a dry run to ensure that it will run as expected. To do this, include a global test variable that mocks write calls if set to true.
Reporting: It's important to keep stakeholders informed about the status of the script. Consider sending email reports to yourself and others about when the script started and stopped running. If the script is run as a cron job, regular reporting will help you maintain visibility into its performance.
Enhance Logging: To assist with debugging in case of errors, log detailed error information and store the logs in a safe place. A safe place doesn't mean that you should pour it into your DB (lmao).
Revert Changes: If your script performs data migration or merging and encounters an error, make sure to exit gracefully and revert all changes. This will ensure that the data remains consistent and the script can be restarted from a known state if necessary.
I am not an expert, but hopefully my experience will help you manage your mistakes gracefully. :')