Indentation

What is indentation ?

Indentation is used to format program source code in order to improve its readability (wikipedia). See wikipedia article . There is nice Unix utility called indent, which reformats C code (C++ is not yet supported).

See effect of indentation of example code by indent command.

Raw code Indented code
int main ()
{
 int i,x;
     for( i =0 ; i< 137; i++){
  x += i*i; }
  return 0; }
int
main ()
{
  int i, x;
  for (i = 0; i < 137; i++)
    {
      x += i * i;
    }
  return 0;
}

This tool works well with C code. It might corrupt C++ code. Example:

Unindented code:

void
RPTrackCandidateCollectionFitter::beginJob (const edm::EventSetup &)
{
  if (verbosity_)
    {
      edm::LogInfo ("RPTrackCandidateCollectionFitter") << "[RPTrackCandidateCollectionFitter::beginJob]";
    }
}

The same C++ indented with indent tool:

void
RPTrackCandidateCollectionFitter::beginJob (const edm::EventSetup &)
{
  if (verbosity_)
    {
      edm::
        LogInfo ("RPTrackCandidateCollectionFitter") <<
        "[RPTrackCandidateCollectionFitter::beginJob]";
    }
}

edm::LogInfo was splitet between two lines.

Which indentation shall we use ?

CMSSW is written (mainly) using GNU style. That means:

  • 2 spaces for one indentation level
  • brackets in new line after definition

Popular editors and Coding Standards:

Indentation tool for CMSSW config files and C++

There is no standard tool for indentation of C++ code, but we can profit from the fact that some text editors have built in support for indentation. We will use indentation function from emacs editor.

This tool does not move brackets from one line to another. It will just adjust number of spaces/tabs at begging of line.

You can find this script on AFS:

  • /afs/cern.ch/exp/totem/soft/cmssw/tools/indentation/indent.sh

Sample usage of indentation script for C++ code:

Raw code Indented code
int main ()
{
 int i,x;
     for( i =0 ; i< 137; i++){
// C++ comment
/* C comment */
  x += i*i; }
  return 0; 
}
int main ()
{
  int i,x;
  for( i =0 ; i< 137; i++){
    // C++ comment
    /* C comment */
    x += i*i; }
  return 0; 
}

Command used for indentation:

[pctotem31] /home/grzanka > /afs/cern.ch/exp/totem/soft/cmssw/tools/indentation/indent.sh uglyCppCode.cc 
(...)
Indenting region...
Indenting region... done
Wrote /home/grzanka/uglyCppCode.cc
Done

Indented code and SVN

Let us take code which is already correctly indented. When we call indent tools (indent command and emacs) for second time, they will update "last modification" timestamp. It is fine for us, but SVN client will treat such files and "modified" and will try to increase version number.

Be careful when using indentation tool. indent.sh script is checking if indentation will change file. If file will be not change, then it will preserve timestamp.

-- LeszekGrzanka - 19 Aug 2008

Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r3 - 2009-12-27 - LeszekGrzanka
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    TOTEM All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback