Boas Práticas de Programação

Estilo na programação

Programming style is all about following some rules while developing programs. These good practices impart values like readability, and unambiguity into your program.

A good program should have the following characteristics:

  • Readability
  • Proper logical structure
  • Self-explanatory notes and comments

For example, if you make a comment like the following, it will not be of much help:

! loop from 1 to 10 
do i=1,10  

However, if you are calculating binomial coefficient, and need this loop for nCr then a comment like this will be helpful:

! loop to calculate nCr 
do i=1,10

Indented code blocks to make various levels of code clear.

Self-checking codes to ensure there will be no numerical errors like division by zero, square root of a negative real number or logarithm of a negative real number.

Including codes that ensure variables do not take illegal or out of range values, i.e., input validation.

Not putting checks where it would be unnecessary and slows down the execution. For example:

real :: x 
x = sin(y) + 1.0

if (x >= 0.0) then
   z = sqrt(x)
end if
  • Clearly written code using appropriate algorithms.
  • Splitting the long expressions using the continuation marker ‘&’.
  • Making meaningful variable names.

Debugging Program

A debugger tool is used to search for errors in the programs.

A debugger program steps through the code and allows you to examine the values in the variables and other data objects during execution of the program.

It loads the source code and you are supposed to run the program within the debugger. Debuggers debug a program by:

Setting breakpoints, Stepping through the source code, Setting watch points. Breakpoints specify where the program should stop, specifically after a critical line of code. Program executions after the variables are checked at a breakpoint.

Debugger programs also check the source code line by line.

Watch points are the points where the values of some variables are needed to be checked, particularly after a read or write operation.

The gdb Debugger The gdb debugger, the GNU debugger comes with Linux operating system. For X windows system, gdb comes with a graphical interface and the program is named xxgdb.

Following table provides some commands in gdb:

Command Purpose break Setting a breakpoint run Starts execution cont Continues execution next Executes only the next line of source code, without stepping into any function call step Execute the next line of source code by stepping into a function in case of a function call.

The dbx Debugger

There is another debugger, the dbx debugger, for Linux.

The following table provides some commands in dbx:

CommandPurpose
stop[Sets a breakpoint when the value of variable var changes.
stop in [It stops execution when a procedure proc is entered
stop at [It sets a breakpoint at a specified line.
runStarts execution.
contContinues execution.
nextExecutes only the next line of source code, without stepping into any function call.
stepExecute the next line of source code by stepping into a function in case of a function call.