Drupal Node Id reflection What's the preferred way to get the current Node ID while inside a Drupal module function, without passing it in?

Adding more text, because StackOverflow won't post short questions:

Ideally, I'd like some kind of reflection function, where I can simply request the NID of the  node being served in the stack trace. Since Drupal revolves around serving nodes, it stands to reason that there should be some kind of reflection function that makes "the node I'm operating on presently, or NULL if I'm not operating on a node", available at all times. 

The best discussions I've found pull the NID out of the URL. This strikes me as a backwards way of recovering the NID, and fragile, to put it mildly.
If you would like to get the current viewed nodes id you should be able to do like this:

// check so we're really viewing a node
if (arg(0) == 'node') {
  // grab the nid for the node
  $nid = arg(1);
  // you could also try to load the node to make
  // sure the supplied nid is valid
  if (node_load($nid) == FALSE) {
    // failed to load the node
  }
}


In the case of an ajax request I guess you really should supply the nid as an argument:

function &lt;MODULE&gt;_menu() {
  $items['sample/%1'] = array(
    'page callback' =&gt; '&lt;MODULE&gt;_sample',
    'page arguments' =&gt; array(1),
    'type' =&gt; MENU_CALLBACK,
    'access arguments' =&gt; array('access content'),
  };
  return $items;
}

function &lt;MODULE&gt;_sample($nid) {
  // use the supplied nid here
  $node = node_load($nid);
  if ($node) {
    // do something with the loaded node
  }
}

jQuery('#output').load('sample/21');