Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Does one encapsulate a class from itself?

by toma (Vicar)
on Mar 07, 2008 at 08:14 UTC ( #672715=note: print w/ replies, xml ) Need Help??


in reply to Does one encapsulate a class from itself?

The important thing is to get used to building object-oriented modules, and make it part of your normal coding style. Build them in any way that seems easy and natural. If you need performance, optimize.

I tried several alternatives, and then settled on a blessed hash for most of my objects. I don't use the getter/setter functions internally, except when I think the object data access needs a more future-proof implementation.

For your matrix example, I would be tempted to access the data even more directly, bypassing the object reference altogether.

{ my $data= $self->{dataref}; foreach my $x (0..$max_x) { $data->{$x}{$y} = $new_value; } }
I use the smallest scope possible for the lexical variables that refer to chunks of my object. These lexical variables have descriptive names, and for me the code is easier to read. This approach reduces the length and complexity of my mathematical and string expressions, especially for setter functions.

I haven't seem much of a performance difference when I use the lexicals to refer to a sub-object, but I have sometimes had speed problems with getter and setter function calls.

There are good arguments for both ways, though. Since it depends on what you are doing, do whatever seems best, and it will be fine. If it isn't, change it!

It should work perfectly the first time! - toma


Comment on Re: Does one encapsulate a class from itself?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://672715]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-07-30 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (230 votes), past polls