[vtk-developers] style

Michael Halle halazar at media.mit.edu
Fri May 25 11:24:13 EDT 2001


I think the particular kind of coding that gets done in vtk inner loops
begs for the vertical compression of code allowed by using the 

			for () {
				// stuff	
			}

style.  Much of the hardest to follow vtk code, particularly the
multi-dimensional code in the imaging classes, consists of a high
"bracket to statement" ratio because of nested looping.  That means
that the total line count can be up to 33% higher, which means that
you're less likely to get the entire n-D loop onto the screen at once.
I think allowing the open bracket to move up significantly improves
code comprehension for the pieces of code that are in fact the hardest
to comprehend.

Here's an example;  it took me all of thirty seconds to find this one,
and I know there are better examples all over the place.  The differences
are relatively subtle here.  However, I think it's difficult to argue
that sample 1 is more readable than sample 2.  I also don't have a real
hard time switching between reading in one style or the other;  I just
happen to have a preference.

//
//     Sample 1 (VTK style)
//
  for (idxX = 0; idxX <= maxX; idxX++)
    {
    xPos += xFloatInc;
    *pXSteps = 0;
    while (xPos >= 1.0 ) 
      {
      xPos -= 1.0;
      itmp++;
      *pXSteps = *pXSteps + 1;
      }
    if (itmp >= inMaxX && idxX <= xMaxIdx)
      {
      xMaxIdx = idxX - 1;
      }
    *pXRatios = xPos;
    pXRatios++;
    pXSteps++;
    }
  yPos = yStart;
  yMaxIdx = maxY;
  itmp = inExt[2];
  for (idxY = 0; idxY <= maxY; idxY++)
    {
    yPos += yFloatInc;
    while (yPos >= 1.0 ) 
      {
      yPos -= 1.0;
      itmp++;
      }
    if (itmp >= inMaxY && idxY <= yMaxIdx)
      {
      yMaxIdx = idxY - 1;
      }
    }
  
//
//     Sample 2 (Uber style)
//

  for (idxX = 0; idxX <= maxX; idxX++) {
    xPos += xFloatInc;
    *pXSteps = 0;
    while (xPos >= 1.0 ) {
      xPos -= 1.0;
      itmp++;
      *pXSteps = *pXSteps + 1;
    }
    if (itmp >= inMaxX && idxX <= xMaxIdx) {
      xMaxIdx = idxX - 1;
    }
    *pXRatios = xPos;
    pXRatios++;
    pXSteps++;
  }
  yPos = yStart;
  yMaxIdx = maxY;
  itmp = inExt[2];
  for (idxY = 0; idxY <= maxY; idxY++) {
    yPos += yFloatInc;
    while (yPos >= 1.0 ) {
      yPos -= 1.0;
      itmp++;
    }
    if (itmp >= inMaxY && idxY <= yMaxIdx) {
      yMaxIdx = idxY - 1;
    }
  }
  

								--Mike




More information about the vtk-developers mailing list