Today I will discuss top coding tips which can be applied to any particular programming language. Also these tips will help you to write self documented programs which are easy to maintain and understand by anyone.
Design first then code
Days are gone when we use to take pen and paper and write the pseudo code first draw the flow diagrams of the program. And finally write the code on computer. Days are changed but still we need to apply SDLC (Software Development Life Cycle) on every program we create. This makes the program bug/error free as well as maintenance is easy.
I suggest you read Martin Fowler’s excellent article Is Design Dead? which explains it in more details.
Variable and Function Naming
Variable names are made up of numbers and letter. There are lots of ways to name your variables and function. I will describe the way I do the naming for variables and functions regardless of any programming language. Generally there are different naming conventions for each programming language as well as they are specific to product/application source code.
Always start the name with the first character of the variable type or the complete variable type name. For example
To declare an integer variable: int iVarName;
To declare a character variable: char cVarName;
To declare an integer variable with variable type name first: intVarName;
To declare a character variable with variable type name first: charVarName;
Also the name should describe the usage of the variable. It should not be just iMyVar or sVar1. So if you want to declare some variables to hold the properties of a car object. Then you can declare the variables like:
int iCarNumber; //variable to hold the car number
string sCarName; //variable to hold the car name
Functions should also have meaningful names, and first character of each word should be capital. There are two ways to name give a name to the functions. Either the first character is always in lowercase then the second word is in proper case. This is mostly done on Linux platforms, but in Microsoft technologies functions are mostly declared with first character in capital.
int GetCarHeight(); //One way of naming the function
int getCarHeight(); //another way
Always write Comments
Comments describe what’s happening and how the specific code is being done. There are certain ways to write comment in C or C++ program.
int MyVariable = 0; //Single line comment here
Multiple line comments
Line of the comment
I would advise to use single line comments for statements and multi line comments at the start of a class or a function. So you could write
Function to return height of a car
Return type: int
int iCarHeight; //variable to hold the car height
Common coding standards
There are lots of ways to declare variables, functions constants, write statements and give comments to a block of code. You can write very good code with proper indentation, comment the code and write the code in a correct syntax. The actual bottom line is that, whatever the coding standard you use, it should be followed throughout the program development. If you use initial character of every variable type name at the beginning of each variable then it must be the same for all the variables declared in the program.
Keep an eye on this blog as in the next few days I would be posting more tips on making your program more memory efficient and processing efficient.