Building Jigsaw Puzzle Frenzy: sharing code between WP and WinRT

When I first started working on Puzzle Frenzy I wanted to maintain both the original WP app and also to create a new WinRT app by reusing and sharing as much code as possible.

My plan was based on the presentations at Build which promised  which promised up to 70% code share between  WP and WinRT. In particular check out Create Cross-platform Apps using Portable Class Libraries.

During different stages of the project I tried several approaches for achieving this goal:

Share physical source files between WP and WinRT projects

I initially started building the WinRT app adding new projects to the solution containing the same files as the WP.

The problem

While a low of classes in the WP and WinRT are “identical” in functionality, they are often not in the same  namespace.  On top of that, using the same source files in more then one project is by itself problematic.

Direct consequences:

1.Trying to use the same source files for both WP and WinRT resulted in files like this one:

#if WINDOWS_PHONE
using System.Windows.Media;
#else
using Windows.UI.Xaml.Media;
#endif

Furthermore, for some special cases I was forced to have constructions like this one:

#if WINDOWS_PHONE
//Do something
#else
//Do something else
#endif

2.Having the physical same file referenced by two projects in Visual Studio is a lot of work. Whenever something breaks for one project (90% of the time due to a usings declaration change) the file containing the error has to be opened from the context of the failing project. If the file is opened in the other project the error won’t be highlighted in the code and the solution won’t compile.

For few files, this setup can be maintained even if it is inconvenient. However, as the file count grows, keeping up with the changes is a too complicated task.

Using Portable Libraries

For the appropriate scenarios Portable Libraries are awesome. It eliminates the need for duplicated files altogether as the library can be referenced by both the WP and WinRT. In my case however I wasn’t able to use them because I use Point and Size classes throughout my project. These classes are not available in Portable Libraries. I could have created my own implementation of Point and Size to overcome this limitation, but given the scale of the changes I preferred not to do it: a simple search reveals that I use these classes in more than 100 places in my code.

From what I’ve experienced so far, Portable Libraries offer limited classes to work with. My ViewModels layer which I was planning to keep there needs far to many things which are not available.

Conclusion

Sharing code between WP8 and WinRT can prove to be troublesome.

Both duplicate projects and Portable Libraries have limitations, making it in my case are quite challenging to use.

If sharing the code between the two apps is really your goal, I am fairly confident it can be achieved. However, in my case, for this particular app, attempting to share the code only slowed me down. In the end I kept using duplicated projects but I am not maintaining them at the same time.

I take turns in development cycles implementing features and adjusting the code to work on the other platform. While doing so I introduce bugs in the project not being maintained at the time, but the amount of work needed to fix it is far less than implementing new features at once on both platforms.

Closing argument

If you start developing a new app and want to target WP and WinRT consider using Portable Libraries from the start. It will have some limitations but will also force you to place the code in the right place in order to be shared.

If you already have an WP app and want to migrate it to WinRT you might want to start by maintaining duplicated projects in order to limit the size of the changes needed. Once you reach a stable state you can of course look into  moving towards Portable Libraries if code sharing is your goal.

Have a look and see how it turned out.

Jigsaw Puzzle Frenzy is available in Windows Store, check it out and tell me what you think: http://apps.microsoft.com/windows/en-GB/app/puzzle-frenzy/28238e98-ac0f-4a6b-81ea-a31a24a15acf.

You can also give your feedback on app’s review page or on app’s Facebook page http://www.facebook.com/PuzzleFrenzyApp.

Happy codding!

Puzzle Frenzy home page

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.