Why would a developer use a macro?

2020-06-30 06:26发布

问题:

  • 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?

回答1:

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.



回答2:

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.



回答3:

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)


回答4:

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).



回答5:

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



回答6:

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



回答7:

Macros are good for DRY (don't repeat yourself) when nothing else works because of language constraints. Modern languages tend to be flexible enough that macros are rarely needed. For example, being able to pass functions around as parameters, and generics helps reduce the need for macros.

Advantages:

  • Common code is kept in one place.
  • The remaining code is less cluttered and therefore more readable.

Disadvantages:

  • Often difficult to understand error messages when the macro causes a syntax error at compile time.
  • Line numbers in stacktraces generated at runtime might be misleading.
  • A macro sometimes has unexpected results - MAX(x++, y) might increase the value of x twice.


回答8:

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.



回答9:

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



回答10:

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



回答11:

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.



标签: macros