2

There are couple of places where #if seems to give you abilities that polymorphism doesn't:

  • Conditionally specifying base class
  • When a section contains code that is not recognised by a compiler on another platform
    • e.g. some new operator only supported on one platform

I was thinking about splitting into 3 partial classes so each platform includes the 2 it requires, but that seems worse to me.

Example 1

Changing base class, depending on condition

public class FullBase
{
    public void Register()
    {
        Console.Write("I am full featured so will use the :-) operator");
        int a = 1 :-) 2
    }
}
public class MobileBase
{
    public void Register()
    {
        Console.Write("My compiler does not know what the :-) operator is");
    }
}
#if MOBILE
    public class MyClass : MobileBase
#else
    public class MyClass : FullBase
#endif
{
    //...
}

Example 2

Here, I have a class to hide the IDisposable pattern boilerplate. If I moved to DI, I think it would introduce DI boilerplate in the many derived classes.

public class Disposable : IDisposable
{
    //...

    private void RunChecks()
    {
        #if !MOBILE
          //Use reflection to walk the OBJECT tree looking for potential issues
        #endif
    }
}

public class A: Disposable
{
    //...
    protected override void DisposeManagedResources()
    {

    }

}
// Also Class B, C, D, E.....
  • 2
    Why isn't polymorphism possible? You could extract that differing behaviour behind some interface and inject it, rather than have it in the base class – Ben Aaronson Apr 24 '15 at 10:22
  • 1
    What language/platform are you dealing with here? Potentially, you could move the relevant sections to a new .dll or whatever and include a reference. – Clockwork-Muse Apr 24 '15 at 11:24
2

Conditional compilation effectively creates multiple versions of the code base. That's exactly the purpose of version control branches.

The other alternative is put whatever code needs to vary in its own assembly/dll. Different dlls can contain different implementations of the same class (assuming the public fields are the same).

Both alternatives have the advantage of not cluttering the code with every alternate implementation simultaneously and presenting only the code you're working with as a coherent whole.

  • So in example 2, I could create DisposableBase, then derive 2 classes with specialised code for each platform, both called Disposable? This would allow classes on any platform to derive from Disposable and get platform specific implementations. p.s. I'm reluctant to branch - I don't want to maintain 2 codebases – Andy Joiner Apr 24 '15 at 12:02
  • 1
    @AndyJoiner If subclassing/interfaces are a viable solution you wouldn't need to use a separate dll; just refer to the base class everywhere and pass the correct subclass wherever it's needed. I've assumed in my answer that for whatever reason this approach isn't viable. With regards to 2 codebases, you're maintaining them either way. The only difference is whether that's going to be reflected explicitly or implicitly in your source control tool. – Doval Apr 24 '15 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.