If you’ve been working with Xojo for a while, you’re likely familiar with conditional compilation directives like #If TargetMacOS or #If DebugBuild. But there are two particularly useful variants that are often overlooked in day-to-day development: #If False and #If True. In this article, we’ll look at what these directives do, when to use them, and why they can be a better choice than regular comments in certain situations.
What Is Conditional Compilation?
Conditional compilation allows you to include or completely exclude sections of code from the final application based on a condition. Unlike an If statement at runtime, this decision is made at compile time. Code excluded through conditional compilation simply does not exist in the compiled application.
#If False – Completely Excluding Code
#If False
MessageBox("This line will never be compiled")
Var result As Integer = DoSomething()
#EndIf
When the condition is False, the entire block is ignored by the compiler. It is not compiled, not included in the application, and not even checked for syntax errors. The code is effectively invisible to the compiler.
Why Not Just Comment It Out?
At first glance, #If False seems to do the same thing as a comment. But there are key differences:
1. Nested Comments Are No Problem
If a code block already contains comments, commenting out the entire block quickly becomes messy:
' Var x As Integer = 42 ' Default value
' Var y As Integer = x * 2 ' double it
' ' This is already a comment
With #If False, everything stays clean:
#If False
Var x As Integer = 42 ' Default value
Var y As Integer = x * 2 ' double it
' This is already a comment
#EndIf
2. Incomplete or Broken Code
Sometimes you want to keep an unfinished code draft without it preventing the build. Since the compiler completely skips the contents of an #If False block, the code inside may even contain syntax errors:
#If False
' Experimental approach – not finished yet
Var data As = LoadFromServer(
ProcessResult(data
#EndIf
This code would be useless as a comment because you’d have to comment out each line individually. With #If False, it’s safely stored away.
3. Quickly Disabling Large Code Blocks
For larger sections, #If False / #EndIf is far more practical than prefixing every line with '. Plus, the block can be reactivated instantly by simply changing False to True.
#If True – Always Including Code
#If True
MessageBox("This line will always be compiled")
#EndIf
An #If True block is always compiled — it behaves identically to regular code. This might sound useless at first, but it does have practical applications.
When Is #If True Useful?
1. Quick Toggling During Development
Imagine you’re working on a feature and want to quickly enable or disable certain code sections:
#If True ' <- Set to False to disable the new feature
' New feature
Var service As New CloudSyncService
service.SyncNow()
#EndIf
By simply changing True to False, you can disable the feature without deleting or commenting out any code.
2. Placeholder for Future Conditions
Sometimes you already know that a code section will later be tied to a condition — such as a platform or build type. With #If True, you can prepare the structure in advance:
#If True ' TODO: Change to TargetMacOS once Windows alternative is ready
Var folderPath As String = SpecialFolder.Applications.NativePath
#EndIf
3. Visual Code Grouping
In longer methods, #If True can serve as a visual structural block to highlight related code:
#If True ' --- Initialization ---
Var db As New SQLiteDatabase
db.DatabaseFile = dbFile
db.Connect
#EndIf
#If True ' --- Load data ---
Var rs As RowSet = db.SelectSQL("SELECT * FROM users")
#EndIf
Defining Custom Compiler Constants
Xojo also allows you to define your own constants for conditional compilation.
#If kEnableLogging
WriteToLog("Application started")
#EndIf
This lets you control features or debug functionality centrally through a constant without having to modify the code itself.
Best Practices
#If Falseinstead of mass comments: For larger code blocks that need to be temporarily disabled,#If Falseis the cleaner solution.- Add a comment: Always document why a block is disabled:
#If False ' Disabled until bug #1234 is fixed
ProcessCriticalData()
#EndIf
- Don’t use permanently:
#If Falseblocks should not remain in the code indefinitely. If the code is no longer needed, remove it. If it is needed, activate it. - Prefer platform directives: When dealing with platform-specific code, use the specific constants (
TargetMacOS,TargetWindows, etc.) instead of#If Trueor#If False. - Avoid deep nesting: While nested
#Ifblocks are possible, use them sparingly as they quickly reduce readability.
Conclusion
#If False and #If True are simple yet powerful tools for everyday Xojo development. They offer a cleaner alternative to commenting out large code blocks, enable quick feature toggling during development, and lay the groundwork for a structured, cross-platform codebase. Used thoughtfully, they save time and help you stay organized — even in larger projects.
If you want to automatically convert a selection into a #If/#EndIf block, you can do so via the context menu: “Wrap In > #If / #EndIf”
Happy coding.
Martin T. is a Xojo MVP and has been very involved in testing Android support.
