Search code examples
c#windows-phonemobile-application

Mobile apps: is it a good practice to use a constant to distinguish the Free/Pro version?


I have two apps which do essentially the same, with small differences (different logo, ads hidden in the Pro, different Facebook app id for the login flow, etc.)

Currently, I use a

public const bool isProVersion = false;

Which I toggle before compiling, and it changes the whole app behavior depending on its value.

The only issue is that I have a lot of "unreachable code detected" notices in my error list.

This is intended, because some code must never be reached in that version, but it doesn't look very clean.

I could use a static variable instead of a constant, but this will make, for example, the ads code compiled and "reachable" into the Pro version, which is not needed and could lower the performance.

Is there any better alternative?


Solution

  • Expanding on the comment by Jeroen Vannevel, you really should use preprocessor directives. You should use an ISPROVERSION directive and two compiling configurations, one that defines ISPROVERSION (the pro configuration) and one that doesn't (the free configuration).

    So, instead of doing this:

    if (YourClassName.isProVersion)
    {
        // user has paid, yey!
        SomeClass.LoadAds(false);
    }
    else
    {
        // user didn't pay, scr** them!
        SomeClass.LoadAds(true);
    }
    

    You would be doing something like this:

    #if ISPROVERSION
        // user has paid, yey!
        SomeClass.LoadAds(false);
    #else
        // user didn't pay, scr** them!
        SomeClass.LoadAds(true);
    #endif
    

    This way, if you build using the pro configuration, the code in the #else statements won't even be compiled.
    Read more about defining preprocessor directives here: /define (C# Compiler Options)