- 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?
views:
294answers:
11In 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.
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).
A "macro" can be used as a template engine in order to minimize repetitive tasks e.g. cut/paste.
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.
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)
Macros are user friendly code. They are often validated by experience. Thus they save a lot of time.
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
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.
A good introduction to Lisp macros can be found in Practical Common Lisp, chapters 7 and 8.
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.