Improved smooth_economy patch

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Jango
Traffic Manager
Traffic Manager
Posts: 189
Joined: 28 Aug 2004 12:13

Post by Jango »

what's with the magic numbers? we're aiming to reduce the number of undefined integers rather than increasing them
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

Jango wrote:we're aiming to reduce...
Who are you anyway? :shock:

Look at the patch or rather patched industry_cmd.c + source and everything will be clear. :roll:
DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Post by DaleStan »

SirkoZ, if you want this patch committed, use eg

Code: Select all

#define MEANINGFUL_NAME 5
#define MEANINGFUL_NAME2 10
//...
for (int i=MEANINGFUL_NAME;i<MEANINGFUL_NAME2;i++){
//...
}
instead of

Code: Select all

for(int i=5;i<10;i++){
//...
}
The latter uses so-called "magic numbers" which are horrors when trying to change or debug existing code.
0 and 1 as magic numbers are sometimes excusable.
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser
User avatar
bobingabout
Tycoon
Tycoon
Posts: 1850
Joined: 21 May 2005 15:10
Location: Hull, England

Post by bobingabout »

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?
JPG SUX!!! USE PNG!!!
There are times when JPG is useful, TTD screenshots is not one of them. Please use PNG instead.

[/url]
Asterix
Engineer
Engineer
Posts: 121
Joined: 04 Apr 2005 18:17
Location: Germany

Post by Asterix »

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?
It does *not* take up extra space, because on compile-time those names will be automatically replaced with the corresponding values by the compiler. :roll:

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 ;) )
Jango
Traffic Manager
Traffic Manager
Posts: 189
Joined: 28 Aug 2004 12:13

Post by Jango »

Asterix wrote:
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?
It does *not* take up extra space, because on compile-time those names will be automatically replaced with the corresponding values by the compiler. :roll:

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 ;) )
written by a true coder :) - anyone who has tried to understand different sections of the ottd code will know that the sections that have been rewritten to use defines rather than meaningless numbers are MUCH easier to read. The main advantages arising from that are that it's easier to avoid bugs and it's quicker to code changes.

Whilst comments are good, defines are often subtler and slightly less verbose although just as meaningful. Having code littered with pointless comments often makes it more difficult to read.

Code: Select all

#define MEANINGFUL_NAME 10
y = (x < MEANINGFUL_NAME) ? 4 : 5 // 4 = something, 5 = something else
By using defines, you abstract away the numbers and concentrate on the actual code. With comments you leave the numbers there and they can be distracting.
Sirkoz wrote: Who are you anyway? :Shocked:
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.
User avatar
GoneWacko
Tycoon
Tycoon
Posts: 8680
Joined: 10 Jul 2002 15:08
Location: Enschede, The Netherlands
Contact:

Post by GoneWacko »

Just a bit of a pre-emptive warning:
Let's keep it civil :)
GoneWacko. Making [url=irc://irc.oftc.net/tycoon]#tycoon[/url] sexy and exciting since 1784.
Mek
TTDPatch Developer
TTDPatch Developer
Posts: 417
Joined: 13 Apr 2004 13:35
Location: Eindhoven, Netherlands
Contact:

Post by Mek »

DaleStan wrote:SirkoZ, if you want this patch committed, use eg

Code: Select all

#define MEANINGFUL_NAME 5
#define MEANINGFUL_NAME2 10
don't the coding guidelines say to use enum instead of #define? so

Code: Select all

enum {MEANINFUL_NAME = 5, MEANINFUL_NAME2 = 10};
User avatar
Thief^
Route Supervisor
Route Supervisor
Posts: 469
Joined: 10 Oct 2004 00:11

Post by Thief^ »

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:

Code: Select all

#define MEANINGFUL_NAME 10 
y = (x < MEANINGFUL_NAME) ? 4 : 5 // 4 = something, 5 = something else
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).

EDIT: btw, before you ask who *I* am, I'm a student who's just finished the second year of a BSc Games Programming course, in the middle of writing a custom full-3d physics engine, and preparing for an interview with a games company for my placement year.
Melt with the Shadows,
Embrace your destiny...
Mek
TTDPatch Developer
TTDPatch Developer
Posts: 417
Joined: 13 Apr 2004 13:35
Location: Eindhoven, Netherlands
Contact:

Post by Mek »

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:

Code: Select all

#define MEANINGFUL_NAME 10 
y = (x < MEANINGFUL_NAME) ? 4 : 5 // 4 = something, 5 = something else
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).
i was refering specificaly to this part of the wiki:

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
(i'm not saying i think this is the proper way to do things, but this is just what is writting on the wiki
User avatar
Thief^
Route Supervisor
Route Supervisor
Posts: 469
Joined: 10 Oct 2004 00:11

Post by Thief^ »

Fine, if that's what they say to do, but it's not strictly correct is all.
As long as there's a coding standard it'll be fine, (which there is :wink:)
Melt with the Shadows,
Embrace your destiny...
Jango
Traffic Manager
Traffic Manager
Posts: 189
Joined: 28 Aug 2004 12:13

Post by Jango »

and, bringing us back to the point, either way is better than magic numbers :)
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

Thank you for all of your constructive criticism - I have changed the code to use predefined values (now in industry.h within an enum statement).

Here it is "screenshot" from industry_cmd.c:

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));
And a "screenshot" from industry.h:

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
};
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

Here are the latest diffs/patches with the above code - from nightly/svn source 2466 (patches for industry_cmd.c and industry.h are separate...):
Attachments
industry-h_enum.patch
(370 Bytes) Downloaded 364 times
industry_cmd-c_enum.patch
(1.6 KiB) Downloaded 379 times
Last edited by SirkoZ on 23 Jun 2005 14:22, edited 2 times in total.
Jango
Traffic Manager
Traffic Manager
Posts: 189
Joined: 28 Aug 2004 12:13

Post by Jango »

the "screenshots" look much better now
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

Wow - thanks - well - the only "problem" is - it's now in two files, but I guess - it's OK - as long as it's more readable/understandable. I hope my integer's name choosing was somewhat right. :)

Please look at the diff-ed industry.h - is it OK, if it's at the end of file - that enum statement?

I have tested the modification and it works as before - as intended. :D
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

Upon further, more extensive testing - this enum stuff doesn't perform as expected, so I'll remove all files from with it from here and SourceForge. :(

I read, that integers in enum statement are not always strictly used....

Please suggest some better methods...

EDIT: I used the wrong exe for testing - enum patch works OK.

Please disregard this post. :oops: :oops:
Last edited by SirkoZ on 23 Jun 2005 14:26, edited 1 time in total.
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

There - this works alright - "screenshot" from industry_cmd.c:

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));
EDIT: Please - disregard this post. :oops: :)
Last edited by SirkoZ on 23 Jun 2005 14:26, edited 2 times in total.
Diadem
Engineer
Engineer
Posts: 70
Joined: 13 Jun 2005 14:19

Post by Diadem »

Why are you using int's for global constants. An int costs memory, it's a dynamic variable used for storing dynamic stuff. Numbers that chance.

You should use enum's or #define's for constants. Enum's are prefered in the coding style we use. If they don't work for some reason, like you claim, then use #define statements.
User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1518
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Post by SirkoZ »

I must have used the wrong exe - default one - I have now retested the "enum" patch and it works flawlessly. Sorry for confusion. :oops: :oops:

Here are the "enum-patches":
Attachments
industry-h_enum.patch
(370 Bytes) Downloaded 336 times
industry_cmd-c_enum.patch
(1.6 KiB) Downloaded 409 times
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 0 guests