WordPress / WordPress-Coding-Standards

PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions
MIT License
2.56k stars 486 forks source link

Proposal: formalize database table and field name naming conventions #1764

Open jrfnl opened 5 years ago

jrfnl commented 5 years ago

Is your feature request related to a problem?

The WP Coding Standards handbook currently covers naming conventions for all sorts of structures, but does not formalize any naming conventions for database tables, nor table fields/columns.

As case-sensitive names can cause issues with cross-platform compatibility, the unofficial guideline is to always use lowercase names with words underscore separated.

This unofficial convention is largely complied with within WP core at the moment, with some notable exceptions, such as the comment_ID, comment_post_ID and comment_author_IP fields in the comments table, ID in the posts and users tables and ID and IP in the multisite registration_log tables.

Based on a brief discussion about this in the #core-coding-standards Slack channel, we are proposing to formalize this naming convention and add a rule to that effect to the handbook.

Describe the solution you'd like

If this proposal is accepted and a rule to this effect added to the handbook, a sniff can be written which would examine any CREATE TABLE and ALTER TABLE database queries to verify that table names and field names comply with this requirement.

I'm proposing that existing exceptions in Core as listed above will not be changed as the BC-break would be too large.

Additional context (optional)

/cc @pento @Ipstenu

Ipstenu commented 5 years ago

Naming conventions for tables themselves should be straightforward.

  1. Use the wpdb prefix (don’t hardcode wp_)
  2. Lower case, underscores only for the rest

That alone would save people much drama!

The fields and columns though ... that’s going to need a lot more thought. Camelcasing columns and field names has been drama for me as a host, but in general it’s supported all down the line.

It’s just when someone’s doofy and uses FieldName and fieldname in the same table...

jodriscoll commented 5 years ago

Having something laid out that emphasized something along the vain of "table names should always be lowercase with underscores used for spacing", would help in eliminating a repeat of the discovery I raised to @jrfnl (in #core-coding-standard).