On Thu, Aug 5, 2010 at 00:16, HACKER Nora <[email protected]> wrote:
> Hi,
>
> Additional noob question: What would be wrong with
>
> $version = '0'.$version;
snip
Do you always want one 0 in front of version? What if the version is
9? Shouldn't it be "009" then? You could write:
#version should be three characters long and padded with 0s
if ($version < 100) {
if ($version < 10) {
$version = "00$version";
} else {
$version = "0$version";
}
}
Or you could write:
#version should be three characters long and padded with 0s
$version = sprintf "%03d", $version;
Now, imagine we are two years in the future and your boss comes to you
and says that the version field is changing from a three character
field to a five character field. Which change would you rather make:
#version should be five characters long and padded with 0s
if ($version < 10_000) {
if ($version < 1_000) {
if ($version < 100) {
if ($version < 10) {
$version = "0000$version";
} else {
$version = "000$version";
}
} else {
$version = "00$version";
}
} else {
$version = "0$version";
}
}
or
#version should be five characters long and padded with 0s
$version = sprintf "%05d", $version;
Which do you think has a higher chance of a typo or other subtle bug
not being caught in testing?
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/