Saturday, May 05, 2007

Consistent variable names across different languages - for Webs.

A dynamic website is often made up of at least three parts:
- html
- a server side programming language (eg python, php)
- a database

(* note, in the current web world, there's more than three. You probably have CSS, javascript, json, xmlprc, rss, and possibly many other encodings of a variable name.)


Why do people still use different variable names for the same data? The html form variable name, the programming variable name, and the database variable name.

Say you have a form which asks people to type their name in and their favourite colour.

<form>
First name <input type="text" name="first_name">
Favourite colour <input type="text" name="colour">
<input type="submit" name="submit" value="What is your favourite Colour?">
</form>


First name
Favourite colour



Now in the server side programming language they convert that to 'firstName' for some weird reason. That's one change that has to be done manually. Since the html variable name is 'first_name'.

$firstName = $_GET['first_name'];


Now in the database they have:

CREATE TABLE products (
`fname` varchar(255) default NULL,
`fav_color` varchar(255) default NULL,
);

That's a third different variable name 'fname', for the same data!

Ok, now here's the fun part. Let's add an 'ORM' which is a Object Relation Mum^H^H^H Manager. For some reason the ORM ads one more variable name change into the mix.

So rather than one variable name, you now have four. Four times as many variable names? No. Because the different names could be anything, fname, f_name, fName, first_name, firstname, firstn, Firstname, etc etc. You have to ask yourself, how was the variable name in the database written? How was it written in the HTML form? Rather than ask yourself these questions you should apply consistency, and then you will know how it is spelt.

Even if you are consistent amongst each different language, you still need to make that mental leap each time - which variable naming scheme am I using now?

Consistent variable names in one programming language is mostly a common practice. However keeping consistent variable names across all of the languages used seems to be lacking. I think the reason for this is that each language has its ways of variable naming. So people forget to apply this consistency across all of them. Preferring to keep consistent in each individual language.

Keep variable names consistent across different languages, and you will have to remember less. Which leaves room in your head for other things.




Melbourne Web Developer Written by a Melbourne web developer. Available for your projects - php, mysql, e commerce, javascript, CMS, css, flash, actionscript, python, games, postgresql, xml.

No comments: