Документ взят из кэша поисковой машины. Адрес оригинального документа : http://xmm.vilspa.esa.es/sas/6.0.0/doc/devel/coding.html
Дата изменения: Fri Sep 21 14:20:31 2001
Дата индексирования: Sat Dec 22 07:41:00 2007
Кодировка:

Поисковые слова: dra
SAS Coding Standards

SAS Coding Standards

In order to ensure consistency between code written at different institutes, and to ease maintainance and testing, all code shall be written to the following standards wherever possible. There may, of course, be special situations where exceptions are necessary, such as interfacing to existing libraries.
 

Contents


Fortran-90 Coding

Fortran-90 has many new features, which make it a significant advance on Fortran-77. Proper use should be made of these facilities, rather than coding in the Fortran-77 subset of the language.

Structure

An explicit interface should be used for all procedures (i.e. subroutines and functions). To this end, Fortran-90 code shall be written as modules. For external sub-programs that are not written as modules a module shall be written that contains interface blocks for all the functions and subroutines.

Module names shall be lower case. The name of a file containing a module, shall be the module name with the suffix `.f90'.

Any algorithm that is likely to be needed in more than one subprogram should not be implemented in a subprogram but instead be put into a library or access layer. This includes error handling (also when opening files), parameter access, data file access and the like, but also any generic subroutine such as FFT, sorting, selecting, general vector and matrix math etc.

The include statement shall not be used; use modules instead.

Readability

Free format layout shall be adopted, not the Fortran-77 convention that code lines start at column 7.

Procedure argements should be given with the intent(in) items first, then those with intent(inout). A possible exception is a status flag as the last argument.

The code shall be written in lowercase.

Fortran keywords (such as data) shall not be used as variable names.

The names of intrinsic functions (such as sum) shall not be used for user-defined functions.

Statement lines shall be indented in order to make the program structure clear. For example, if, do and case blocks should be indented.

The Fortran-90 relational operator symbols (e.g. `$>$') shall be used in preference to the older .gt. style.

The end statements of long blocks such as if, do, case and main program and subroutine sections, shall use the label matching the original start of the block to make the structure more obvious.

Variable names shall use the Fortran-90 facility for longer variable names (i.e. short cryptic names should be avoided).

Comment lines shall be separated from the main code by a single line. A space should be left between the `!' marker and the text.

All code shall be in lower case, but variable and function names can use uppercase letters to delimit words in the name (e.g. focalLength). User-defined types shall start with an upper-case letter.

Continuation lines shall be indented with respect to the first line.

Initial values for variables should, where they are needed, be given in the type statement.

Variables should not be reused for a different purpose, but a new one declared for each new purpose.

The implicit none construct shall be used and all variables shall be explicitly declared.

Procedures that return a single value should be functions. Note that single values could also be user-defined types.

Trailing optional arguments shall be specified with dummy arguments (call foo(bar=1) instead of call foo(1)).

Robustness

Floating-point values should not be tested for exact equality or inequality except against 0.

Character dummy arguments should have a length specified as (len=*) where possible.

Generic subroutines/functions should be written using assumed shape arguments where appropriate.

Identifiers in a module should be made private by default. They shall be declared public if they are part of the public interface of the module.

A case statement shall always have a default clause.

Subroutine and function arguments shall use the intent descriptor.

Obsolete or to be avoided

Only ANSI-standard Fortran-90 features shall be used.

High Performance Fortran extensions shall not be used.

Common blocks must not be used. Modules should be used instead, where shared access to data is necessary.

Equivalence statements shall not be used.

Entry statements shall not be used.

Goto statements should not be used. One exception would be a single global label for error conditions. Note: For jumping out of nested loop constructs, the Fortran-90 exit statement should be used.

The P-format descriptor should not be used. Use the ESw.d or ENw.d instead.

Do not use units without opening. It is not safe to assume that certain units are connected to standard input, standard output or standard error. Use unit=* for standard input and output.

Comment lines shall be separated from the main code by a single line. A space should be left between the `!' marker and the text.

Before opening a file a unit number has to be retrieved from the library function getUnit.

Do not use namelist I/O.

The save statement is discouraged. Better use module variables for state.

The following obsolete features shall not be used:

The following (obselete) Fortran-77 extensions should not be used:

Fortran-90 File Headers

All Fortran-90 source files shall have a header giving the filename, author, date and a brief description of the purpose of the module.
!+
! Module: example_mod
!
! Authors: Giuseppe Vacanti, ESA/ESTEC
!
! Description: example f90 header
! 
! Comments: just an example
! 
! $Id: coding.html,v 1.5 2001/09/21 10:20:31 gvacanti Exp $
!- 
module example_mod

  use dal
  use param  
  
  implicit none 
  
  private 

  public example

