Qbasicnews.com

Full Version: Good practice? Bad practice? You tell me?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I was wondering, is it bad practice to use operators in an array. I'm thinking for inventories in games it would be handy. Like, using arrayexample$(25+4) and such, where using arrayexample$(29) would not be applicable because you simply wouldn't know it would be 29 or something (so the array would probably be arrayexample$($counter+1). Or is there a more efficient way to tackle this?

Cheers,
Jamie
x
Nah, it's not bad pratice to do such a thing.... It's often clearer, eg:
Code:
DIM AS INERGER FB_Image(800 * 600 * 4 + 4)

As opposed to calculating, then putting:
Code:
DIM AS INTEGER FB_Image(1280004)

The first makes it easy to look back and see that the image is 800x600.. (FreeBasic example, though, it shows it's usefull to have operators in arrayes at times... Wink )

And like you stated, if you had:
Code:
DIM ArrayName(Counter + 1)

All the same.... It's not bad practice to use it imho.... :winkwink:
Quality stuff.

Thanks for the speedy response mate :ninja:
I second Rattrapmax6. In so far that it conveys an idea of what the code is supposed to do and how you've designed your program it makes sense. In the area of performance, it is always a matter of context. Inside of an often repeated area, like a loop or a frequently called subroutine it is usually a good idea to keep the number of calculations to a minimum, but in a portion of code that isn't executed often there's no harm done.

For example,

for index= 0 to 999
g$(base+index)=lcase$(g$(base+index))
next index

might not be as preferable as

for index=base to base+999
g$(index)=lcase$(g$(index))
next index
Quote:So I was wondering, is it bad practice to use operators in an array. I'm thinking for inventories in games it would be handy. Like, using arrayexample$(25+4) and such, where using arrayexample$(29) would not be applicable because you simply wouldn't know it would be 29 or something (so the array would probably be arrayexample$($counter+1). Or is there a more efficient way to tackle this?
Cheers,
Jamie
x
I think that both of the following are considered as bad practice:
DIM array$(25+4)
DIM array$(29)

Looking at either one of these DIMs, I have no clue what 25 or 4 mean, nor what 29 means. They must mean something special because they are defining the size of the array.

Let's say, for example, that 25 means the maximum number of normal entries, and that 4 means the maximum number of overflow entries. Here's how I would code this:
const maxnorm = 25
const maxoflow = 4
DIM array$(1 to (maxnorm + maxoflow))

This will run the same as yours, but it's al lot more understandable. Plus, you can use the constants maxnorm and maxoflow when you need them later in the program, without using the literal constants of 25 and 4 again.

Also, note the DIM array$(1 to ....)
I specifically want to start the array from 1. If you don't code this explicitly, the lower bound of the array will start according to any specified OPTION BASE statement.

*****
One of the most useful lessons a programmer can learn: get rid of the "magic numbers".

That's why good programming practice means declaring all constants at the beginning of the program. 29 is a "magic number". It has no apparent meaning. Instead, one should define:

CONST ArcTotal = 29

if you're creating a table in which the index is an angle, or for example:

CONST DogTotal = 4

if you're creating an array of your pet dogs' names

DIM PetName$( 1 TO DogTotal )

Then, if one of your dogs gets puppies, all you have to change is the DogTotal constant to a new value, and your program will work all the same.
Torstum, excellent advice.
*****
A good compiler should precalculate literal expressions involving constants, so it's fine to use them, it won't slow down your program.
This is true, but it's not a real program. I just wanted to make sure you knew what I was referring to when I said operators in arrays, that's all.