0

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?

the_nuts
  • 5,634
  • 1
  • 36
  • 68
  • 3
    Without any experience in this area: why don't you use a preprocessor directive if it's resolved at compile-time anyway? This will prevent these errors and will also prevent the code for the other version to never be shipped in the first place. – Jeroen Vannevel Dec 31 '15 at 01:27

1 Answers1

1

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)

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120