Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Only significant change is to sentence starting: The programmer might assume that "everyone knows $BufferSize equals 512" and that right-shifting 9 bits

...

This noncompliant example first provides a constant value $BufferSize, set to 512. This constant can later be used to buffer data read in from a file. But then the code example defeats the purpose of defining $BufferSize as a constant by assuming its value in the subsequent expression:

Code Block
bgColor#FFcccc
langperl
our $BufferSize = 512;

# ...

my $nblocks = 1 + (($nbytes - 1) >> 9); # because $BufferSize = 512 = 2^9

The programmer 's assumption underlying this code is might assume that "everyone knows that $BufferSize equals 512," and that right-shifting 9 bits is the same (for positive numbers) as dividing by 512. However, 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.

...