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!
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.
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.
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
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 :)
Should have written in the same comment, sorry.
Posts like this on how to do things are nice in my opinion.
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.
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.
More posts like this are welcome!
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.
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 :)
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.
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.
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.
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!
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.
@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.
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! :)
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.
It seems that WordPress has swallowed my next comment :-P