Posted: 20 Jun 2005 12:16
what's with the magic numbers? we're aiming to reduce the number of undefined integers rather than increasing them
The place to talk about Transport Tycoon
https://www.tt-forums.net/
Who are you anyway?Jango wrote:we're aiming to reduce...
Code: Select all
#define MEANINGFUL_NAME 5
#define MEANINGFUL_NAME2 10
//...
for (int i=MEANINGFUL_NAME;i<MEANINGFUL_NAME2;i++){
//...
}
Code: Select all
for(int i=5;i<10;i++){
//...
}
It does *not* take up extra space, because on compile-time those names will be automatically replaced with the corresponding values by the compiler.bobingabout wrote:while the first takes up extra space... a simple comment stating what 5 and 10 mean are also good, but why use a dynamic number and take up extra memory for a number that never changes?
if its used several times, sure, a variable or constant would be ok, but if its just 1 or 2 times, why?
written by a true coderAsterix wrote:It does *not* take up extra space, because on compile-time those names will be automatically replaced with the corresponding values by the compiler.bobingabout wrote:while the first takes up extra space... a simple comment stating what 5 and 10 mean are also good, but why use a dynamic number and take up extra memory for a number that never changes?
if its used several times, sure, a variable or constant would be ok, but if its just 1 or 2 times, why?
And even if its only used a few times, it makes the code easier to read for those that didn't write it. (on a sidenote, I dont use enough names, myself)
Code: Select all
#define MEANINGFUL_NAME 10
y = (x < MEANINGFUL_NAME) ? 4 : 5 // 4 = something, 5 = something else
Clearly someone with more manners than you. I've done my fair share of OpenTTD coding as well, and I'm merely helping you to write code which you'll understand in a few years time. Besides, if you submit a patch like that, Tron will probably kill you. It's all very well having clever code (using bitwise operators and the like), but in reality it's MUCH slower to read.Sirkoz wrote: Who are you anyway? :Shocked:
don't the coding guidelines say to use enum instead of #define? soDaleStan wrote:SirkoZ, if you want this patch committed, use egCode: Select all
#define MEANINGFUL_NAME 5 #define MEANINGFUL_NAME2 10
Code: Select all
enum {MEANINFUL_NAME = 5, MEANINFUL_NAME2 = 10};
Code: Select all
#define MEANINGFUL_NAME 10
y = (x < MEANINGFUL_NAME) ? 4 : 5 // 4 = something, 5 = something else
i was refering specificaly to this part of the wiki:Thief^ wrote:in actual c/c++ coding practice enums should only be used when a variable can take more than one value representing different states.
As the example is:y should be an enum type with "something" and "something_else" defined as 4 and 5, but MEANINGFUL_NAME is a constant, not a variable state (it's never assigned to x, only compared to it) so should be a #define or const int (and indeed is).Code: Select all
#define MEANINGFUL_NAME 10 y = (x < MEANINGFUL_NAME) ? 4 : 5 // 4 = something, 5 = something else
Code: Select all
* Use simple enums for holding weakely related constants like this:
enum {
NUMBER_OF_BIRDCAGES = 10,
MAX_BIRDS_PER_BIRDCAGE = 3,
}
...
* Use enums instead of #define
Code: Select all
if ((i->pct_transported[j] > PERCENT_USED) && CHANCE16I(1, CHANCE_USED, r))
new += ((RandomRange(INCREASE_A) + INCREASE_B) * old) >> 8;
if ((i->pct_transported[j] < PERCENT_USED) && (i->pct_transported[j] > 0) && CHANCE16I(1, CHANCE_USED, r) && (old > MIN_RATE))
new -= ((RandomRange(INCREASE_A) + INCREASE_B) * old) >> 8;
if ((i->pct_transported[j] == 0) && CHANCE16I(1, CHANCE_UNUSED, r) && (old > (MIN_RATE + 1)))
new += INCREASE_UNUSED - (RandomRange(MIN_RATE));
Code: Select all
enum {
CHANCE_USED = 50,
CHANCE_UNUSED = 10,
INCREASE_A = 50,
INCREASE_B = 10,
INCREASE_UNUSED = 2,
MIN_RATE = 5,
PERCENT_USED = 153
};
Code: Select all
int CHANCE_USED = 50;
int CHANCE_UNUSED = 10;
int INCREASE_A = 50;
int INCREASE_B = 10;
int INCREASE_UNUSED = 2;
int MIN_RATE = 5;
int PERCENT_USED = 153;
new = old = i->production_rate[j];
if ((i->pct_transported[j] > PERCENT_USED) && CHANCE16I(1, CHANCE_USED, r))
new += ((RandomRange(INCREASE_A) + INCREASE_B) * old) >> 8;
if ((i->pct_transported[j] < PERCENT_USED) && (i->pct_transported[j] > 0) && CHANCE16I(1, CHANCE_USED, r) && (old > MIN_RATE))
new -= ((RandomRange(INCREASE_A) + INCREASE_B) * old) >> 8;
if ((i->pct_transported[j] == 0) && CHANCE16I(1, CHANCE_UNUSED, r) && (old > (MIN_RATE + 1)))
new += INCREASE_UNUSED - (RandomRange(MIN_RATE));