Thursday, January 7, 2016

Xamarin vs Cordova

Update: 21-4-2016: Note that all price related remarks on Xamarin have become invalid, since Microsoft has acquired Xamarin and announced that its use will be free of costs. As before Mono will still be used as runtime for Xamarin so those remarks stay the same. Microsoft has also announced new ways of sharing code via Microsofts Standard Library. That is still in preview and therefore not applicable to this blogpost.

Original post:

Recently I've had the privilege to work with Xamarin, where in the past I've been working a lot with Cordova. This blogpost compares both platforms and points out the major pros and cons for both platforms, since both platforms sadly aren't perfect.

I've hands-on experience with both platforms and successfully released multiple business apps to all App stores (Windows, iOS and Android) and also to several Enterprise Mobility Management suites such as MobileIron and HockeyApp. All apps that I have made are business to business apps and are generally data driven apps. The fact that I made mostly b2b apps may cloud my judgement and you should consider if my remarks are applicable to your situation.

Cordova

So lets start with Cordova. Whats great about Cordova is, is that it is completely open. You can look it up, file bugs or tasks and even join the development community. The fact that the apps are web based makes startup time really quick and the learning cursive is almost not there. With the possibility to write native parts using plugins makes it a full service platform. People who say that native functionality is not possible with Cordova just don't know the power of it.

Whats also great about Cordova is, is that it got a big community. There are lots of guys on github and stackoverflow that will answers your questions. Having such as a big community also causes that the tooling got significantly better. Visual Studio got great support for Cordova and the command line interface of Cordova is really easy to use.

Sounds like Cordova is perfect, well that's absolutely not true. Cordova definitely has its quirks. Developing for web has its traditional issues, a major part of that is browser compatibility. You know the browser hell we lived in back in the day? Remember supporting IE6/7/8, Firefox and Chrome? Well iOS and Android in particular reintroduced that same problem. The web views that are loaded with Cordova have different implementations for every OS version. Apple and Google updated their browsers with every OS release and that caused subtle behavior differences in the web views with every release. So as a developer you have to constantly test your code against multiple OS versions for you to be sure that it works perfectly.

Another problem with the web views are keyboard interactivity and form inputs. Business apps naturally have a lot of forms and therefore have a lot of keyboard interaction. This is really a struggle when you use Cordova. The different platforms behave differently with keyboard events, some platforms resize the web view and others just overlay the web view with the keyboard, making your inputs not visible because of the overlap of the keyboard. You wont have this problem when you are using native UI components. Another point I was trying to make was the fact that the HTML5 input controls are poorly implement in the several web views. This can really be a struggle since, for instance, you won't get the nice date picker keyboard for date inputs in certain browsers. You will have to work with input masks and weird date pickers which are probably a little confusing for your users.

Another point that I would like to make is performance. Cordova apps generally perform well but there are boundaries. You can expect performance issues when you have large lists or grids with a lot of data, in other words when your DOM is blowing up. The web views render significantly slower on mobile devices then on your desktop, I recommend that you develop and test on actual devices to identify performance issues early in your development cycle. Cordova also performs really bad for HTML5 games, don't try to use plain Canvas for gaming purposes since you will be disappointed about its performance. And also WebGL is poorly or not at all implemented in different OS versions.


Pros and Cons of Cordova
Pros Cons
Web technology
  • Tons of JavaScript libraries
  • Small startup time
  • Almost no learning curve
Different web views everywhere
  • Forms based applications are a mess!
  • Android made a mess of it
Opensource
  • Big community (Stack overflow, Github)
  • Lots of plugins available
  • Free to use
Performance
  • Calculations tent to be slower
  • Multi threading is only possible with webworkers
  • Large lists or grids render slow.
Tooling
  • Great Visual Studio support
  • Cross platform CLI
Gaming

Xamarin

Well and there is Xamarin, finally cross platform mobile development tooling in C# .NET. As a .NET developer I was really happy when Xamarin came out. Yay! I can use my favorite language to write Android and iOS apps. But then I looked at the price and the threshold for getting started became to high for me. But luckily after a while the company that I'm hired to decided to create Apps with Xamarin so that's how I met Xamarin. So is Xamarin worth the money? Lets find out!

