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

David Doria daviddoria+vtk at gmail.com
Fri Jan 29 11:20:05 EST 2010


On Fri, Jan 29, 2010 at 10:45 AM, Michael Halle <mhalle at bwh.harvard.edu> wrote:
> 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

There was a brief discussion of this here:

http://vtk.uservoice.com/forums/31508-general/suggestions/361302-declarative-api?ref=title

It seems reasonable to me. Those who think it will make debugging
harder as Francois mentioned can simply use the old method, right?
There shouldn't be any loss in debugging ability when you are simply
calling Set functions that set a variable (vtkSetMacro). Maybe you
should just not use this new method for Set functions which actually
contain code.

Thanks,

David



More information about the vtk-developers mailing list