gfxgfxFreeBASIC Games Directory Forumgfxgfx
gfx gfx
gfx
Welcome, Guest. Please login or register. May 21, 2013, 11:22:03 PM

Login with username, password and session length
11.5.2013 - Added a webpage for the latest FBGD competition.

13.3.2013 - Members registrations temporary disabled. For all membership requests, please email me: lachie13@yahoo.com

30.11.2012 - The ninth issue of BASIC Gaming is out! Read it here: http://games.freebasic.net/forum/index.php?topic=560.0

22.11.2012 - Be sure to check our currently running annual FBGD game making competition. This year's theme is SEASONS OF THE YEAR, 300 $ first place prize, and the competition runs till 18th of February. Link: http://games.freebasic.net/forum/index.php?topic=559.0
gfx
gfx
*
gfxgfx
gfxgfx gfxgfx
gfxgfx Home Help Search Login Register   gfxgfx
gfx gfx
gfx
Pages: [1]
Print
Author Topic: Macros - the pros and cons  (Read 2818 times)
notthecheatr
Global Moderator
Forum Sage
*****
Gender: Male
Posts: 351


Who's the guy from 21 Jump Street?

notthecheatr TheMysteriousStrangerFromMars
View Profile WWW Email
« on: January 23, 2008, 09:21:12 PM »

This was originally going to be a reply in my "Why OOP?" topic, talking about efficiency in loops, but I decided to make it a whole topic on its own.


One interesting way to make a pretense of using procedures when in reality doing no such thing is to use macros  Cheesy

#macro MOVE_PARTICLE_TO(particle, x, y)
  particle._x = x
  particle._y = y
#endmacro

Type pticle
  As Integer _x, _y
End Type

Dim As pticle myparticle

MOVE_PARTICLE_TO(myparticle, 0, 0)
Print myparticle._x
Print myparticle._y
MOVE_PARTICLE_TO(myparticle, 23, 42)
Print myparticle._x
Print myparticle._y

Sleep

This can be used in loops without slowing things down, since it's not really a function call at all.  The one downside is that you don't have type checking, meaning you could do really weird things like

MOVE_PARTICLE_TO("this is a string, not a particle!!!", "hi, guys!", "this is a totally invalid place to move the particle to!  it's not even a number!")

where you pass arguments that are the wrong type and get weird errors for seemingly innocent lines of code.  Even worse, the first argument to the macro can be any type that has public members _x and _y - so for example, I could do

Type myrandomthing
  As String _x, _y
End Type

Dim As myrandomthing someObj

MOVE_PARTICLE_TO(someObj, "Hello, ", "World!")

Print someObj._x
Print someObj._y

Sleep

and it will compile just fine despite the fact that the object and parameters to the macro have nothing to do with particles or even positions.  Such is the facts of life when using the preprocessor, which does nothing more than a simple string replace and converts the above code to:

Type myrandomthing
  As String _x, _y
End Type

Dim As myrandomthing someObj

someObj._x = "Hello, "
someObj._y = "World!"

Print someObj._x
Print someObj._y

Sleep

which is of course perfectly valid, if odd, code.

Despite the weirdness, macros do have their uses, and keeping things fast (while somewhat clean-looking, as long as you're careful with it) could be one of them.
Logged

The funniest thing happened yesterday.
KristopherWindsor
Forum Sage
*****
Gender: Male
Posts: 363


The Thirsty Smiley


View Profile WWW Email
« Reply #1 on: January 24, 2008, 12:03:49 AM »

You can look at the macros at the top of the source code file for UnAlien for a good example. The code works on objects of several different types, so in previous programs, I had essentially copied and pasted that macro code several times throughout my code. Wink
Logged

Lachie Dazdarian
Double dipper
Administrator
Forum Sage
*****
Gender: Male
Posts: 1195


lachie13
View Profile WWW Email
« Reply #2 on: January 24, 2008, 04:48:02 PM »

Again, very well explained. I'll think about using them. Thanks.
Logged

"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist
dabooda
Forum Howler
****
Gender: Male
Posts: 122



View Profile WWW Email
« Reply #3 on: January 24, 2008, 07:33:11 PM »

Just my 2c's but macros should be kept really short and sweet also. Because you are throwing them directly into code and will have weird bugs if you aren't careful. Again notthecheater great job on the explanation.

DaBooda out...
Logged

Baa...Baa...Bang! F&#K! I'm Wool!
Pritchard
Global Moderator
Forum Howler
*****
Posts: 149



View Profile Email
« Reply #4 on: January 25, 2008, 07:35:25 PM »

Macros are evil.
Logged
BadMrBox
Forum Sage
*****
Gender: Male
Posts: 371



View Profile WWW
« Reply #5 on: January 25, 2008, 08:44:33 PM »

Why so Pritch? Why so? I dont find any use for it as for now, as for OOP. But why do you consider it evil? Shocked
Logged

nkk_kan
Forum Howler
****
Gender: Male
Posts: 180

Let's rocK~!

nkk_kan
View Profile WWW Email
« Reply #6 on: January 26, 2008, 03:19:17 AM »

again! interesting and illuminating!!  Shocked  Tongue

but one little question....why do we have to use those underscores and where to use them?
Logged

KristopherWindsor
Forum Sage
*****
Gender: Male
Posts: 363


The Thirsty Smiley


View Profile WWW Email
« Reply #7 on: January 26, 2008, 03:31:02 AM »

but one little question....why do we have to use those underscores and where to use them?

You don't need them; they are just characters in the variable names. Wink
Logged

nkk_kan
Forum Howler
****
Gender: Male
Posts: 180

Let's rocK~!

nkk_kan
View Profile WWW Email
« Reply #8 on: January 26, 2008, 03:57:04 AM »

like..for differentiating them?...
one more question...the variables used in a macro are always scoped to the macro only or are they global?
Logged

notthecheatr
Global Moderator
Forum Sage
*****
Gender: Male
Posts: 351


Who's the guy from 21 Jump Street?

notthecheatr TheMysteriousStrangerFromMars
View Profile WWW Email
« Reply #9 on: January 26, 2008, 08:01:29 PM »

Macros are evil.

I understand why you think this, but I don't think macros are always evil.  I do think they can be overused or used inappropriately (to tell the truth I have never once used them in any actual code I've written, other than for testing purposes), and as this whole post tries to explain, there are some major disadvantages/unsafe things about using them, but I think there are some valid reasons for using them.  Like everything else, the preprocessor exists for a reason and it makes some things a lot easier (while I don't perfectly understand it yet, I think the way they are used in the fbext library makes a lot of things much simpler than they otherwise would be).  There are upsides and downsides to everything...
Logged

The funniest thing happened yesterday.
Pages: [1]
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
Cerberus design by Bloc
Valid XHTML 1.0! Valid CSS!
gfx
gfxgfx gfxgfx