Re: Formatting variable names
by hippo (Bishop) on Nov 29, 2020 at 11:38 UTC
|
all lower case with an underscore sometimes between words
This is called snake case.
capitalising the first letter of each word other than the first
This is called camel case.
I'm wondering how you do it, why you do it that way and if you think it matter a great deal.
I use snake case because it is more legible to me. It doesn't matter overly so long as you are consistent within any one body of code.
| [reply] |
|
"I use snake case because it is more legible to me."
Note that snake case has always been the de-facto, unofficial standard for Perl code since forever. I develop in numerous languages, and typically stick with what the language norm is. That way, others who read, use or work on my code will be familiar with whatever language the code is written in. If they add to my code, they'll typically write using the language 'standard', and I'm far less likely to get variations.
There's not much worse than having a source file with functions or variables where some are like_this and others are likeThis or even LikeThis. Inconsistency makes code harder to read, and harder to understand when looking at it from a distance.
In Perl, AllUpperCamelCase represents a package name. snake_case represents variables, functions and methods. ALL_UPPER_SNAKE or ALLUPPER is often used for env vars and constants.
| [reply] [d/l] [select] |
|
| [reply] |
|
These two are joined by several others in having colloquial names:
Naming convention, multiple-word identifiers
I've seen kebab-case used in Scheme. It is also convenient in domain names, though there are some sound arguments in favor of registering both the hyphenated and non-hyphenated version of a given domain. One reason is that it's hard, in an audio advertisement, to specify the hyphen without making that the most memorable part of the domain.
| [reply] |
|
Re: Formatting variables
by LanX (Saint) on Nov 29, 2020 at 11:40 UTC
|
you might want to have a look into Damian's "Perl Best Practices", he's elaborating a naming convention there.
Personally I' diverging from his _ref appendices for references and prefer prefixes like $a_list = \@list for arrays and so on ( $h_ , $c_ ... )
Reasons:
- I want to know what type of ref I'm looking at, _ref is too generic
- It's a sigil in disguise, hence it should come upfront
- It's shorter
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] [d/l] [select] |
Re: Formatting variables
by stevieb (Canon) on Nov 29, 2020 at 15:44 UTC
|
crmr_5 Read access to business unit 5
crmw_3 Write access to business unit 3
crmn_1 Read and write notes access...
I would do something like:
my $ro_business_unit_5;
my $wo_business_unit_3;
my $rw_notes;
I would much rather have long variable names that I can absolutely, unambiguously know what they represent, than having to wade through code trying to find the original assignment. To boot, typically if someone is using non-descriptive variable names, they likely are naming functions ambiguously as well, which makes it doubly as hard to sort out the true meaning behind the variable name.
Some people say "I have to type more if I use long variable names!", to which I say, get a better editor/IDE that does variable and function auto-populate. If they don't want to do that, then they can contemplate later down the road how much time they would have saved by typing the long var names as opposed to trying to look through thousands of lines of code figuring out which variable does what six months after they last looked at the code. | [reply] [d/l] [select] |
|
Seconded; with something like company mode you just have to pick from a list or hit a key to cycle through the alternatives.
Also whenever you have a family of variables (e.g. named with suffixen or indexen) it's a red flag that you probably want a data structure (as was mentioned above but linking to MJD's article with more commentary).
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] |
|
Some people say "I have to type more if I use long variable names!"
Even without a better editor/IDE, slightly longer typing variable names seem like a reasonable trade off for easier to maintain code in the future - be that next week or next decade.
| [reply] |
Re: Formatting variables
by GrandFather (Saint) on Nov 29, 2020 at 19:57 UTC
|
Snake versus Camel case is pretty much a personal thing and tends toward religious fervor. prefix and postfix annotations of any sort are generally much less important than getting the identifier name "right". Often prefixes or postfix annotations mess up good names and add clutter. Where annotations are used they are often an institutional convention or coding practice.
As a personal thing I use a k prefix for global constants and that's about the only annotation I use in Perl. In other languages (C++ mostly) I use g as a prefix for global static variables - those almost never leak out of a compilation unit. Most compilers for languages that care are pretty good at reporting type mismatches so there's no point decoration variable names to indicate the variable's type - that just adds clutter and slows down understanding. Good variable names are essential. Decorations generally just distract attention from the work being done.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
| [reply] |
Re: Formatting variables
by LanX (Saint) on Nov 29, 2020 at 12:59 UTC
|
>
crmr_5 Read access to business unit 5
crmw_3 Write access to business unit 3
crmn_1 Read and write notes access...
looks like a nested structure to me, TIMTOWTDI
either
$cmr_access{r}[5]
or
$cmr_access[5]{r}
or
use constant { NOTE=>0,READ=>1,WRITE=>2 };
$cmr_access[5][READ]
it really depends on the use case
| [reply] [d/l] [select] |
|
looks like a nested structure to me, TIMTOWTDI
These are actually HTML form element names, not Perl variables but they are dynamically generated by a Perl script like so:
$query=$dbh->prepare("SELECT idBusiness,emaillist,private,name,title,d
+escription FROM Business ORDER BY metric");
$query->execute();
while (($id,$em,$private,$text,$title,$desc)=$query->fetchrow_array())
+ {
$list_color=$em?'black':'slategray';
($chk, $sub, $crm, $warmth)=$dbh->selectrow_array("SELECT Business
+_idBusiness, subscribe, CRMaccess, warmth FROM Business_has_Contact W
+HERE Business_idBusiness = $id AND Contact_idContact = $data{'contact
+'}");
if ($chk) {
$check=' checked';
$dis='';
} else {
$check='';
$dis=' disabled';
}
$warmth = 0 unless $warmth;
$crmr_chk = $crmw_chk = '';
$crmr_chk = ' checked' if $crm;
$crmw_chk = ' checked' if $crm eq 'W';
$subscribe=$sub?' checked':'';
$text="<i>$text</i> <span class=\"privatelist\">[private]</span>"
+if $private;
print "<tr>\n<td><input type=\"checkbox\" name=\"bus_$id\" onChang
+e=\"subscribe(this,$id);\"$check>\n";
print "<input type=\"checkbox\" name=\"sub_$id\"$subscribe$dis></t
+d>\n";
print "<td title=\"$title\" style=\"color:$list_color\">$text</td>
+\n";
print "<td style=\"text-align:center\"><input type=\"checkbox\" na
+me=\"crmr_$id\" onClick=\"crmAccess(this, $id);\"$crmr_chk>\n";
print "<input type=\"checkbox\" name=\"crmw_$id\" onClick=\"crmAcc
+ess(this, $id);\"$crmw_chk></td>\n";
print "<td><img src=\"/images/site/warmth$warmth.png\" style=\"wid
+th:28px\"> </td>\n" if $chk;
print "</tr>";
}
This is part of a 4500+ line script that was originally written a long time ago and has been added to and modified lots over the years. Hence lack of white space and other poor legibility on some of the older bits of code. Nowadays I insert spaces either side of = and a space after a comma.
| [reply] [d/l] [select] |
|
Please don't be offended, but after looking at your code I'd really appreciate if you stopped complaining about the way this site was implemented.
| [reply] |
|
Re: Formatting variables
by perlfan (Vicar) on Nov 30, 2020 at 18:59 UTC
|
I don't use "camelCase" because it looks too much like Java, regardless of it's name. I have found that the tendency for one to use camelCase often accurately indicates their less advanced relationship with idiomatic Perl. It's not 100% accurate, but accurate enough. An additional reason I don't arbitrarily capitalize letters in variable names is that this makes it meaningful when I do, so it's an additional degree of freedom for conveying information that you do not get when you just arbitrarily nameStuffLikeThis. | [reply] |
|
| [reply] |
Re: Formatting variables
by harangzsolt33 (Chaplain) on Dec 01, 2020 at 06:03 UTC
|
Programming is my hobby, so when I write code, I don't have a boss to tell me "This is how you're going to do it or you're fired." But I like to write my variables in this format:
$VariableName
SubName();
Sometimes, I switch to all caps when I use a global variable or an important value.
my $DEBUG = 1;
my $GLOBAL_VARIABLE_NAME = 'hello';
If there's a function or sub that is part of a larger group of subs or "class," I might use an underscore character to divide the name:
Chart_DrawGrid();
Chart_Open();
Chart_Close();
Chart_Move();
If I need to create a "low-level function" that I only call from within certain functions or a function that is called many-many times and needs to be high performance, then I start its name with an underscore:
_FormatName();
_SaveValues();
_RecalculateSize();
For braces, each opening and closing brace goes on a new line, and indentation is always 2 spaces (I don't like to use tabs). In for loops, I like to use single-letter variables in lowercase that starts with $i, $j, $k... I like to avoid using $l because the lowercase L looks like a number 1. That gets confusing very quickly, so I will pick a variable name like $g or $f or something else instead of $l.
for (my $i = 0; $i < 43; $i++)
{
...
}
| [reply] |
Re: Formatting variables
by Anonymous Monk on Nov 29, 2020 at 23:19 UTC
|
As a poor soul who has digested a tremendous amount of "strange and unfamiliar source-code written by other people long gone," the only thing that I can ask of you is that you, please, be as consistent as you can. You are talking to humans at this point, not language compilers. These "subsequent souls" are poring over your source-code without you (RIP?), and they appreciate your clues. | [reply] |