how to add taxonomy vocabulary and terms through code
Rather than writing code, I use the Taxonomy CSV module.  It allows you to export and import taxonomy as CSV files.  You can use the module's user interface or do it by command line with Drush to speed things up even more.

Initial configuration can be a little tricky, but it's able to preserve tree hierarchies and order, etc. so a bit of complexity is to be expected.