Skip to main content

How to avoid bugs

Write in parts


Finding one bug in the 20 lines you typed for the last fifteen minutes is easier
than finding fifty bugs in the two thousands lines you have typed for one month.

Good identifiers

Use long identifiers such as sum_of_the_weights instead of short ones. Use
longer identifiers for variables and functions used in many parts of the program.
If a variable is used only in a 5 line loop, it can be called s with no risk. If you
are really lazy, at least use acronyms (ws for weight sum for instance).
Also, reuse the same names and parameter order everywhere. Avoid at all cost
this sort of mess

int rectangle_surface(int xmin, int ymin,
int xmax, int ymax) {
return (xmax - xmin) * (ymax - ymin);
}
int rectangle_diagonal_length(int xmin, int xmax,
int ymin, int ymax) {
return sqrt(double( (xmax - xmin) * (xmax - xmin)
+ (ymax - ymin) * (ymax - ymin) ) );
}


Use constants instead of numerical values


It is extremely dangerous to have a consistency between values which is not made explicit. For instance, the size of an array which appears both for the allocation and in a loop should always be specified by the mean of a constant with a name. That way, it can be changed without having to change it in many places, and it also reminds you the semantic of that value (i.e. it is a number of elements).

Comment your code


Comments help the one who is going to use the source code later. It can
be somebody else, or it can be you in one month, or you in fifteen minutes.
Depending upon your goal – are you going to work in team ? who are you going
to work with ? are you planning to maintain this code ? will severe teachers
read it ? – your comments have to be more or less precise.
Always put comments if a piece of code has a non-obvious behavior, for instance
if there is a constraint on the parameters of a function, or if it returns values in
a strange way.

// Angle in degrees, radius in meter, returns square meters
double piece_of_pie_surface(double angle, double radius) {
return M_PI * radius * radius * angle / 180.0;
}

Symmetry and indentation

Arrange your source so that obvious missing or incorrect elements will be instantaneously spotted. Which of the two sources below is easier to debug 

int size; cin >> size; double *a[size];
if(size > 0)
{
for(int i = 0; i < size; i++) {
a[i] = new double[i];
for(int j = 0; j < i; j++) a[i][j] = j + i;}
delete a[i];
}
int size;
cin >> size;
double *a[size];
if(size > 0) {
for(int i = 0; i < size; i++) {
a[i] = new double[i];
for(int j = 0; j < i; j++) a[i][j] = j + i;
}
delete a[i];
}

Note that in a given block of instructions, the number of new is equal to the
number of delete, except in rare cases. The example above does not respect
this rule.

More to read :- Use a DEBUG flag

Popular posts from this blog

C++ Program to find the sum, difference, product and quotient of two integers

#include <iostream.h> #include <conio.h> void main() {   clrscr();   int x = 10;   int y = 2;   int sum, difference, product, quotient;   sum = x + y;   difference = x - y;   product = x * y;   quotient = x / y;   cout << "The sum of " << x << " & " << y << " is " << sum << "." << endl;   cout << "The difference of " << x << " & " << "y <<  is " << difference << "." << endl;   cout << "The product of " << x << " & " << y << " is " << product << "." << endl;   cout << "The quotient of " << x << " & " << y << " is " << quotient << "." << endl;   getch(); }

Putimage function in c

putimage function outputs a bit image onto the screen. Declaration:- void putimage(int left, int top, void *ptr, int op); putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at (left, top). ptr points to the area in memory where the source image is stored. The op argument specifies a operator that controls how the color for each destination pixel on screen is computed, based on pixel already on screen and the corresponding source pixel in memory. c smiling face animation This animation using c draws a smiling face which appears at random position on screen. See output below the code, it will help you in understanding the code easily. C programming code #include<graphics.h> #include<conio.h> #include<stdlib.h>   main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p;   initgraph(&gd,&gm,"C:\\TC\\BGI");   setcolor(YELLOW)...

What is Dynamic Memory Allocation in C++ Program

In the computer world, anything that is processed be it an instruction or any data first needs to be loaded and located in internal memory.  In C++ programs also any data that is processed while executing the program is held in the internal memory.  What is Dynamic Memory Allocation? Dynamic Memory allocation means that the memory that will be used during the program is not known beforehand and is allocated dynamically and on the go. It is allocated during the runtime as and when required by the program. In C++ there are two operators used in dynamic memory allocation  1. New  2. Delete New operator in dynamic memory allocation The new operator in C++ is used to create objects of all types. The new operator will allocate memory of the size of the data type specified in the program.  For Example iptr = new int ;  Storing initial values will allocate needed amount of memory from the free store to hold the value of the specified data-type and store the startin...