Qbasicnews.com
October 25, 2021, 10:17:42 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Good practice? Bad practice? You tell me?  (Read 3386 times)
neuauld
New Member

Posts: 4


« on: July 12, 2006, 04:06:21 PM »

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
Logged
Rattrapmax6
__/--\__
*****
Posts: 2577



WWW
« Reply #1 on: July 12, 2006, 04:31:16 PM »

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:
Logged

Kevin (x.t.r.GRAPHICS)

neuauld
New Member

Posts: 4


« Reply #2 on: July 12, 2006, 04:45:32 PM »

Quality stuff.

Thanks for the speedy response mate  :ninja:
Logged
RyanKelly
Forum Regular
**
Posts: 109



WWW
« Reply #3 on: July 13, 2006, 12:14:42 AM »

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
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #4 on: July 14, 2006, 10:11:47 PM »

Quote from: "neuauld"
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.

*****
Logged
torstum
Member
*
Posts: 62


« Reply #5 on: July 15, 2006, 09:50:25 PM »

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.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #6 on: July 16, 2006, 08:57:31 PM »

Torstum, excellent advice.
*****
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #7 on: July 17, 2006, 06:11:00 AM »

A good compiler should precalculate literal expressions involving constants, so it's fine to use them, it won't slow down your program.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
neuauld
New Member

Posts: 4


« Reply #8 on: July 19, 2006, 02:07:49 PM »

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.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!