talexb has asked for the wisdom of the Perl Monks concerning the following question:

I'm building a Librarian module -- it has a couple of methods for basic object creation, getting, setting and cleanup. These objects are actually used to access documents in a file system.

So far I have a new method that creates a new object with a supplied name if that name is available. If the name's not available, the new method fails.

I'm doing development by testing, so as I add more code I write tests, and continue when the tests pass. It's a very neat way to develop code, but now I want to add tests that try to access an object that already exists, and the current new method won't allow that -- so I've figured I want to write an init method that will also be a method constructor, but only for names that already exist.

Obviously there's some common functionality between the two methods .. I'm not concerned about that. But is there a usual and customary naming convention or approach to do this in Object Oriented Programming?

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re: Determining what new and init methods do
by dragonchild (Archbishop) on Oct 26, 2007 at 14:59 UTC
    In what I've seen across a couple languages:
    • new() acts as the constructor. It handles the physical building of the object, blessing it, and all that. new() is not supposed to be overloaded by children.
    • init() acts as the initializer. It handles validation of the inputs to new(), setting the right values, and the like. init() is supposed to be overloaded by children. The base class will often provide a basic implementation, but may only provide a stub.

    As for accessing objects that already exist, that does sound like something new() should do, in the sense that it needs to create the physical representation of the object. But, init() should handle the setting of attributes according to the fact that this is a load-from-datastore and not an insert-into-datastore.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Determining what new and init methods do
by eric256 (Parson) on Oct 26, 2007 at 15:42 UTC

    I would expect new to create a new object representing a file. I wouldn't expect the creation of a file when calling new though. Maybe createFile and loadFile both call new with the file name, create would just create it first. Then you have the common code in new, and the specific codes for createing or loading in seperate methods. You could then also have a createOrLoadFile that does both and your code doesn't even have to care if the file exists yet or not.


    ___________
    Eric Hodges
        Maybe createFile and loadFile both call new with the file name, create would just create it first.

      That's it -- thank you.

      That's one disadvantage to working on a one-man team -- no one in meatspace to bounce things of. Now I need to change some tests and get moving again. :)

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Determining what new and init methods do
by ikegami (Patriarch) on Oct 26, 2007 at 16:00 UTC

    I've create initializers when I needed a method that was only called once the object was fully contructed. For example,

    my $store = MyStore->new($data);

    might prevent MyStore from being subclassable. The alternative would be

    my $store = MyStore->new(); $store->load($data);