Web Development: Standardizing variables to code faster - gHacks Tech News

Web Development: Standardizing variables to code faster

Welcome to a new exploratory post here on gHacks! I actually work as a web designer specializing in standalone PHP and MySQL based sites, and I've always wanted to write a bit about coding, but it doesn't really fit my own blog and I just don't have time to start a whole new blog for it. Since gHacks is a tech blog we thought we'd try it out here with Martin and see how it goes. if you like the idea of similar posts here on gHacks please let us know in the comments (and also if you don't), we want to post stuff you guys are interested in, so feel free to boo it on out of here! So here goes nothing:

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!

Summary
Article Name
Web Development: Standardizing variables to code faster
Description
Tips and information about standardizing variables in web development to code more efficient.
Author

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:

Comments

  1. Beirti said on January 2, 2009 at 8:28 pm
    Reply

    I gotta say I’m a littleappalled! Variables should be named in such a waythat anyone can sit down an read the code. Using $u for username saves you some characters but if you keep going like that you’re just self-obfuscating.

  2. Martin said on January 2, 2009 at 8:48 pm
    Reply

    Beirti I guess it depends on the purpose of the code. If you write for yourself it might work if you use standardized variables all the time.

    If you write for someone else or might sell or distribute the code / product you might want to think about proper names and commentary.

  3. Flo said on January 2, 2009 at 8:55 pm
    Reply

    I gotta agree, that for self-coding variables can be named [however up to a certain point and number] in an organized, standardized way.
    However if you work on something bigger, longer or a little more descriptive variables are better.

    PS: web dev posts were the only thing missing to make ghacks the perfect blog, hope there is more where this came from ;)

    PPS: happy new year everyone :P

  4. RG said on January 2, 2009 at 8:59 pm
    Reply

    Usability for your yourself or distributable code is best when heavily commented. Standardizing as it is defined here and saving keystrokes or not.
    My 2 cents :)

  5. RG said on January 2, 2009 at 9:02 pm
    Reply

    Should have written in the same comment, sorry.
    Posts like this on how to do things are nice in my opinion.

  6. Paul. said on January 2, 2009 at 9:17 pm
    Reply

    After three times reading your articel, and many searches, i understand now to the detail, what your writing about. Perhaps its possible to write a more explanatory, blogmessage. Mabay because you are so well know with the topic its not explanatory enough for me.
    But I find it very interesting what you write about. Perhaps it is wise/useful to start with a novice curcus.

  7. Dotan Cohen said on January 2, 2009 at 11:12 pm
    Reply

    I agree with Beirti on this one. _Always_ code for someone else, never for yourself, even if you are the only person who will ever see the code. When you go back to your code five years down the line, you will not be the same person you were when you wrote the code, and you will not remember what all the shortcuts mean. Write _maintainable_ code.

  8. David said on January 3, 2009 at 4:31 am
    Reply

    More posts like this are welcome!

  9. Angelo R. said on January 3, 2009 at 9:10 am
    Reply

    First off, great post. As an avid web developer and designer articles like this are alwass great to see and this was really all that was missing from ghacks. And now it’s there as well, hopefully there will be quite a few more.

    I’ll admit that having naming conventions are a great idea when programming. You always want something standard across your code that you can quickly see and identify, but obfuscation is never the way to go. Not only does it make your code harder to read later on, but it makes it harder to make changes later on.

    Sure when you program something you always intend to be the only person ever working on it, but say you run into a problem, you may need someone else to take a look at it and help figure it out, and now your obfuscation – however well intentioned – has slowed you down.

    Granted it could be easily remedied by comments, and perhaps when you’re done creating your web site/application a simple find/replace all would solve the problem.

  10. Daniel Pataki said on January 3, 2009 at 12:03 pm
    Reply

    Hi Everyone!

    First of all, thanks for the positive comments, I’m glad you like the idea of web development posts here on gHacks!

    Second of all, actually I agree with you guys too, but I can only share so much in one article, so I couldn’t explain better :)

    In reality my code is very commented. I am actually training someone to work with me, so my code right now is about 70% comments. Also, with variables like $u it is VERY obvious in the code.

    I actually only use that variable in the login script which is short, after that I use the session variable which is descriptive. Overall I agree that variables should be descriptive, but I also believe that coders should be free to save themselves time and use $u for username for example. I usually send along a documentation for all my websites I code, but I did feel a bit ashamed, so I might change my evil ways :D

    Thanks a lot for your positive comments, I’ll try and make things as interesting as possible! Also, I feel I know quite a lot about this topic, but I am nowhere near knowing everything, I am anxious to learn from you guys so please do comment :)

  11. Daniel Pataki said on January 3, 2009 at 12:09 pm
    Reply

    Hi Paul!

    I wanted to address your issue separately. I did think about people who are just learning this stuff, that’s partyl why I added the reference to HTML and CSS.

    I have good news though, I will be writing some articles aimed toward the beginner side! However I think there are plenty of great sources online for learning this stuff (w3schools.com, php.net, tizag.com and so on) so I will focus more on the practical side.

  12. rruben said on January 3, 2009 at 1:11 pm
    Reply

    I am novice in PHP but am busy learning it when I have some spear time. So in my case these sort of posts are welcome, but this might scary of some people who are not coders and don’t want to learn this. For them, these kind of posts might dilute the image of Ghacks for them, because this is not about software and simple tips/hacks everybody can use or apply.

    So although I like the idea of having this sort of posts I suggest that you first run a poll to do some research about how many people on Ghacks really are webmasters.

    E.G.
    You can make a poll like this:
    Are you interested is posts about:
    -PHP & MYSQL
    -HTML & CSS
    -I am not interested in coding related articles

    In my opinion you could better post this kind of articles on a coding focussed blog as a guest blogger and you can of course link to that blog to give some more exposure to the articles.

    Of course it is possible that I am not right, but like I said you might run a poll first to see how many Ghacks readers support this idea.

  13. Daniel Pataki said on January 3, 2009 at 6:24 pm
    Reply

    Hi rruben!

    Thanks for your input! The “bad” side of this situation is that you might be right. I was thinking of what you said before undertaking this here at gHacks. Martin is definitely in on your comment and he will decide on the poll, but we have decided to deduce the attitude of the readers by the comments and if we get negative responses we will not post any more than a few of these articles :)

    If you guys would like less coding articles I don’t think I will be guest posting, I’ll create a separate blog for it and let those of you interested know :)

    Again, gHacks is for you guys, so if you don’t like these posts let me know and there will be no more of this evil! :)

    Thanks for input rruben!

  14. rruben said on January 3, 2009 at 6:51 pm
    Reply

    Thanks for appreciating my comment.

    Why I think a poll is better than relying on comment is that most of the readers do not comment. Especially to say you are against something is a bigger step to really give a comment.

    A poll is anonymous which means you will hear the voice of a lot more readers which makes the statistic more trusted.

    Like I said before I would love to have these kind of articles but why I think it’s not the best idea is purely from a branding/marketing point of view.
    gHacks is branded as a blog where you get tips and hacks and get to know about software to make life easier. What’s making this so strong is that everybody can use them, even if you are not an IT person. You can see this as a niche, but still big enough to attract everybody who use a pc regularly and who are interested enough of course.
    When you also write about specialistic things not everybody understand, you satisfy the minority but in exchange you probably decrease the interest of the mass by diluting the image of the brand gHacks.

    The effect of talking about code is actually the same as talking about philosophy. It’s really interesting, but when it becomes difficult, little people stay listening while many start to wander off.

  15. Angelo R. said on January 3, 2009 at 7:20 pm
    Reply

    @Daniel Pataki – Alternate blog or guest-posts, either way you’ve found yourself a new reader. I’ve already been following your blog over at Hack Your Day for quite a while and I love reading about the way other programmers work, it offers you solutions to problems that you may never have thought about.

  16. Daniel Pataki said on January 3, 2009 at 10:11 pm
    Reply

    Hi Angelo!

    I’m glad you like my stuff, thanks for being on board!

    You know I actually didn’t realize how true what you said is until I started creating sites. And not only helpful in solving problems you might not think of, but also in solving those you know of better. Coding is a very useful thing to learn, even just as a hobby :)

    On another note, I’m trying to make Hack Your Day a lot bigger than it is, so keep on visiting! :)

  17. Jeremiah said on January 5, 2009 at 2:06 am
    Reply

    I second rruben’s comment – I am happy to see this kind of post but really think it belongs in a separate venue (maybe even a subdomain). If you decide to start another blog and are looking for articles I would love to ‘guest’ contribute.

  18. rruben said on January 5, 2009 at 1:30 pm
    Reply

    It seems that WordPress has swallowed my next comment :-P

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.