[vtk-developers] chaining vtk Set... methods

Michael Halle mhalle at bwh.harvard.edu
Fri Jan 29 10:45:35 EST 2010


All this talk of vtkNew and vtkSmartPointer has got me thinking.

One of the elements of VTK syntax that has always bothered me is the 
redundancy of multiple "object->SetXXX();" method calls in typical 
applications.  You end up with a long list of "object->Set...;" lines. 
  That quite redundant. It's also error prone, since cutting and pasting 
code might result in an "object2->..." being added when "object->..." 
was intended.

The fix, as far as I know, is actually relatively simple (if not highly 
pervasive): change the return value of SetXXX() methods to be "this".

I don't know why I didn't think about it years ago, when the change 
would not have been as major.

// Compare (old way):

   vtkProperty *property = vtkProperty::New();
   property->SetColor(1.0, 1.0, 1.0);
   property->SetDiffuse(0.7);
   property->SetSpecular(0.4);
   property->SetSpecularPower(20);

// new way:

   vtkNew<vtkProperty> property;  // or old-style New() call
   property->
     SetColor(1.0, 1.0, 1.0)->
     SetDiffuse(0.7)->
     SetSpecular(0.4)->
     SetSpecularPower(20);

// or
   actor->GetProperty()->
     SetColor(1.0, 1.0, 1.0)->
     SetDiffuse(0.7)->
     SetSpecular(0.4)->
     SetSpecularPower(20);


I believe the change to be backwards compatible (since SetXXX methods 
invariably return "void" now), and also compatible with the scripting 
wrappers.  You get natural indentation, reduced verbosity without loss 
of clarity, and the ability to pick and choose between old and new syntax.

Perhaps I missed a discussion about this idea sometime in the history of 
VTK, but are there technical downsides to this scheme?  The only major 
problem I see is that the new calling syntax can't be used with 
unmodified third party libraries that still return void instead of "this".

Insane?

--Mike



More information about the vtk-developers mailing list