Monday, January 5, 2009

Standardizing Variables to Code Faster

If you’ve been coding for a while you probably have some set ways in which you work. I recommend you also widen this method to naming variables, which when done consistently can be a huge help, especially when you are using some fancy MySQL along with your PHP code. If you are building a site with 15 MySQL tables it is helpful if you know all the column names because you always name them in the same way, but the same thing goes for simple variables, and even ids and classes for HTML elements.

A very common example can be variables which hold a user’s data. In my case I always use the variable ‘$u’ for holding the username, and the variable ‘$p’ for holding the password. If you are grabbing a lot of data, like first name, last name, email and so on you can make up your own standard set, I use ‘$fn’, ‘$ln’ and ‘$e’ respectively for the three.

Since you usually use these variables in the same script the importance of naming them consistently may not seem like a huge time saver. However when you get into session variables it saves you a lot of time if you don’t always have to look at where you are defining them to find out the names of each. I pull user data using a mysql query when a user logs in and I immediately place the whole array in a session variable name “$_SESSION['user']“. The names of the columns represent the values of this array, so the username and email would respectively become “$_SESSION['user']['Username']“, “$_SESSION['user']['Email']“. This is the same in my case for every single website I build where applicable.

User data naming consistency is also very prominent in the MySQL tables themselves. As you can see above my columns are also named the same in each case, a user’s email is always “Email”, his last name is always “LastName”. I steer clear of abbreviations with MySQL tables so other coders can easily see what’s going on as well. Consistency doesn’t just apply to the actual names, you can also apply it to cases. As you can see all my column names are always capitalized, they are like wikiwords, but user session variables which don’t come from MySQL are not capitalized. For example, when a user logs in he is assigned a time, which is usually one hour. If he/she is inactive for more than this time (no refreshes), he/she is logged out. There is no need to store this in the database, and so I know that this is not in there the session variable here is “$_SESSION['user']['time']“. This means that if I come back to work on a site a year after completion I know exactly where variables come from.

Even if you don’t yet know any PHP, you’re just getting into HTML and CSS you can still use these good practices. When assigning ids and classes to elements try and work out a structure for yourself that you always use. I use empty div tags to clear floats a lot, so I have a separate class named “cl” for this purpose. Whenever I start out a new webpage I include this class by default in the stylesheet since I know I will most likely need it.

That’s about it for standardizing your coding work, most of it is just common sense really. Try and work out your own method which works best for you. It takes time and a lot of practice to get a system working well since there is so much to think of, but if you always keep this in mind somewhere while coding you’ll be a faster and better coder in no time at all!

Script2please.com is one of the leading offshore outsourcing companies in India, We Offer Rich php web development services, and custom web development

Source: http://www.ghacks.net/2009/01/02/web-development-standardizing-variables-to-code-faster/

1 comment:

seo company in india said...

Seo Company in India

Param Network is a leading natural organic seo company in India, Seo Company, organic seo company, natural organic seo company, natural seo company, professional seo company, timely deliverance and consistent support and cost effective seo services assured.