Thread: Coding nicely
View Single Post
Old 12-05-2007, 03:36 PM   #1 (permalink)
Tanax
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Coding nicely

I've seen alot of people's codes here on talkPHP, and I thought I'd write an article about how to code nicely.

Let's start with just some basic stuff.

if- and else statements

Let's just code a really simple if statement:
php Code:
if($_GET['user'] == 'Tanax') {

}

As you see, it's really simple, and you might think that you already know how to code an if statement.
But I've seen some who codes like this:

php Code:
if($_GET['user']=='Tanax')
{

}

The example above, is ofcourse by all means, NOT incorrect. It will work the same.

However if you sometimes want to edit your code, or upgrade it, or perhaps post it here for feedback, it's much harder to read.

This is, ofcourse, user preference, but as you read more in this article, you will perhaps understand why I code the way I do, and why I think that the above example isn't the best way to go.

Now, let's do something inside our if statement:

php Code:
if($_GET['user'] == 'Tanax') {
   
    if(admincheck('Tanax') == TRUE) {
       
        if(isset($_POST['submit'])) {
           
            // Do something..
           
        }
       
        else {
           
            // Do something else..
           
        }
       
    }
   
    else {
       
        echo 'You are not an admin.';
       
    }
   
}

else {
   
    echo 'Select a user.';
   
}

I have.. NO idea what this script would do.
But this is the blueprint. As you see, I'm placing the starting bracket, always directly after the if statement, on the same line. The closing bracket, is places directly under the if statement, that way you know which bracket closes which if statement.

The else statements, you can do pretty much how you want, but this is how I prefer it. Some may do like this:

php Code:
if($_GET['user'] == 'Tanax') {

} else {

}

And I think it looks just as good. However, I prefer the way I did in the previous example.


Another thing you might've noticed, was that I always did one line spacer between the if statement line, and the code inside the brackets. Also, I did a TAB, so I know that the code that are one TAB to the right-> is inside the first if statement.

It's easy to keep control of your script, and easy to go back and make changes to it!


Now, one really important thing, is that you should ALWAYS write comments. Wheather it's a really simple thing that seems obvious at the time. Because 1 month later, you might realize that you want to make a class out of your gallery script(for example), and that way, the comments can help you remember why you did that, and what it did.

Let's go back to our script, a good script would look something like this:

php Code:
// Checks if the user in the URL is set to Tanax.
if($_GET['user'] == 'Tanax') {
   
    // Checking if the function admincheck returns true.
    if(admincheck('Tanax') == TRUE) {
       
        // If the submit button has been pressed.
        if(isset($_POST['submit'])) {
           
            // Do something..
           
        }
       
        // If the submit button haven't been pressed.
        else {
           
            // Do something else..
           
        }
       
    }
   
    // If the admincheck function did not return true.
    else {
       
        echo 'You are not an admin.';
       
    }
   
}

// If the user in the URL is not set.
else {
   
    echo 'Select a user.';
   
}

Yes, it does generate more lines. Even for this simple script. But in the end, you will benefit greatly from it!

Now just to proove my point, here's also an example of a bad code:

php Code:
if($_GET['user']=='Tanax')
{
if(admincheck('Tanax')==TRUE)
{
if(isset($_POST['submit']))
{
// Do something..
}
else
{
// Do something else..
}
}
else
{
echo 'You are not an admin.';
}   
}
else
{
echo 'Select a user.';
}

As you see, it's hard to keep track of which closing bracket is closing which statement.

And if you would look back at this script 2 months after you've written it, it would take a good 10 minutes to figure out what all that does.

Just compare the example of the "good" code I wrote above this example, with the example of "bad" code I wrote in the example above.


This isn't an article to point out that "you code wrong". Because I hope you don't misunderstand me. I'm just pointing out ways for you to code so it's easier to read.

Happy coding!
// Tanax
Tanax is offline  
Reply With Quote