[nycphp-talk] naming identifiers
Kristina D. H. Anderson
ka at kacomputerconsulting.com
Mon Aug 31 21:06:22 EDT 2009
Essentially the thinking is that whenever you're processing code that
involves a lot of tables or a lot of queries (and returned results),
you end up with a bucketload of things named 'id' to handle.
For instance imagine pulling out the contents of ten tables through ten
queries in one script, and all the ID fields are named, id. Or if you
are doing a SELECT query where you are pulling stuff from 3 tables at
once and all three of them have a field that you need to reference
named, you guessed it, id in all three cases.
The thinking being that if you don't name everything 'id' then you
won't make mistakes caused by everything being named 'id'.
The flipside of the argument is that it gets darn annoying always
forgetting whether or not in this table the ID is prodid or prod_id or
productid or whatever it may be...and you're looking it up for the 6th
time that day at 2 AM....
Or you inherit a database that's got fun ID field names like
cat_subCat_xtab_id and you start cursing the day that someone came up
with the silly idea that naming ID fields 'id' was a bad idea...When
you start muttering things like "this is a database, not the Iowa
Writer's Workshop" under your breath, you know it's time to leave and
get that cold beer.
And you've got to be consistent with your own database design. If you
decide now to go with prod_id, stick with that, don't all of a sudden
start to think that the underscore is "so last year", and start to use
prodid...so you don't get confused between databases. I've been guilty
of getting bored with using the same old, same old field names over and
over and making my life more difficult than it has to be...
I've tended to think over the years that this "rule" has significant
points both for and against.
> Kristina D. H. Anderson wrote:
> > ...and also to never use just "id" for the primary key field name.
> Hi Kristina,
> I'm probably going OT when I shouldn't, but I'm so glad you mentioned
> that because I was just wondering about it yesterday. I started out
> using "id" because it was easy when I needed to use PHP to delete
> records form different tables and such, but then sometimes it got
> confusing so I started using names like "prod_id".
> Besides what I discovered on my own, are there other reasons it's
> considered bad practice? I'm very interested in knowing.
> New York PHP User Group Community Talk Mailing List
More information about the talk