Don't Hack Core! - Breaking Drupal's Cardinal Rule

DON'T HACK CORE! It's THE cardinal rule of Drupal. Like another rule, Don't eat yellow snow, it's inviolate...immutable...or is it?
There is one circumstance when hacking core is a good thing. "Really?" you ask. Yes, definitely. Hacking core is a great way to find out how a particular part of Drupal is working, particularly if you can't use something like xdebug. In that case, if you want to see what the world looks like when you get to a certain place in the code, there's nothing better than sticking in some debug code. There are, however, some precautions you should take:

  1. Copy the file you'll be hacking and give it an additional suffix, like .orig 
  2. If you're using git, you'll be reminded looking at git status. If not, you might want to create a directory solely for moving the copies to, so that you can easily check on what you need to move back
  3. Wrap your debug code in some type of active test. I tend to do this:

  if (isset($_GET['debug'])) {
   debug code
  }
A Drupal 7 example of using this is when you want to see what permissions are being tested for on a page. At the start of the user_access function in the user module, inserting
  if (isset($_GET['debug'])) {
    dpm($string);
  }
if you have the devel module installed, or 
  if (isset($_GET['debug'])) {
    drupal_set_message($string);
  }
if you don't, will answer the question for you, if you load the page with the query string ?debug=1
Oh, and regarding yellow snow...even That has an exception: yellow snow cones!