in reply to Filling an array
In this case, you just happened to be able to fill up the array with ten ones. Later on, you had to change that to 100. As a programmer, you should count on that happening and plan accordingly.
What's more, the actual number of items in the list needs to be made clear. If I'm trying to read your code, and I'm wondering how many ones there are, I have to count. That's not fun. Never mind you having to type 100 ones: What about the next person to read your code (who could be you) having to count those 100 ones.
Why are there 100 ones? I don't know, and your code doesn't tell me. Is it the number of wangos that you're processing? Then comment it:
Better yet, define a variable that tells how many wangos you can process:my @arr = (1) x 10; # We can process 10 wangos.
Best of all, define a constant that is easily changeable AND describes what you're doing:my $max_wangos = 10; my @arr = (1) x $max_wangos;
An excellent book that these concepts, and many many more, is Steve McConnell's excellent Code Complete. No serious programmer should be without it.use strict MAX_WANGOS => 10; my @arr = (1) x MAX_WANGOS;
xoxo,
Andy
--
<megaphone>
Throw down the gun and tiara and come out of the float!
</megaphone>
|
|---|