Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coding_style: Document each's evilness #280

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions _development/coding_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ And the bad example:
my $circle_area = $radius ** 2 * 3.14159;
```

### Don't use each

`each` is evil, and although it can be used correctly, should be avoided. The problem is that each sort-of remembers the last element it iterated over, this has all sorts of nasty side effects.

Use `foreach` to iterate over `keys` instead.

Bad example:

```perl
while (my ($k, $v) = each(%h)) {
...
}
```

Good example:

```perl
foreach my $k (keys %h) {
my $v = $h{$k};
...
}
```

### Module header

A Perl module **must** start with a line like:
Expand Down
Loading