Next most basic thing that one should know before getting into Programming is, some best Coding Practises. These practises are recommended to be followed for writing error free and efficient programs.
1) Commenting : Whenever you write a program logic, make sure you comment about the logic. This helps you understand underlying codes and the logic in it, when you revisit the code at a later time.
2) Variable Names : It is recommended to give a variable name, which is meaningful. One common mistake programmers do is, giving variable name as i,j,k etc for array indexers.
3) Indentation : Make sure you properly indent blocks of codes, so that it is easily readable. When there are lots of opening and closing braces your code will become unmanageable if you didnot indent. You wont be able to see where a block of code starts and ends.
4) User Inputs : Make sure that program logic will be able to handle all possible user inputs. Many times, we wont see what will be the possible inputs from the users. For eg) assume that we wrote a program to find whether a number is prime or not. Most times, we wont check for the integrity of the user inputs. If we give negative numbers, then also it will show whether the number is prime or not. Sometimes our code will say 1 as prime number, if we did not make necessary checking for the user input as 1. Take another example of finding average of 5 marks which is given by user. Most times we assume that user will give something between 0 and 100. But what if the user gives a negative number or something above 100. The program should ignore such cases/throw errors.
5) Overflow/Truncation: When the result of addition or mulitplication increases beyond the actual value a datatype can hold, error occurs. And instead of floating point operation as in finding average, normal integer operation is performed error will occur in the result. So checking the data type of variables used is very important and we should make sure that user inputs corresponds to the variable of the data type
6) Memory : Use variables of appropriate data types so that we use memory for what we need and we wont be wasting memory. It is not a good practise to use long int, when you are sure that values stored in the variables will be in the range which int variable can hold. Usage of arrays with very large maximum index will create wastage of memory. Decide on the maximum index, which you thing you will need. There is other options like Linked List also.
7) Code Optimization : Make sure you follow code tuning techniques, so that your code will use less memory and takes less times to execute. Loops should be properly handled, and exit conditions should be available in the loops so that it wont get into infinite loop.
8) Error Handling : The Program should have appropriate error handling techniques and the user should be displayed with messages which the user can understand. Logging can be done for debugging incase of any errors, so that we will be easily trace the problem and fix them.
Incase if you find any improvements that can be done in the code snippets that you find in this blog, let me know so that others will be benefited.
1) Commenting : Whenever you write a program logic, make sure you comment about the logic. This helps you understand underlying codes and the logic in it, when you revisit the code at a later time.
2) Variable Names : It is recommended to give a variable name, which is meaningful. One common mistake programmers do is, giving variable name as i,j,k etc for array indexers.
3) Indentation : Make sure you properly indent blocks of codes, so that it is easily readable. When there are lots of opening and closing braces your code will become unmanageable if you didnot indent. You wont be able to see where a block of code starts and ends.
4) User Inputs : Make sure that program logic will be able to handle all possible user inputs. Many times, we wont see what will be the possible inputs from the users. For eg) assume that we wrote a program to find whether a number is prime or not. Most times, we wont check for the integrity of the user inputs. If we give negative numbers, then also it will show whether the number is prime or not. Sometimes our code will say 1 as prime number, if we did not make necessary checking for the user input as 1. Take another example of finding average of 5 marks which is given by user. Most times we assume that user will give something between 0 and 100. But what if the user gives a negative number or something above 100. The program should ignore such cases/throw errors.
5) Overflow/Truncation: When the result of addition or mulitplication increases beyond the actual value a datatype can hold, error occurs. And instead of floating point operation as in finding average, normal integer operation is performed error will occur in the result. So checking the data type of variables used is very important and we should make sure that user inputs corresponds to the variable of the data type
6) Memory : Use variables of appropriate data types so that we use memory for what we need and we wont be wasting memory. It is not a good practise to use long int, when you are sure that values stored in the variables will be in the range which int variable can hold. Usage of arrays with very large maximum index will create wastage of memory. Decide on the maximum index, which you thing you will need. There is other options like Linked List also.
7) Code Optimization : Make sure you follow code tuning techniques, so that your code will use less memory and takes less times to execute. Loops should be properly handled, and exit conditions should be available in the loops so that it wont get into infinite loop.
8) Error Handling : The Program should have appropriate error handling techniques and the user should be displayed with messages which the user can understand. Logging can be done for debugging incase of any errors, so that we will be easily trace the problem and fix them.
Incase if you find any improvements that can be done in the code snippets that you find in this blog, let me know so that others will be benefited.
0 comments:
Post a Comment