So lets start with the most obvious advantage of Xamarin. You can write C# code for mobile devices. Its not just fancy, its very functional as well. C# is a mature language and probably one of the best out there. It also got a huge existing code base. You can find examples and solutions of practically everything so that's really great! You can also use NuGet for existing packages so you won't have to write everything all over again. But does Xamarin really run the .NET framework? Well actually it doesn't, it runs Mono, the opensource variant of .NET. Is that a problem? Well it shouldn't be, but sometimes it is. As a developer you trust .NET with your life but when you start coding on Mono you will soon figure out that there are subtle changes and that it sometimes doesn't really work as expected. Debugging for instance is quite fragile, e.g. async and await debugging performs really strange and step over functionality works really lame. These are just examples so don't think that these are the only problems.

Lets focus on the code reuse. Probably all of your code is written in C# .NET instead of Mono. This means that there's a chance that some of your C# code isn't executable on Mono, but the chance of that is minimal since Mono is quite up to date. You shouldn't be using WCF, WPF and MVC in Xamarin anyway. There is a greater chance that your existing code is not a portable class library. Normal class library's wont be usable since they rely on the complete .NET framework instead of the Portable variant of .NET. For more information please visit Cross-Platform Development with the Portable Class Library documentation of Microsoft. So please make sure that before you start with Xamarin that the code you want to re-use is compatible or convertible to a PCL.

What's actually really great about Xamarin is the performance compared to Cordova. Xamarin actually executes native code whether or not via a managed code runtime such as a Java VM or the Mono runtime. Depended on what code you call either the Java VM or the Mono runtime will execute native code which really speeds up your calculations and processing time. It also enables you to use multi-threading which can really speed up your time intensive tasks, especially with use of the latest multi-core devices, and also background tasks can significantly improve your UI responsiveness which is always great for a better user experience.

Sounds like Xamarin is an expensive gift from heaven right? Well sadly it's not. You will still have to write your UI for every platform. And that will not be easy at all. You will have to know the Android and iOS frameworks really well since Xamarin just provides C# wrappers for the Native UI parts of both platforms. And on top of that the in-code documentation of Xamarin is just not there, so you will have to have Google on your second screen with the SDK documentation of the specific platform. But hey, when your finished you got native UI's for every platform, that probably means better performing views and a platform matching UI which will result in a better User Experience. But I hear you thinking what about Xamarin Forms? Well Xamarin Forms is immature and to probably to basic for your use. It contains the absolute basic UI parts if you want anything custom you will end up writing platform specific UI's.

Pros and Cons of Xamarin
Pros Cons
Performance
  • Multi-threading is enabled
  • More responsive UI
Expensive
  • Pay big money for everything.
  • e.g University, Test cloud
Write in C#
  • Chance of code reuse is high
  • e.g Data contracts, serialization
  • Write Native UI in C#
Mono instead of .NET
  • Tooling contains bugs
  • Looks like .NET but actually isn't
  • Poor in code documentation
Tooling
  • Great Visual Studio support
  • Excellent Android UI designer
Platform specific UI
  • Steep learning curve
  • Immature Xamarin Forms
  • Wouldn't recommend for Gaming

Conclusion

So which one is the best? Well of course the answer should be that it depends on your situation. Cordova's learning curve is short and you are really quick up and running. Xamarin is expensive and the learning curve is steep, but it also provides better User Experience and performance. To help you decide I made some guidelines:

Choose Cordova if:
  • You don't want to spend multiple thousands of dollars on tooling.
  • You definitely want the same UI on every platform, regardless of User Experience.
  • JavaScript is your thing and C# isn't.
  • You want to be up and running withing days/weeks.
  • You don't rely on calculations, multi-threading and lots of data in the UI.
  • You got an existing code base written in JavaScript.
Choose Xamarin if:
  • You don't mind spending multiple thousands of dollars on tooling.
  • You pick User Experience over easy UI development.
  • You are willing to learn platform specific UI frameworks.
  • You rely on calculations, multi-threading or UI intensive screens.
  • C# is your thing and JavaScript isn't.
  • You got an existing code base written in C#.
If you have any question feel free to ask, you can also contact me to advise you on a more personal level.

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Here is an interesting infographic featuring the comparisson of native, hybrid and Xamarin development: http://gloriumtech.com/blog/xamarin-development-mobile-apps/

    ReplyDelete
  3. You should update this after Xamarin was bought by MS and is now free.

    ReplyDelete
    Replies
    1. You are absolutely right, I'm on it!

      Delete
  4. Xamarin technology is ruling the App development industry now!

    DevRabbit is helping the start ups and large businesses in accelerating their growth through cutting edge technology by leveraging Mobile App Development for iPhone and Android, Cloud Solution, Web Application Development & Big data analytics solutions, Digital Marketing services. DevRabbit is a Xamarin Consulting Company.

    ReplyDelete