Raised This Month: $52 Target: $400

[TUT] Code Styling & Good Programming Habits

Thread Tools Display Modes
Prev Previous Post   Next Post Next
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 07-28-2012 , 15:43   Re: [TUT] Code Styling & Good Programming Habits
Reply With Quote #33

Because the post didn't mention it, and i didn't read all 9 pages of comments I shall say something (very short) on macros

Macros rock. Basically a really quick way of writing a static (as in nothing in it will change) one-line function. To make calculations easier to read, easier to put into your code, and even grab parts of code required to be used many times.

If you are using a macro with more than one parameter you cannot separate them with commas (my main point) And each parameter will be prefixed with a % followed by an identifier. I'm not sure if this convention can change....but it seems to be accepted, so i'm going with it.
For instance....you have three arrays. One holds indexes to each of the two other arrays. You can combine these two array indexes using a macro with some simple math in it. Then two other macros to retrieve them. See my code....

PHP Code:
new g_PlayersData[33]

#define PRECISION 10000        // this makes sure the numbers don't run together
#define SET_DATA(%1 %2)        %1 * PRECISION + %2
#define GET_INDEX1(%1)        %1 / PRECISION 
#define GET_INDEX2(%1)        %1 % PRECISION 

    // type and size are indexes to the playerType/Size arrays
public doPlayerThing(idtypesize)
g_PlayersData[id] = SET_DATA(type size)
getPlayerData(id, &type, &size)
data g_PlayersData[id]
type GET_INDEX1(data)
size GET_INDEX2(data)

As you can see to combine the two numbers i used a macro to multiply the first index by a large number (i used 10000 for the easy math) any number greater than that of the second will do, really, then add the second. Then to retrieve each number i used another macro for each. To get the first you do some simple integer division (which will leave off the remainder, returning all whole numbers in the division) this gives you that first index. Then for the second i used modulus (%) to get the remainder of the division. This gives you that second index you added on. All combined successfully give you a sleek, efficient way of storing two numbers into one cell.

Hope i helped.
Good day.
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 07-28-2012 at 15:50.
Liverwiz is offline

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -4. The time now is 04:47.

Powered by vBulletin®
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Theme made by Freecode