NEVER EVER alter any template or file in a theme or plugin that you did not author, and this goes for any core file as well. There is no way to protect the code that you alter or add in any of those files, except maybe changing file permissions, but then again, you will run into other issues
ALL customizations should and must be made in either a child theme or a custom plugin. Code like shortcodes as in your question must always go into a custom made plugin as shortcodes add functionality to your site and not your theme.
You should definitely go and read the following post
This is one of the most important things that you need to consider when customizing anything for a site