Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: chaining method calls

by EvdB (Deacon)
on Jun 12, 2003 at 09:22 UTC ( #265307=note: print w/replies, xml ) Need Help??

in reply to chaining method calls

I am not a fan.

Consider this code:

use HugeObject; my $result = 0; { my $object = HugeObject->new(); $result = $object->get_even_bigger(); $object = undef; } # Phew, should free up memory now. if ($result) { # code here }

As the object was created inside the {} it should go out of scope and the memory should be cleared - a good thing. However as object returns a reference to itself it is not cleared, and stays in memory.

If the object had just returned true or false this would not be a problem.

This may not be a problem if you read the docs for HugeObject but why add the compication for some slightly shorter code?

--tidiness is the memory loss of environmental mnemonics

Replies are listed 'Best First'.
Re^2: chaining method calls
by adrianh (Chancellor) on Jun 12, 2003 at 10:16 UTC

    If it's a method that is intended to return a boolean result then I would return a boolean. I only use chaining on methods that don't return a result.

    If a false result is intended to indicate an error, I would throw an exception instead - so I wouldn't need to store $result.

    For the style of code I write this problem wouldn't be an issue.

    (also, the $object=undef is redundant. It would be collected at the end of scope regardless.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://265307]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-11-24 07:16 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (345 votes). Check out past polls.