1. Perl
  2. reading material

Make sure you give the variable a proper name

A good name for the variable will make the program easier to read.

This is my personal opinion, but I would like to write down what I think about how to name variable.

  1. Make variable names meaningful.
  2. Variable names are all lowercase and word joints use _ (underscore).
  3. How detailed the variable name should be
  4. Variable names that may be omitted
  5. How to name variable that represent boolean values
  6. Array naming method

(1) Make the variable name meaningful.

The hardest thing to read in programming is the intent of the person who wrote the code.

Proper variable names make it easier to convey your intentions.

(a) When variable have meaning

# This variable stores the sum of something
my $total;

# This variable represents some offset.
my $offset;

# This variable represents the maximum number of clients
my $max_client

(b) When variable have no meaning

# I don't know anything
my $p;
my $q;
my $k;

The only concern for computers is whether variable names can be distinguished, but for humans it is important what the variable names mean.

(2) Variable names are all lowercase, and word joints are _ (underscore.)

Do not use uppercase letters in variable names, only lowercase letters. Use underscores at word joints.

my $user_name;
my $search_word;
my $max_database_connection;

This is a Perl convention, not compulsory, but if you're writing code in Perl, you'll probably want to keep it. I think there is less confusion for the user.

On the contrary, when programming with java, do not bring in the perl convention and write it as userName.

(3) How detailed the variable name should be

I think that how detailed the variable name should be based on whether the meaning can be distinguished within the scope.

(a) If you can clearly understand the intention, you can use a short variable name

sub sum {
  # Represents the total value
  my $total;
}

(b) If it is necessary to distinguish, use a variable name that can distinguish the meaning depending on the situation

For example, if you had a function that added odd sums and even sums, give each sum an appropriate name.

sub sum {
  # Odd sum
  my $odd_total;
  
  # Even sum
  my $even_total
}

(c) If you can clearly understand the intention with for statement, you can use a short variable name such as $i

for my $i (0 .. @nums - 1) {
  ...
}

(d) If there is a possibility that the intention may be difficult to understand in for statement, use a variable name that can distinguish the meaning

my $data_table = [[1, 2, 3], [2, 4, 6]];
for my $row_num (0 .. @$data_table - 1) {
  for my $colon_num (0 .. @{$data_table[$row_num]} ―― 1) {
    ...
  }
}

(4) Variable names that may be omitted

You may omit anything that makes sense by convention. If the variable scope is narrow, you can omit it. When omitting it, it is easy to understand to leave the first few characters.

Variable names that can be understood even if omitted

my $num; # number
my $len; # length;
my $char; # charactor;

(5) Naming method for variable that represent boolean values

Here are some examples of variable names that represent boolean values.

# Safe (is + adjective)
my $is_safe;

# Switched on (noun + preposition)
my $switch_on;

# Loading request (noun + present participle)
my $request_loading;

# Writing is complete (noun + past participle)
my $write_done;

# Check successful (noun + noun)
my $check_success;

# The state is ok. (Noun + noun)
my $status_ok;

(6) Sequence naming method

If you want to name the array, add an s at the end.

# How to add s to the end
my @members;

for my $member (@members) {
  # ...
}

Related Informatrion