Well, as I think we have agreed: the very best way is to not change core Moodle code if there is any other option. But, sometimes you have to.
So, let us say: the second best way is to use a good code version control system, which in the Moodle world means githttps://git-scm.com/. (But really, that is just a more sophistcated way to do what you said: "keeping some patch files around to apply after upgrade". The extra sophistication is really worth having, however.