Monday, January 3, 2011

The .NET Preprocessor - Part 4

NTSTATUS: STATUS_DOTNET_MACRO

OK, so now let’s see what macros would be useful… But before we actually get to that, I think we should look at the motivations and the sensibilities behind it. For those who are or were not C/C++ programmers, this would be relevant. Also, the article on the C Preprocessor in Wikipedia is a good place to start.

One of the first points, I believe, is that the macros should help you reduce or simplify your work/effort. Apart from general term replacement as in declaring what would be const values and such, is that the macros can help you expand arguments into pieces of code. So the common sense here would be that if it takes more effort to use the macros than actually writing the code, then it’s probably a poor choice in using that macro.

The only real guidelines I can recommend are the following:

  1. If you start to get annoyed about having to write some code because you’ve done it so many times and want some magic shortcut, then it’s a good candidate to be a macro.
  2. If you are writing a piece of code that is repetitive, but a term or two changes just a little bit here and there, then it’s a good candidate to be a macro.
  3. If calling the macro takes more effort than writing the code itself, then it might not be a good idea.
Because IntelliSense is so good now, some code don’t need to be macros as keystrokes have been reduced dramatically for languages like C# and VB.NET. Not so lucky if you’re writing C/C++. IntelliSense still (until today) behaves like a retarded potato that is two bricks short of a load. I just don’t get it. Do they think that Visual C/C++ don’t need IntelliSense to pop-up as soon as you type and make clever suggestions and auto-complete for you? OK, now that I’ve gotten that out of the way, you get the point.

If a macro results in more keystrokes, then it might not be that great an idea. Also, do take note that the macro (keystroke) recorder is a brilliant tool which I love, that is perfect for adjusting text with a pattern or structure. It’s an absolute gem of Visual Studio. However, that isn’t really practical in actually writing code.

Here are some macros that you might useful for C#. I use some of them:




As for the names of the macros (the identifier), those above are basically cryptic. This is because I’m keeping them short, for if not, it’ not be practical as I’d have to type a long name, which might make me punch more keys than if I did not use the macro in the first place. So it’s really up to you how you choose to name it. Refer to guideline no.3 above.

So really, you can do quite a lot with macro expansion, and as cliché’d as it sounds, it’s really up to your imagination. For example, the macro PROPVM(p) above is something that would be useful for implementing MVVM, and you’re not using any magic framework. Even a basic Property like that though simple to write, becomes tedious when you need to do a lot of it. Yes, you can copy/paste and modify, you but it does get tedious, doesn’t it? The macro would have reduced work significantly.

No comments:

Post a Comment