Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
langperl

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
bgColor#ccccff
langperl

my $nblocks = 1 + (($nbytes - 1) / $BufferSize;

...

Tool

Diagnostic

Perl::Critic

ValuesAndExpressions::ProhibitMagicNumbers

Related Guidelines

Bibliography

...

...

[

...

...

...

 

...

Image Added Image Added |http://search.cpan.org/~elliotjs/Perl-Critic-1.116/] [ProhibitMagicNumbers|http://search.cpan.org/dist/Perl-Critic/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitMagicNumbers.pm] \[[Wall 2011|AA. Bibliography#Manpages]\] [perlfunc|http://perldoc.perl.org/perlfunc.html]Image Removed      02. Expressions      Image Modified