I’d just like to say I don’t consider myself perfect, I’m just offering tips to those who are starting programming. I’m going to use some (sort of) real examples I’ve come across while going through previous developers source, so if anyone gets offended, sorry, and I hope you’ve improved.
Tips for Comments
Not many programmers do enough commenting, it kind of sucks, I know. I’m even guilty occasionally when I need to complete something quickly. So here’s a programming tip for comments.
When you do comment, make sure it’s worth the time. Don’t explain what the following line of code is doing unless it’s incredibly cryptic. Instead, explain why you have the following line/block of code in there.
//if urgency is urgent
if($urgency == 'urgent')
Real example… Even if I hadn’t had years of programming experience I would still know what that if statement was there for. Something better…
//change the text color if the post was urgent
if($urgency == 'urgent')
Maybe not the best example but you get the point. In most cases the person reading the source later in an applications life will have some programming experience. This means they know what simple statements are doing, don’t waste your time on them.
Variable Name Tips
Programming tip for variable names – use human readable variable names. If you start abbreviating variable names you’ll run out pretty quickly. That and it’s impossible to know what it’s supposed to be. Don’t be lazy, you’ve probably got crazy fast typing skills, whats a few more characters?
$result = mysql_query($q);
$cda = mysql_fetch_row($result);
$ar = $cda;
$t = $cda;
$l = $cda;
$a = $cda;
$tn = $cda;
Take a guess what’s in the query, result, array, etc… Can’t even guess without seeing the query really.
A table of CDs was queried returning the artist, title, length, album, and track number respectively. It’s pretty obvious what you can rename those variables to, which leads into my next tip.
Database Queries and their Results
Here’s a programming tip that will make your code much more readable. When doing database queries, describe the results in the variable name instead of using $result, which is used for a lot of database connectivity examples. $result is used to describe what is being stored in the variable, in a database connection example it fits perfectly, in production code, not so much. A fellow programmer will know the query command will return a result into the specified variable.
Building off the example above…
$queryAllCDs = "SELECT * FROM CD";
$cdCollection = mysql_query($queryAllCDs);
$CD = mysql_fetch_row($cdCollection);
$artist = $CD;
$title = $CD;
$length = $CD;
$album = $CD;
$trackNumber = $CD;
But wait, we can make it even better.
$CD = mysql_fetch_assoc($cdCollection);
$artist = $CD['artist'];
$title = $CD['title'];
$length = $CD['length'];
$album = $CD['album'];
$trackNumber = $CD['trackNumber'];
Now the above code is a little unnecessary and it’s a bit obvious, right? Good. Typically you would not create a new variable just to change it’s name but the example is supposed to show you how much more readable you can make your code with a few minor tweaks. Like the following two for loops.
while($newRow = mysql_fetch_row($result))
And now for the better one..
while($CD = mysql_fetch_assoc($cdCollection))
If you doing the same thing in multiple places, create a function. Typically if you’ve done something twice already, there’s a good chance you’ll have to do it again. And what happens when you need to modify it? For instance, pretend mysql_escape_string() and similar functions don’t exist and you want to escape single quotes for your MySQL database. Then you’re company switches to MSSQL!
$artist = str_replace("'", "''", $_POST['artist']);
$title = str_replace("'", "''", $_POST['title']);
return str_replace("'", "''", $value);
$artist = escapeSingleQuotes($_POST['artist']);
$title = escapeSingleQuotes($_POST['title']);
So now when the variables need single quotes escaped differently due to a new database, we just change the function.
I guess that’s enough for now, I’ll probably have more programming tips again soon. Happy coding everyone!
And if you’d like to contribute leave a comment.