[Bioc-devel] S4 initialize methods (was Re: "patches" for Gviz: utr plotting support and direct BamFile plotting)

Hervé Pagès hpages at fhcrc.org
Thu Aug 23 19:20:11 CEST 2012

Hi Florian,

On 08/22/2012 11:47 PM, Hahne, Florian wrote:
> Uh, uh,
> Seems like I am the bad guy still using the dreaded initialize methods
> around here :-(
> I do agree with most of what you guys say, but still want to put my two
> cents here. My lawyers are preparing a more complete statement at this
> point :-)
> Having a constructor function to me somewhat implies that you want objects
> from that class to be created by the user in a manual process. In more
> complex class hierarchies you don't really want that, but rather you want
> to pass through all the parent class' instantiations to fill the relevant
> slots appropriately. Whenever you need something more complicated than
> foo at a=b in these cases I do not see a way around the initializer. For
> instance, in Gviz I have a whole bunch of classes that inherit from each
> other, each of them grabbing the arguments to fill their slots while
> objects are instantiated. The bottom-most of these classes will gobble up
> all the arguments that are left over and stick them into a plotting
> parameters object. I guess I could have explicit constructors for all of
> those classes, and in those explicitly call the parent constructor, thus
> walking through the hierarchy, doing whatever magic I need to do to make
> things work. Now that doesn't strike me as particularly elegant either,
> and I can't see how that would help with the code copying issue.
> Another remark regarding validation. For classes with a large memory
> footprint I am very much worried about unnecessary copies of the data. For
> objects with very light content that are created very often however I care
> much more about fast object instantiation. Running through a validation
> method each time you create an object adds quite some overhead to this

You don't have to. You can either use setValidity2/new2 from the
IRanges package:

   setClass("A", representation(x="numeric"))
   setValidity2("A", function(object) {cat("validating ... OK\n"); TRUE})

   > a <- new("A", x=2.34)
   validating ... OK

   > a <- new2("A", x=2.34, check=FALSE)

or bring the case in front of the R-devel court for adding some kind of
'check' argument to new(). Take all your lawyers with you.


> (and we all know that building S4 methods even without validators is not
> cheap at all). I remember there were times when the use of validation
> methods for classes was not recommended. And personally I am no big fan of
> them for the reasons pointed out by Kasper and Martin before.
> That being said, I will take a closer look at my package to figure out a
> way to code everything without the initialize methods and report back to
> you guys about my success. I do not generally advertise the use use of
> initializers (as a matter of fact I am far far away from that), I just
> want to stand up here for these cuddly little creatures, threatened by
> extinction and make the point that they still do have their rightful place
> in our Bioconductor eco systemŠ
> Cheers,
> Florian

Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319

More information about the Bioc-devel mailing list