Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Advice please on variable naming style in modules

by isync (Hermit)
on Aug 21, 2012 at 17:33 UTC ( #988803=perlquestion: print w/ replies, xml ) Need Help??
isync has asked for the wisdom of the Perl Monks concerning the following question:

Is there some standard on how to name variables in nested modules? Again and again, I am unsure whether I should name my variables this way or the other way round.

One style is that the local variable of a module is called $self or $this. Seems to be the common style.
So far so good. A problem arises when I use the local-module-var of the next level up within a nested module one level below. How should I name the former $self here? $toplevel_self? If I go down this path, I end up with some modules where I refer to a specific var by $self (probably the top-level module) and in other parts of my app, I name this var $that_toplevel_self, $parent or similar. This is especially awkward when the top-level $self contains some global config vars for my app.

So, is there a good argument for always naming my var $self/$this? Or is the other style I exemplify below equally sane, or even more helpful in a nested modules environment? Any hints which style I should use to adhere to "good coding style", or readability, when other developers join my projects?
Some pseudo-code to explain what I am talking about:
package App; sub new { my ($class, %args) = @_; my $self = bless { %args }, $class; $self->{widget} = App::Widget->new( $self ); return $self; } package App::Widget; my( $class, $self ) = @_; my $widget = bless { }, $class; $self->{menu} = App::Widget::Menu->new( $self, $widget ); return $self; } package App::Widget::Menu; sub new { my( $class, $self, $widget ) = @_; my $menu = bless { }, $class; # let's do something with %{ $self } here; $self->{some_arg} = 1; $self->{menu}->BackgroundColor(222,222,222); }

Comment on Advice please on variable naming style in modules
Download Code
Re: Advice please on variable naming style in modules (role > pos)
by tye (Cardinal) on Aug 21, 2012 at 18:21 UTC

    I tend to use $me not $self. Then the parent might become $dad or $mom.

    But even better is to describe the role rather than just the position in some hierarchy. So call the App $self only inside the App package and call it $app everywhere else. Call the Widget $self only in the Widget class and call it $widget everywhere else. Call the Menu $self only in the Menu class and call it $menu everywhere else.

    my( $class, $self ) in your examples looks mostly confusing to me. I'd much rather see "return $app;" so I will more quickly realize that the Menu c'tor is returning a container, not the new menu. Actually, it looks like you even managed to confuse yourself here because the Widget c'tor is supposed to return the new $widget, not the App that is stored in the (confusingly named) $self.

    - tye        

      Yes, your comment is what I hoped for is (one) good practice: $self only in the class itself, everywhere else according to function...

      (And you are right: managed to confuse myself in the example, and edited the original post: App::Widget now returns $widget...)
Re: Advice please on variable naming style in modules
by Anonymous Monk on Aug 21, 2012 at 19:04 UTC
    Be. Consistent.
      Good. Advice. !
Re: Advice please on variable naming style in modules
by Jenda (Abbot) on Aug 21, 2012 at 21:41 UTC

    Your variable naming is quite strange. $self should be the object whose method you are writing, not some parameter. That is within the App package it's the application, within App::Widget it's the widget and within App::Widget::Menu it's the menu. And you, generally, should not access the insides of the App from within an App::Widget::Menu method.

    If I understand things right, you pass an app to widget constructor and both app and widget to menu. so the code should be

    package App; sub new { my ($class, %args) = @_; my $self = bless { %args }, $class; $self->{widget} = App::Widget->new( $self ); return $self; } package App::Widget; my( $class, $app ) = @_; my $self = bless { }, $class; $app->{menu} = App::Widget::Menu->new( $app, $self); return $self; } package App::Widget::Menu; sub new { my( $class, $app, $widget ) = @_; my $self = bless { }, $class; $app->{some_arg} = 1; $app->{menu}->BackgroundColor(222,222,222); return $self; }

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Could be I was misleading you by a typo: App::Widget returns $widget, App::Widget::Menu returns $menu.

      The thing is, both App::Widget and App::Widget::Menu at times want to be able to access settings stored in App's $self. You can use a jump-the-hoops-method like my $self_of_the_topmost_level = wxTheApp->GetTopWindow(); (in a Wx app) to get that.

      Question is: what's a sane name for $self_of_the_topmost_level when it resides next to a "local" $self?

        What's "self" to you is isync to me. If the menu needs access to the top window, it should have a $top_window. The fact that within the methods of the top window the same reference will be in a variable called $self is irrelevant. To the method within the menu it's $top_window, not $self.

        <!-- Node text goes above. Div tags should contain sig only -- >

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (13)
As of 2014-09-19 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (137 votes), past polls