Skip to main content
Joachim's blog

Main navigation

  • Home
  • About

Breadcrumb

  1. Home

Nodes As NIDs

By joachim, Wed, 04/11/2009 - 15:41

Is it just me who finds this poor style and potentially confusing:

<?phpfunction my_function($nodes) {  foreach ($nodes as $nid) {    // do stuff  }}?>

To me, a variable names $nodes will be an array of nodes -- that is, node objects. If it's an array of nids, I would call it $nids to avoid confusion about what we have there.

I'm curious if other people agree (in other words, is it worth my time writing a patch for core or will it just lead to bikeshedding?)

In general, though, I think it's a good idea for variables to be named in such a way that describes what they hold; also for the same data to have the same variable name as it travels through functions. In other words, avoid this:

<?phpfunction foo() {  bar($ponies);}function bar($caterpillars) {}?>

Unless there's a good reason, say if bar() is generic and can accept any animal. In which case, call its parameter $animals, obviously.

Tags

  • drupal
  • code style
  • drupal planet

Frequent tags

  • Drupal Code Builder (9)
  • git (7)
  • module builder (6)
  • 6.x (5)
  • drupal commerce (4)
  • patching (3)
  • Drush (3)
  • Composer (3)
  • Entity API (3)
  • development (3)
  • Field API (3)
  • core (2)
  • issue queue (2)
  • Drupal core (2)
  • modules (2)
  • roadmap (2)
  • 7.x (2)
  • developer tools (2)
  • Rector (2)
  • wtf (2)
  • contributing code (2)
  • code style (2)
  • contrib module (2)
  • maintaining projects (2)
  • drupal.org (2)
  • debugging (2)
  • tests (2)
  • multisite (2)
Powered by Drupal