Thursday, February 23, 2012

Some useful Pragma commands

The Real Studio mailing lists are always a source of lively discussions.  A recent thread started with a suggestion to allow commented code to retain its formatting for readability.  This brought up a long discussion about ways to disable code.


Disabling Code
Like many of you, when I need to disable a block of code I'm working on, I will select it in the code editor and then click the Comment button on the toolbar (or press Command-').  This loses the code formatting structure (indentation), which I never really considered an issue.


It turns out there is already a way to disable code and keep your formatting: use conditional compilation.


Although you may never have done it in this manner, code like this will disable everything within the #If/#Endif:

#If False Then
  Anything can go here, whether it is code or plain text.


  Dim count As String
  For i As Integer = 1 To 100
    count = count + Str(i)
  Next
#Endif

#If commands are parsed before the compiler does its magic.  Since #If False is never true, these commands are stripped from your code before being sent to the compiler.


Of course, it is less convenient to disable code this way because you actually have to type in the commands.  A contextual "Wrap this" menu might be useful here.  I created a Feedback case for this: 20397


Errors and Warnings
The discussion brought up a couple other useful Pragmas: Warning and Error.  To be perfectly honest, I never noticed that these pragmas even existed!  It turns out they were added years ago (Real Studio 2008, according to the Language Reference).


Something that I do often is mark code that I need to go back to for some reason.  Typically, I add a comment at the top and put TODO in it with a description:

// TODO: Fix this nasty hack

I think that usage stems from my Visual Studio days.  I would then periodically search the project for TODO and would have a list of things I need to address.


It turns out you can get even better behavior using #Pragma Warning:

  #Pragma Warning "Update this code to use Menu.Clone."
  Dim selectedMenu As MenuItem
  selectedMenu = EditMenu.PopUp

Your project runs just fine, but when you Analyze Project (or Item), this warning is displayed in the Warnings List.  I really like this and will start making more use of it.


The related Error pragma generates a compile error:

#Pragma Error "Stopped the compile."

I haven't thought of a good use for this yet, but I'm sure someone can enlighten me!

3 comments:

Eric de La Rochette said...

The #pragma Error can be automatically added in a method needed to implement an interface to remind you that you forgot to implemente a method.
I sometimes use it inside a '#if Targetxxx...#endif' block to prevent building for a particular platform. It proved to be useful a couple times...

Eric de La Rochette said...

Oops! My mistake. The interface method are (optionally) filled with a simple '#error' which is not an actual pragma. My mistake...

Jason said...

The only nuisance with #pragma Warning is that more than one are placed within the same "category" when compiled or analyzed, regardless of where the warnings are located. It's a small nuisance.

Otherwise, it's very handy, and other pragma commands are certainly useful for speeding up sections of code, if handled carefully.