Conditionally Hide Code from the Compiler

Conditionally Hide Code from the Compiler

If you want to compile your app with two difference versions of Xcode or two different Base SDK settings then you should use compiler directives:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 // iOS 7.0 supported
// iOS 7 code here
#else
// Pre-iOS 7 code here
#endif

Do not use this for runtime checks. This solution is only to be used when you must compile your code with two different versions. An example would be you have added iOS 7 code but you still need to compile the code with Xcode 4.6. Using the compile directives allows you to "hide" the iOS 7 code from the compiler using the older Base SDK.

See the "SDK Compatibility Guide" in the docs for more on this and proper runtime checks.

Is it possible to conditionally hide properties at compile time in .Net?

I happened across this issue again recently and this time the answer came to me very quickly; simply set up a couple of constants:

Friend Class CompilerUtils

#If HideCode Then
Public Const Browsable As EditorBrowsableState = EditorBrowsableState.Never
Public Const BrowsableAdvanced As EditorBrowsableState = EditorBrowsableState.Never
#Else
Public Const Browsable As EditorBrowsableState = EditorBrowsableState.Always
Public Const BrowsableAdvanced As EditorBrowsableState = EditorBrowsableState.Advanced
#End If

End Class

Then in your code, decorate a member like so:

<EditorBrowsable(CompilerUtils.Browsable)> _
<EditorBrowsable(CompilerUtils.BrowsableAdvanced)> _

Force the compiler to ignore some lines in the program

Short answer:

Use macros and #ifdef checking. For example:

#ifdef MY_CONTROL_MACRO
...
#endif

the code within this scope will only be compiled if you already defined the MY_CONTROL_MACRO macro.


More stuff:

  1. To define such a macro, you can

    • Add #define MY_CONTROL_MACRO to your code. Or,
    • For VS, add MY_CONTROL_MACRO to Project > Properties > C/C++ > Preprocessor > Preprocessor Definitions. Or,
    • For GCC, compile your code with option -DMY_CONTROL_MACRO.
  2. You can check out here for more info.

    This block is called a conditional group. controlled text will be included in the output of the preprocessor if and only if MACRO is defined. We say that the conditional succeeds if MACRO is defined, fails if it is not.

    The controlled text inside of a conditional can include preprocessing directives. They are executed only if the conditional succeeds. You can nest conditional groups inside other conditional groups, but they must be completely nested. In other words, ‘#endif’ always matches the nearest ‘#ifdef’ (or ‘#ifndef’, or ‘#if’). Also, you cannot start a conditional group in one file and end it in another.

  3. You can also use the advanced ifdef-else-endif style:

    #ifdef MY_CONTROL_MACRO
    ... // this part will be valid if MY_CONTROL_MACRO is defined
    #else
    ... // this part will be valid if MY_CONTROL_MACRO is NOT defined
    #endif

Hide ascx markup based on condition

Similar way you use the <% %>, just not add the =, and your code will be as:

<% if (!string.IsNullOrEmpty(siteUrl)) {%>
<a href="<%= siteUrl %>"> <%= MessageMe %> </a>
<% }%>

You can also read Scott Guthrie block for that syntax

Java conditional compilation: how to prevent code chunks from being compiled?

Nope there isn't any support for conditional compilation in Java.

The usual plan is to hide the OS specific bits of your app behind an Interface and then detect the OS type at runtime and load the implementation using Class.forName(String).

In your case there no reason why you can't compile the both OS* (and infact your whole app) using Java 1.6 with -source 1.5 -target 1.5 then in a the factory method for getting hold of OS classes (which would now be an interface) detect that java.awt.Desktop
class is available and load the correct version.

Something like:

 public interface OS {
void openFile(java.io.File file) throws java.io.IOException;
}

public class OSFactory {
public static OS create(){
try{
Class.forName("java.awt.Desktop");
return new OSJ6();
}catch(Exception e){
//fall back
return new OSJ5();
}
}
}

Simple ways to disable parts of code

I'm not sure I should post this because it's not something I think is 'good code', but I'll admit to having used the following technique as a quick-n-dirty way to be able to quickly switch between two small snippets of code when I'm just checking something out:

// in the following , foo() is active:
/**/ foo(); /*/ bar(); /**/

Now just remove one of the asterisks at the front:

// now bar() is active:
/*/ foo(); /*/ bar(); /**/

Of course, this should never make it past the "just checking things out" phase...

can I hide a method to some environments with conditional imports?

It is not possible. Your idea I would say that it is the best possible, and it doesn't even seem to me a hack (simply those who have as target dart or flutter native can import an additional file, not available when the target is the web).

Support for conditional imports/exports is currently very limited (and has become even more limited by switching to Dart 2, before you could use any constant as condition, not just the properties of dart.library), and it is not possible to do know the compiler that different APIs are available based on the platform. Also famous packages like sqflite have adopted naive solutions such as require different imports based on the mobile VS Desktop platform (if a developer then wants to use only the APIs available in all platforms, can wrap everything in a file with a conditional import, and only expose the common APIs. In reality it is a bit more complicated than that, but the substance is this).

The Dart Analyzer does not even verify the conditional imports, so that in the compilation phase you may have errors not reported by the IDE or vice versa, and as a condition you can use non-existent references (for example if (xxx) 'my_import.dart') without neither the Dart Analyzer nor the compiler signal the error; in this case the compiler will consider the condition always false, which can lead to bugs.

This to say that your solution is not bad at all, since the support is currently so lacking.

C Macro trick to hide assignment?

If the code accessing var_name in source2.c is unreachable when CUST1 is not defined (as you seem to say), then you can define it like this:

#include <stdlib.h>

#define var_name (*(abort(), (type *)0))

This will work syntactically as an lvalue (or rvalue) of the correct type, but will abort the program if it's actually ever executed.



Related Topics



Leave a reply



Submit