Tuesday, December 25, 2012

How to avoid error while writing program/codes ? Avoid errors in PHP, Javascript, ASP, JQuery, C++, Java etc.

Programmers deal with a lot of errors in their programs. They face these errors mainly due to clumsy programming. Today I am sharing some tips to be more efficient to write complex programs and codes.

Hope this article will help you.

Before writing Codes

 

Before writing complex code you need to do the following :

Learn more maths

Open a Maths book and solve some logical question. This is in fact the very first thing to do before writing complex programs. If you don't have enough logical mental ability you won't be able to write proper program.

Draw proper flowcharts or pen down algorithms


Before writing proper script draw a flowchart explaining what will be the elements in your program. The flowchart must contain finalized structure of your program. Once you have built your flowchart you can write your program according to it.

Install  a color editor

Want to test for errors while writing programs? Best way to do it is by using Color editor. They will color syntax up, check each and every line for errors and give you the line which contains errors though they won't actually correct them. You'll sure notice that missing bracket when the entire bottom half of your code is one color!

Adobe Dreamweaver has proved to be the best color editor in writing JavaScript, JQuery, PHP, ASP or ColdFusion programs. It detects all the errors as well validates your file against proper markup. You can get a copy of Adobe Dreamweaver from your nearest Adobe store.

 

While writing codes

 

Stop copying and Pasting code

This is in fact a frequent way to get errors in your code. Type down proper codes as they should be as defined in the flowchart. You can copy and paste the code if they have to be similar but beware while using if.. else statement. A slightest error will ruin your whole program.

Pay attention while writing codes


If you are writing complicated codes, Pay more attention. Don't divert your mind away. A small distraction may result in errors.


Use Comments

Many a programmer use comments just to set copyright of the code. But comments for a important part while writing complex codes. Whenever writing a program don't forget to use comments. Every part of the program should be commented. It will help you understand the code better when you go through the code again. Don't waste your time by commenting each line of your code but a section of code.

Keep your coding clean


This is my hearty request to all the programmers. Don't mess up your code. Be professional and write tidy coding. Try to close nested if.. else statement or other commands properly as shown.


An untidy code with errorsA tidy code with no errors

<?php
if(some_condition){
if(some_condition){
code to execute
}
else{
code to execute
}
else{
code to execute
}
?>
<?php
if (some_condition){
        // Nested if...else 
          if (some_condition){
           code to execute
           }
           else{
           code to execute
           }
}

else{
code to execute
}
?>



Shorten your code

Don't write too long code. Try to shorten your code. For example define a single function instead of writing a whole program in place of it. Remember shorten codes is a sign of professionality.

 

After writing the codes

 

Finalize your work

Don't think "I will add this feature later". When you write codes in one session you know completely the structure of the code. But when you go through the code for the second time, you may not be able to understand the code resulting in errors.

Use proper Interpreters to test your code

Turn on the feature of error reporting in the interpreter that will show you errors, if any, on each line. This takes a lot of the guess work out of what is going wrong, so you know where to start fixing!

Still can't find the errors

Take a bit rest. Refresh your mind. Leave your computer and hang out with your friends. Come back after 3 to 4 hours and clean your mind and eyes. Now with full concentration check your code for errors. This is very helpful because continuous coding can retard your efficiency.


Your comments are most welcomed.

No comments:

Post a Comment

Please don't use slangs. Be polite