...
| Code Block | ||||
|---|---|---|---|---|
| ||||
our $BufferSize = 512;
# ...
my $nblocks = 1 + (($nbytes - 1) >> 9); # because $BufferSize = 512 = 2^9
|
The programmer might assume that " everyone knows $BufferSize equals 512 " and that right-shifting 9 bits is the same (for positive numbers) as dividing by 512. But if $BufferSize changes to 1024 on some systems, the subsequent expression must also be updated and can be overlooked easily. This makes modifications of constants difficult and error prone.
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
my $nblocks = 1 + (($nbytes - 1) / $BufferSize;
|
...
Tool | Diagnostic |
|---|---|
Perl::Critic | ValuesAndExpressions::ProhibitMagicNumbers |
Related Guidelines
Bibliography
...