ITK/Examples/GuideLines

From KitwarePublic
< ITK‎ | Examples
Revision as of 19:37, 3 March 2011 by Lorensen (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Requirements

Although education of new users in the main motivation, the ITK wiki examples should also:

  1. Encourage good programming style
  2. Promote the proper and modern way to use ITK and write ITK programs
  3. Facilitate the nightly compilation and testing of examples that reside in the ITK wiki

These requirements must be met without compromising the main goal of user education.

Guidelines

All examples should follow the ITK programming style.

  • The indentation style can be characterized as the "indented brace" style. Indentations are two spaces (no tabs!), and the curly brace (scope delimiter) is placed on the following line and indented along with the code (i.e., the curly brace lines up with the code). Example:
 if (this->Radius == radius)
   {
   return;
   }
 for (i = 0; i < this->Source->GetNumberOfPoints(); i++)
   {
   p1 = this->Source->GetPoint(i);
   [...]
   }
  • When appropriate, explicity use the std:: namespace:
std::cout << "Print something" << std::endl;
rather than
cout << "Print something" << endl;
  • All includes from the toolkit should use "" notation. This follows C++ programming conventions. For example:
#include "itkMedianImageFilter.h"
is preferred over
#include <itkMedianImageFilter.h>
  • The main program must have the following signature:
int main (int argc, char *argv{})
or, if argc and argv are not referenced in the code,
int main (int, char *[])
  • If arguments are required, a check similar to the following should be made at the start of the main program.
if (argc != 3)
  {
  std::cerr << "Usage: " << argv[0] << " Alpha InputFile OutputFile" << std::endl;
  return EXIT_FAILURE;
  }
  • An example should never call exit(). If the main program executes successfully, it should
return EXIT_SUCCESS;
otherwise
return EXIT_FAILURE;
  • Input/Output filenames
When possible, filenames should be passed on the command line. This gives the examples utility beyond the data that is used in the specific example.
  • If there are just a few parameters for the example, these should be passed in as arguments. This increases the utility of the example and facilitates testing.
For example, this program
MedianImageFilter InputImageFile radius
would use the arguments in this manner
reader->SetFileName (argv[1]);
radius.Fill(atoi(argv[2]));
  • If your example includes procedures other than main, be sure to declare them static. This is not only good C++ programming practice but is required for the testing framework. All examples within a kit are converted automatically into tests and all tests in a kit are placed in one executable. If a procedure is not declared static, the test executable may have duplicate symbol errors.
static void CreateImage(ImageType::Pointer image);
rather than
void CreateImage(ImageType::Pointer image);