Avoid using Perl-style special variables

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: ruby-code-style/no-cryptic-perlisms

Language: Ruby

Severity: Notice

Category: Code Style

Description

The rule ‘Avoid using Perl-style special variables’ is important for improving the readability and maintainability of your code. Perl-style special variables, such as $0, $1, and $_, while powerful, can make your code less readable and harder to understand, especially for developers unfamiliar with Perl or its influence on Ruby. They can also introduce subtle bugs due to their global nature and the special behavior associated with them.

To avoid violating this rule, you can use the more descriptive aliases provided by the English library. This library, which is part of Ruby’s standard library, provides human-readable names for Perl-style special variables. For example, instead of using $& to get the string matched by the last successful pattern match, you can use $MATCH.

Here’s a compliant code example: Instead of $_, you can use $LAST_READ_LINE. Instead of $!, use $ERROR_INFO. This makes your code more self-explanatory and reduces the potential for confusion. Example:

require 'English'
puts $LAST_READ_LINE
puts $ERROR_INFO

This practice significantly enhances the readability of your code and makes it more accessible to developers who are not familiar with Perl-style variables.

Non-Compliant Code Examples

$! = ' -- '
$@ = ' -- '
$; = ' -- '
$, = ' -- '
$/ = ' -- '
$\ = ' -- '
$. = ' -- '
$_ = ' -- '
$> = ' -- '
$< = ' -- '
$$ = ' -- '
$~ = ' -- '
$* = ' -- '
$& = ' -- '

Compliant Code Examples

require "English"

$OUTPUT_FIELD_SEPARATOR = ' -- '
"Lorem ipsum dolor sit amet" =~ /dolor/
print $POSTMATCH, $PID, "\n"
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: rtrieu/product-analytics-ui-changes