Single-line #if statements

| | Comments (6)

I never knew this before, so I figured that it's entirely likely no one else knew about it either. REALbasic has a single-line if construct for "pre-compiler" #if statements! In order to use this, you have to do:

#if someConstant then expression

It's not something I expect most people will be using, but it does have one very handy use: debugging statements. Instead of having to do:


#if DebugBuild
Log( "Some Debugging message" )
#endif

You can instead do:

#if DebugBuild then Log( "Some debugging message" )

which is much more succinct!

There are some cases where the single-line #if doesn't behave in an entirely expected fashion. There is a bug report in the feedback system about it not working in conjunction with declare statements, which is how I happened to stumble upon this construct in the first place! If you find any other cases where this doesn't behave in the same fashion as a "regular" #if statement, then be sure to file a bug report so that it can be looked into.

6 Comments

interesting

I may be old and pedantic though .. I almost always use multi-line forms as I just find it easier to read and follow regardless of the volume of code

With codefolding it's not a big hit

But I can see using this for debugging in the fashion you describe

I'll have to try that out and see how far back it goes. Outside of debugging, and maybe those declares, I don't see it as helping much overall. Declares *might* benefit, if you are only doing one declare at the top and most of the code between platforms will be the same. I don't see much other use for it, though.

@jdiwnab -- it's not that particularly useful. It just surprised me as neither Mars nor I recalled it actually being a feature of the compiler. But it is; we had to write code specifically to support it!

ah yes
writing code in a stupor can give you that "Huh" result much later on :)

This is a feature i once used. But there were problems using this form in nestet #if-statements...

@Carsten -- you would nest single-line #if statements? Wow.. that idea never even occurred to me. It would produce some very long lines of code!

If you have any examples that are still problematic, please file a bug report with an example project.

Leave a comment

Disclaimer

I'm currently an employee of REAL Software. My blog is mine. The opinions represented in this blog are mine as well and may not represent my employer's opinions. All original material is copyrighted and property of the author.

REALbasic® is a registered trademark of REAL Software, Inc. REAL SQL Server™ and Lingua™ are pending trademarks of REAL Software, Inc. All rights reserved.