Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

What's the correct and succinct way to refer to object attribute in (informal) description?

by Anonymous Monk
on Jun 10, 2024 at 08:41 UTC ( [id://11159869]=perlquestion: print w/replies, xml ) Need Help??

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

When writing to someone, I think it's perfectly OK to say e.g. "hey, have a look at HTML::Element::tag() method". But what about attributes?

  • '_tag' attribute of an instance of HTML::Element class? (too wordy)
  • $HTML_Element_obj->{'_tag'}? (is actual class name clear from instance name?)
  • HTML::Element->{'_tag'}? (short, but neither pseudocode nor correct Perl)

(Suppose it's blessed hashref but not HTML::Element as in example above and there are no getter/setter for an attribute.)

  • Comment on What's the correct and succinct way to refer to object attribute in (informal) description?
  • Select or Download Code

Replies are listed 'Best First'.
Re: What's the correct and succinct way to refer to object attribute in (informal) description?
by choroba (Cardinal) on Jun 10, 2024 at 08:56 UTC
    Peeking into attributes breaks encapsulation. It shouldn't be done under normal circumstances, so I'd consider it OK one needs to be wordy to tell others to do it.

    With the new class feature, objects are no longer blessed hash references and their attributes (now called fields) can't be accessed without accessors, anyway.

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use 5.039.010; use experimental 'class'; class My { field $x :param :reader; } my $o = 'My'->new(x => 10); say $o->x; # No other way.

    Update: Fixed a typo, thanks LanX.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      > Peeking into attributes breaks encapsulation

      That's true, and I always defended the argument.

      But from my (limited) experience with Python it seems they generally don't care.

      Why is it so? Do they have a good a posteriori mechanism to override the attribute with accessor logic to fix potential problems?

      Or do they just succeed by being less anal?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        StackOverflow discussion about public attributes in Python.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        Python also has no true read only values ("constants" are named with all caps but there's nothing mechanically in the language itself stopping code from changing the values they point to) so my days of not respecting their design chops have certainly come to a middle.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: What's the correct and succinct way to refer to object attribute in (informal) description?
by LanX (Saint) on Jun 10, 2024 at 14:26 UTC
    I would go for a variation of
    • $HTML_Element_obj->{'_tag'}
    But consistently shorten HTML::Element to something like HE, to improve readability
    • $HE_obj->{'_tag'}
    Maybe clarifying that
    • $HE_obj = HTML::Element->new(...)
    NB: I was assuming that you have to document objects from multiple classes in the same doc.

    Otherwise just go for $obj or $elem

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re: What's the correct and succinct way to refer to object attribute in (informal) description?
by cavac (Parson) on Jun 12, 2024 at 14:52 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11159869]
Approved by choroba
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-09-17 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (22 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.