If you plan to extent the CMSTransformation class then yes, it belongs in the App_Code directory. If you plan to use it in more than one place i.e.: macro, then yes, it belongs in the App_Code directory. If you only make the change once, it will be just fine, yes it will restart but same thing happens if you change the web.config.
The other, not so recommended, place to add this would be in the transformation itself wrapped in some < script runat="server" > tags. Essentially you'd copy the whole GetDocumentCategories() method in the OnInit or OnLoad event.