tags:

views:

294

answers:

11
  • Why would a developer use a macro?
  • What are the advantages and disadvantages to using them?
  • Where can I find some resources about and how to use they?
+6  A: 

In general a macro is an instruction that expands into several instructions. An abbreviation, if you will. Macros exist for the same reasons that abbreviations exist; they are a kind of shorthand.

Robert Harvey
+1  A: 
Mark Byers
+1  A: 

Macros are batches of commands. They are usually used by end users to automate repetitive tasks.

Example macros could include:

  • replacing occurrences of certain text in a document
  • applying the same filter to a bunch of images
  • downloading a set of webpages
  • ...

Generally macros are intended for direct use by the end user (in contrast with e.g. APIs which are intended for developers).

Patonza
A: 

A "macro" can be used as a template engine in order to minimize repetitive tasks e.g. cut/paste.

jldupont
+6  A: 

Macros is a pretty broad term. In C++, for example, there are macros such as these. From a software development perspective, they allow you to use these convenience macros (__FILE__, for example) to do certain things, like get the name of the current source file, etc.

There are also keystroke macros you can record in an editor. For example, if you are going to edit each line of a file and surround it with quotes, you could record a series of keystrokes to do that, and then "save" the macro to a single key. Then you could press that key which would execute all the keystrokes you just saved for each line in the file.

dcp
+3  A: 

Macros exist for many reasons

  • to provide a handy shorthand
  • to provide an indirection where some setting or other is define, saving the need modify this value in every location where the macro is referenced
  • to provide a level of abstraction (I guess a bit like a non-macro function)
mjv
+1  A: 

Macros are user friendly code. They are often validated by experience. Thus they save a lot of time.

Harviej
+1  A: 

Without defining what Macro is, as most of the guys already did, I'll just list down some pros/cons (by no means exhaustive)

Advantages:
- Saves time (purpose of a shortcut)
- Ergonomic (subjective - less typing but usually need to press 2 or more keys at one go)
- Makes a geek-wannabe feels geek-ier

Disadvantages:
- Can be tedious to manage if there are too many macros
- Possible conflict with an existing app/system shortcut and cause confusion
- Potential hazard for someone use to use the same computer/app

o.k.w
+1  A: 

Benefits:

Resharper's shortcuts are possibly viewed as a macro to some and in some cases can be useful in cutting down the number of key strokes to perform certain actions. Same applies to using IntelliSense.

Drawbacks:

If a developer becomes almost dependent upon such tools that they don't know how to do something in ways if the tool isn't available or if enough macros are used, that it will almost appear like another language as 80-90% of the code may just be macros, which I have seen and used in previous places including a custom markup language for generating web pages.

JB King
+1  A: 

A good introduction to Lisp macros can be found in Practical Common Lisp, chapters 7 and 8.

Svante
A: 

I use macros when inline code is faster than calling a function. This rarely matters for me now, except in DSP programming. I used macros all the time in assembly language and C.

Nosredna
Even then, most of us are using compilers which can reliably generate code using inline functions/methods that are as fast as macros. I mentioned this just to be clear, that people don't get confused into thinking 'macros are better, because they are faster - I should use a macro' because that is only conditionally true, and typically false.
Justin
I like to run performance tests both ways, and I sometimes have trouble figuring out whether a compiler is inlining or not, as it's often up to the compiler whether or not to inline even if you tell it to. But as I said, it rarely matters, except for DSP or embedded applications.
Nosredna