Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Monks,

I am just writing down some thought here below. If you want to take it serious, be my guest... ;-)

I have been wondering on the naming of variables. Not so much what to call them (Non ambigious, descriptive, not to long and to to short) but wheter to use plurals for different types of variables.

An array holds a list of variable values of the same context, say : database fields. The context of the program could lead us to suspect that all is database related, and thus you choose the name fields. This is logic. Now we come to hashes. A hash is nothing more then a noble array, but the context of the hash could be much wider. If you want a hash with database fields, and a context specifier in the value of each key, would we then call the hash 'field' or 'fields'. I agree that the entire hash contains fields, and it should thus be plural, but when we put the hash to use, field is more appropriate. (eg: $fields{ID} or $field{ID}).

Hmm... Now the ideal plural for the array is no longer that logic, cause when I start to use the array litteraly in my code ($fields[0]) and there is but one thing in that item, the naming has suddenly become ambigious.

It seems that one has to think deeper on his variable naming for list storing variables (hash or array) then seems apparent. If you store field names in an array, and you wish to use the values distinct in your code, you must call the array @field, cause it makes more sence. If you wish to apply the entire list (eg: foreach my $field (@fields) { ... }) You must name you array @fields.
With the use of hashes I think avoiding plurals in naming them is best, since you won't just use the keys of a hash for lists in foreach, you will actualy dive deeper, and plurals will (mostly) become ambigious and non-descriptive...

*sigh*

UPDATE : Saw the flaw, ChOas pointed out, and took over simon.proctor's syntax...

er formait hyarya.
"Field experience is something you don't get until just after you need it."

In reply to Plural variable naming (or not?) by Sinister

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-23 10:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found