Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: Pointers and References

by Bod (Parson)
on Nov 23, 2020 at 21:40 UTC ( [id://11124082]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Pointers and References
in thread Pointers and References

Where I want to explicitly show that a variable is deliberately global, I declare it with our.

Replies are listed 'Best First'.
Re^5: Pointers and References
by GrandFather (Saint) on Nov 23, 2020 at 22:02 UTC

    I almost never use our. Usually when I say global I mean global to the file and I might prefix the variable name with a g. our tells you something is "global" where you declare the variable, but it doesn't tell you that where you use it. You tend to use a variable much more often than you declare it so the ugliness of a g prefix is an immediate and obvious reminder at the point of use that this is a global variable.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Your coding style is peculiar, probably C'ish? ;)

      > our tells you something is "global" where you declare the variable, but it doesn't tell you that where you use it.

      I have problems to follow. our $var creates a lexically scoped alias to a package variable $PKG::var whith PKG for the current __PACKAGE__ . If you are afraid to loose count "where you use it" just limit the scope, much the same way you do with private vars declared with my.

      > of a g prefix is an immediate and obvious reminder at the point of use that this is a global variable.

      Then maybe consider full qualification instead of a g_ prefix.

      Using $PKG::var is IMHO much clearer, and $::var is a handy shortcut for $main::var

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

        C++ish I'd accept (but see the list of languages I've worked with on my home node).

        Perhaps I was being too specific when I said "our tells you something is "global" where you declare the variable". I was more intending to suggest that using an identifier over a wide scope puts more onus on the identifier to convey meaning and that any amount of "documentation" at the point of declaration doesn't help at the point of use. A simple g prefix is a heads up that this variable has large scope: beware, here be dragons. Most of the time the extra markup to convey that the variable hails from some package or another is unnecessarly heavy handed and doesn't add much value anyway.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re^5: Pointers and References
by jcb (Parson) on Nov 29, 2020 at 05:23 UTC

    I use a simple convention: local variables have names in all_lowercase, global variables have names with an Initial_capital, and constants or quasi-constants have names that are ALL_UPPERCASE.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found