10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

Author Topic: Macros - the pros and cons  (Read 9923 times)

notthecheatr

  • Global Moderator
  • Forum Sage
  • *****
  • Posts: 351
  • Who's the guy from 21 Jump Street?
    • AOL Instant Messenger - notthecheatr
    • Yahoo Instant Messenger - TheMysteriousStrangerFromMars
    • View Profile
    • notthecheatr Home
    • Email
Macros - the pros and cons
« 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  :D

Code: [Select]
#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

Code: [Select]
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

Code: [Select]
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:

Code: [Select]
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.
The funniest thing happened yesterday.

KristopherWindsor

  • Forum Sage
  • *****
  • Posts: 363
  • The Thirsty Smiley
    • View Profile
    • Reddit/r/pics
    • Email
Re: Macros - the pros and cons
« 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. ;)

Lachie Dazdarian

  • Double dipper
  • Administrator
  • Forum Sage
  • *****
  • Posts: 1308
    • Yahoo Instant Messenger - lachie13
    • View Profile
    • The Maker Of Stuff
    • Email
Re: Macros - the pros and cons
« Reply #2 on: January 24, 2008, 04:48:02 PM »
Again, very well explained. I'll think about using them. Thanks.
"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
  • ****
  • Posts: 123
    • View Profile
    • DaBooda Gaming
    • Email
Re: Macros - the pros and cons
« 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...
Baa...Baa...Bang! F&#K! I'm Wool!

Pritchard

  • Global Moderator
  • Forum Howler
  • *****
  • Posts: 160
    • View Profile
    • Email
Re: Macros - the pros and cons
« Reply #4 on: January 25, 2008, 07:35:25 PM »
Macros are evil.

BadMrBox

  • Forum Sage
  • *****
  • Posts: 411
    • View Profile
    • BadMrBox.com
Re: Macros - the pros and cons
« 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? :o

nkk_kan

  • Forum Howler
  • ****
  • Posts: 193
  • Let's rocK~!
    • Yahoo Instant Messenger - nkk_kan
    • View Profile
    • nkk's code cache
    • Email
Re: Macros - the pros and cons
« Reply #6 on: January 26, 2008, 03:19:17 AM »
again! interesting and illuminating!!  :o  :P

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

KristopherWindsor

  • Forum Sage
  • *****
  • Posts: 363
  • The Thirsty Smiley
    • View Profile
    • Reddit/r/pics
    • Email
Re: Macros - the pros and cons
« 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. ;)

nkk_kan

  • Forum Howler
  • ****
  • Posts: 193
  • Let's rocK~!
    • Yahoo Instant Messenger - nkk_kan
    • View Profile
    • nkk's code cache
    • Email
Re: Macros - the pros and cons
« 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?

notthecheatr

  • Global Moderator
  • Forum Sage
  • *****
  • Posts: 351
  • Who's the guy from 21 Jump Street?
    • AOL Instant Messenger - notthecheatr
    • Yahoo Instant Messenger - TheMysteriousStrangerFromMars
    • View Profile
    • notthecheatr Home
    • Email
Re: Macros - the pros and cons
« 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...
The funniest thing happened yesterday.