Below I’ve just put a couple of my own conventions, let me know what you think.
Databases:
- Tables are lowercase and singular and separated by an underscore.
This prevents confusion both during coding and managing the database. A lot of languages and frameworks are case sensitive and lowercase is the easiest method to remember.
"user" instead of "Users", "users", "User"
"user_role" instead of "userRole", "userRoles" - Fields are lowercase, singular, and separated by an underscore.
Fields are easier to read when their words are separated by a character. One reason for not using spaces are because they add more syntax to a typical query (ie: SELECT`first name` vs SELECT first_name ).
"user_id" instead of "userId","user-id","userID","UserID","UserId" - Association tables begin with parent and end with child
There may be instances where an association does not involve a parent and a child, so you must use your best judgment.
"vehicle_car" instead of "car_vehicle" - Do not use numbers
You should never have to use numbers in the field name. Perhaps “address1″ and “address2″ are acceptable exceptions (because they are used in real world, however adding “address3″,”address4″ are unneeded and probably mean you need to store addresses in a separate table). - Primary key should end with “_id”
Most of the time, you will begin with the table name and end with id. Makes joining tables together simpler because you will always know what the primary key is.
Programming:
- Variables are camel case with meaning or with comments
Example: If using “roll” in a loop, either keep the loop small or commented to explain what the rolling value will contain or use something more verbose such as “numberEntriesAdded”.- Acronyms are lowercase (”getXmlDocument” instead of “getXMLDocument”).
- If you find yourself having to place numbers in variable names, your code is either redundant or hard to read.
- Private class variables should be prefixed with a “_” ( “_privateVar” instead of “privateVar”).
- Classes are uppercase without separation
"MyNewClass" instead of "mynewclass","my_new_class"- Constants are uppercase with separating underscores.
"MY_CLASS_CONSTANT" instead of "my_class_constant","MYCLASSCONSTANT"
- Constants are uppercase with separating underscores.
- Methods are camel cased without separation
"executeMyMethod" instead of "execute_my_method","ExecuteMyMethod"- Name contains at least an action, object, and value/field
action: get,set,increase,decrease,add,subtract…
object: car,row,page,template…
value/field(if applicable):width,pixels,id,name
- Name contains at least an action, object, and value/field










October 13th, 2008
O comments at "Naming Conventions"
Comment Now!