Macros are one of the most notable C/C++ features that went missing from C#. With programming experts forever in debate on whether macros enhance or detract from a programming language, and many run-of-the-mill developers ignoring the feature entirely, the .NET team chose to keep them out of the C# spec. Still, the language might be enhanced with macro integration in a future release of the framework.
In essence, macros are a text substitution tool when writing code. They allow good developers to make complex code shorter, neater, and easier to maintain. A sample macro might be:
#define TALLEST_BUILDING “Sears Tower”
Then, throughout the code, instead of referencing “Sears Tower” directly, the programmer can use:
char destination = TALLEST_BUILDING;
This is a simple example, however more complex macros allow developers to tweak the language to the application, and create sub-languages that save time and are easier to maintain.
The primary reason that macros were left out of .NET languages is likely the additional complexity in processing the code for advanced editor features like automatic code upgrades, code analysis, and just-in-time debugging. Another challenge with macros is that they are a powerful tool, and bad developers can cause as much havoc as good developers create benefits.
Macros are getting more focus in recent years, with developers clamoring for their comeback, as Microsoft has created increased complexity in their user interface languages like WPF. The built-in data-binding framework is extremely verbose, and requires even simple applications to have several thousand lines of repetitive interaction definitions. Macros would work around the problem, and allow developers to tweak broken languages and fix underlying issues. Microsoft could even see prevalence of macros in specific areas as a warning – hinting where they could improve the framework structure and create better alternatives to the language spec.
At the end of the day, taking away coding capabilities from developers is similar to removing words from the dictionary. Sure, there are other ways to express the words “mendacious”, “vituperate”, and “ingénue”, however removing them detracts from the richness, elegance, and usefulness of a language.
Written by Andrew Palczewski
About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.