contains 
  
  !---------------------------------------------------------------------
  subroutine example
  !---------------------------------------------------------------------
    

  end subroutine example
  
end module example_mod





This header
is automatically created by running pkgmaker.

C++ Coding

Class names shall start with an upper-case letter. Variables with local scope, member functions and non-member functions shall start with a lower-case letter. Member variables shall start with an underscore.

A source file shall normally define only one class. However, it may be appropriate to include a small related class, such as an iterator for a container class.

The file name of a class shall be the same as the class name, with the suffices `.cc' and `.h'. Source files which do not contain classes (e.g. main.cc), shall have names starting with a lower-case letter.

Header files shall define a symbol, to prevent multiple inclusion, which is the name of the file capitalised, with `.h' replaced by `_H'. For example, FooBar.h should contain:

#ifndef FOOBAR_H
#define FOOBAR_H
... class definition

Global symbols, other than class names, shall be avoided where possible. Constants, types, etc should be defined within the scope of a class. For example:

class Foo {
  public:
The following C++ features shall not be used, since they are not supported by some C++ compilers: Classes implementing new general-purpose Qt widgets shall have a name prefix which has been registered with Troll Tech, for the individual developer.

For example, J.Brumfitt has registered the prefix Qjb, resulting in class names such as QjbDoubleEdit.

Other classes should have names which are unlikely to clash with Qt, STL, GNU and other standard libraries. However, general purpose classes should not use the application name (i.e. Sas) as the prefix.

Classes which dynamically allocate memory, such that the default shallow copy does not work, must either implement a copy constructor and assignment operator, or disable these functions (by declaring them private).

Member functions, function argument and function return types shall be declared `const' where possible. (This may not always be possible where existing non-const functions are used.)

User-defined implicit conversion functions should not be used, since they often lead to ambiguities and unexpected results when mixed with constructor conversions. Explicit conversion functions should be used instead. For example:
Foo::operator int(); // Implicit - Not allowed
int Foo::toInt(); // Explicit - OK

Assignment operators should always test for self-assignment (e.g. x = x).

Member data should be declared private (or protected where appropriate).

The use of `friend' declarations should be avoided, except for overloaded global operators defined in the same files as the class (e.g. operator<<).

Pointers should be set to 0 after a `delete' operation if they remain in scope.

All classes should define an operator<<, as this is useful for debugging. Where there is too much data to print, the operator may simply print the class name or a short summary of the object's state.

Member functions which directly access member variables should normally have the same name as the variable, but without the underscore. For example, _foo would be accessed by a function foo().

A class with any method declared virtual must have its destructor declared virtual. If it has no destructor, then an empty destructor must be added, since the default destructor is non-virtual.

Header files should be compatible with the cxx2html source code documentation tool. This automatically extracts source code documentation, provided that the comments are in a suitable format.

Each member function shall be described by a short comment preceding it in the header file.


C++ Header Files

C++ headers should be based on the following example:
 
/***************************************************************
*
* Source.h               & XMM Science Analysis System (SAS)
*
* Class representing astronomical source
*
* (c) 1997 ESA - Written by Jon Brumfitt, Aurora
*
* $Id: coding.html,v 1.5 2001/09/21 10:20:31 gvacanti Exp $
*
*****************************************************************/
#ifndef SOURCE_H
#define SOURCE_H
#include <iostream.h>
// The Source class describes an astronomical source in the sky,
// which has an associated right ascension and declination.
class Source
{
  public:
  private:
};


Scripts

If you are writing a shell script or PERL script, for inclusion in
the SAS, please keep to the following rules to ensure portability:

Use only "sh", "csh" or "perl". Other shells, such as "tcsh" and "bash" may not be available on other people's machines.

Do your development and testing under Solaris, so you get a real Bourne shell or C-shell. The Linux versions are simply symbolic links to "bash" and "tcsh".

The #! line should specify one of the following paths:

#!/bin/sh
#!/bin/csh
#!/usr/local/bin/perl
(The distribution of Perl installs itself by default in /usr/local/bin/perl. If it is elsewhere, make a symbolic link in /usr/local/bin.)

Don't use "awk" scripts. Use "perl" instead.

Use portable command-line options for unix utilities. For example:
ln -sf                          works on linux but not Solaris
ln -f -s                        is more portable

Scripts should run on an ordinary Solaris installation (+ any programs explicitly mentioned in the installation instructions). For testing, it is a good idea to put the Solaris utilities in front of the GNU utilities in your path. Also, put /usr/bin and /usr/ccs/bin  before /usr/ucb/bin in your path (for Solaris) so you get the System V versions of utilities.


Updated on: September 21, 2001