“ Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”
– Martin Fowler
Programming style means an elegant and organized way of writing a program so that it can be easily understood. To develop a good program, certain rules and guidelines are essential. These guidelines help us to create more understandable and readable programs.
Let us see some of these guidelines:
- Use proper and meaningful names for identifiers i.e. variables, constants, functions, etc.
For example,
Suppose we wish to write a program to calculate the area of a circle. We require a variable for radius and area. So, instead of using r and a, we use radius and area. For functions, we name them depending upon their usage or the output or their working etc. Like using sum() name for the function which will add two number.
Similarly, we use underscore ( _ ) for variables (or functions) with longer names, for example using avg_marks to represent average marks.
- Indentation and braces
Indentation can be defined as the way to organise and identify major sections of a program. A proper code indentation will make a program easier to read, understand, modify and maintain.
C++ is very much flexible with blank spaces and empty lines, hence giving proper spaces will make a code appear cleaner.
- Comments
Program comments are explanatory statements written in simple English language to help the programmer to understand a segment of a code. Comments are neglected by the compiler during execution of the code.
There are two types of comments- single line comments and multiple line comments.
Single line comments begin with // and continue till the end of line.
Multiple line comments begin with /* and continue till */ is not found.
These were some of the basic guidelines one must keep in mind while writing a code, be it in any language.