People have some bad habits on doing things. We specially the programmers also have some common things that we never think to be taken care of. I am going to give you some ideas that may help you to organize your coding style more tightly and efficiently.
1. Plan before you start: Most of the time we never plan of what we are going to do or suppose to do. In this regard, we always miss an important trend called Commenting your every piece of code.
Think about a script where you will write a total piece of code. Let me show you an example:
// Initialize the database connection
// Include the common header markup
// Determine the page variables from the POST data
// Load the proper database info using the page vairiables
// Loop through the loaded rows
// Format the images for display
// Create a permalink
// Format the entry for display
// Add the formatted entry to the entry array
// Collapse the entry array into page-ready markup
// Output the entries
// Include the common footer markup
If you look at this snippet, you will totally get the idea of what is happening here without writing any single line of code. In our case, most of us never include any comment in the script, so sometimes it becomes really impossible to understand what us really going on in a particular coding bloc; even for the coder himself.
Let' see another example:
In this example, it is hard for most of us to understand actually what's going on. But
Now for everyone, this pieces of code is clearly understandable that the $extension array will store all the image extensions of $image_name files.
2. Which statement is for whom: There is another common trend that is been shown to many of the programmers which is,not including any curly braces within a IF...ELSE statement. Some of you may think that this phenomena is not quite harmful or may be you may think this is natural as far it's not generating any warning or errors. But for a thousands of lines of coding where you probably included quite a number of IF....ELSEIF....ELSE statement, then this is obviously a very critical situation for others to understand your code without using any braces in the conditional statements. Lets put an example here:
$foo = 8;
if( $foo<10 )
if( $foo>5 )
echo "Greater than 5!";
else
echo "Less than 5!";
else
echo "Greater than 10!";
echo "Another note.";
If we go through this snippet, it's hard to understand the statements that belong to their respective condition. At the end of the code, it's quite tough to measure whether the last line will execute when the value is greater than 10. If we change the code as below:
3. Follow a Development Pattern:You should always have a structure when you code. I don’t mean to imply that you need to be following the MVC pattern or something equally rigid, but I do mean that you should know how to classify components and where they should go.By following a logical development pattern, many decisions become automatic, and someone coming into your code doesn’t have to guess much when looking for a certain functionality in your codebase.It doesn’t take long, and it really will clarify your apps in a big way.
We’ll never be perfect. But we can do everything in our power to make sure that we’re getting as close as possible.
1. Plan before you start: Most of the time we never plan of what we are going to do or suppose to do. In this regard, we always miss an important trend called Commenting your every piece of code.
Think about a script where you will write a total piece of code. Let me show you an example:
// Include necessary data
// Initialize the database connection
// Include the common header markup
// Determine the page variables from the POST data
// Load the proper database info using the page vairiables
// Loop through the loaded rows
// Format the images for display
// Create a permalink
// Format the entry for display
// Add the formatted entry to the entry array
// Collapse the entry array into page-ready markup
// Output the entries
// Include the common footer markup
?>
Let' see another example:
$pieces = explode('.', $image_name);
$extension = array_pop($pieces);
$extension = array_pop($pieces);
In this example, it is hard for most of us to understand actually what's going on. But
// Get the extension off the image filename
$pieces = explode('.', $image_name);
$extension = array_pop($pieces);
$pieces = explode('.', $image_name);
$extension = array_pop($pieces);
Now for everyone, this pieces of code is clearly understandable that the $extension array will store all the image extensions of $image_name files.
2. Which statement is for whom: There is another common trend that is been shown to many of the programmers which is,not including any curly braces within a IF...ELSE statement. Some of you may think that this phenomena is not quite harmful or may be you may think this is natural as far it's not generating any warning or errors. But for a thousands of lines of coding where you probably included quite a number of IF....ELSEIF....ELSE statement, then this is obviously a very critical situation for others to understand your code without using any braces in the conditional statements. Lets put an example here:
$foo = 8;
if( $foo<10 )
if( $foo>5 )
echo "Greater than 5!";
else
echo "Less than 5!";
else
echo "Greater than 10!";
echo "Another note.";
?>
If we go through this snippet, it's hard to understand the statements that belong to their respective condition. At the end of the code, it's quite tough to measure whether the last line will execute when the value is greater than 10. If we change the code as below:
$foo = 8;
if( $foo<10 )
{
if( $foo>5 )
{
echo "Greater than 5!";
}
else
{
echo "Less than 5!";
}
}
else
{
echo "Greater than 10!";
}
echo "Another note.";
if( $foo<10 )
{
if( $foo>5 )
{
echo "Greater than 5!";
}
else
{
echo "Less than 5!";
}
}
else
{
echo "Greater than 10!";
}
echo "Another note.";
?>
Now, you can understand clearly that the last line will execute every time regardless of the value of $foo.3. Follow a Development Pattern:You should always have a structure when you code. I don’t mean to imply that you need to be following the MVC pattern or something equally rigid, but I do mean that you should know how to classify components and where they should go.By following a logical development pattern, many decisions become automatic, and someone coming into your code doesn’t have to guess much when looking for a certain functionality in your codebase.It doesn’t take long, and it really will clarify your apps in a big way.
We’ll never be perfect. But we can do everything in our power to make sure that we’re getting as close as possible.
No comments:
Post a Comment