Monday, 27 May 2013

UnityScript vs. C#

The first choice you're probably going to face developing in Unity is which scripting language to use. I must admit I didn't think about it much at first. I just picked UnityScript and went with it. Later, when I wrote a small application as a technology assessment, I've become to understand the pros and cons of my choice better. Enough to make me reconsider my options.

According to Unity documentation, there are three scripting languages available – JavaScript, C# and Boo.

First of all, let me clear up one thing. The Unity's JavaScript is not the JavaScript (ECMAScript) at all. It has nothing to do with it. If you by chance have some experience with JavaScript programming, for instance from web scripting, you will not benefit from it. I'm not entirely sure why the Unity Technologies chose to masquerade behind an already established name, probably some marketing reasons, but it's only confusing. From this moment on, to steer away from the confusion, I'm going to refer to it solely as the UnityScript, as many developers already do. It seems a more appropriate name. If you're curious about the differences, you can read this very interesting article: http://wiki.unity3d.com/index.php/UnityScript_versus_JavaScript.

Now, Boo has a flavour of Python. I've never worked with Python. My background comes from C++ and Java, plus a few other, scripting languages. Hence I haven't really looked into it apart from reading a Wikipedia article and its manifesto. I haven't stumbled upon much code written in Boo neither, so I'm not going to discuss it any further. All I can say is, if you like Python, give Boo a try.

For me, the main question stands: Should I stick to the UnityScript or the C#? This question continuously raises many heated debates. The UnityScript is the officially recommended language of choice created specifically for Unity. The C# is a more mature language widely used with Microsoft products. What's important is to realize that Unity is built on the .NET platform, which means core data types and libraries are available in all languages. Do you want to use the Dictionary structure in UnityScript? Go ahead and import “System.Collections.Generic”. Everybody can benefit from the .NET framework documentation on MSDN as an additional scripting reference. Developing for mobile platforms also imposes static typing. The final decision boils down to a different syntax and marginal language features.

There's no point to debate the syntax. It's a thing of preference. Its only aspect that can be objectively gauged is verbosity. The UnityScript seems slightly less verbose than C#, though not by much. However, the balance gets tipped the other way if you start comparing the language features.
The UnityScript was designed to be as simple as possible, so it could be learned quickly. That roughly translates to as few features as possible while still convenient to use. I've wondered what features I've employed that I couldn't have used in C#. I've only come up with not having to write the UnityEngine import statement and class declarations for MonoBehaviour descendants. So I've been saved from typing some boilerplate code. (Which is great!)

On the other hand, I've also considered what I miss from Java or C++ (and by extension from C#). Symbolic constants. Yes, a simple feature, but I miss it greatly. They significantly add to code clarity. Generic programming. Really, who'd want to implement the same patterns again and again just because of working with different data types? Or constantly cast the return values? Sadly, support for generics on iOS and Android is limited, yet still there. Speaking of casting, auto unboxing of primitive types would be nice. Have you ever tried to get an int from an Array? parseInt(), seriously? There seems to be no way of re-casting an Object to a primitive type, although the opposite works. And finally, namespaces. Everybody talks about them. Everybody wants them. And in Unity 4 it is still unclear if and how they can be used in UnityScript. These are all minor things, but handy enough to make me think if I should go for the C# instead. Here's another reasonable attempt to compare the language features: http://www.holoville.com/blog/?p=820.

One more point to consider. The UnityScript itself is poorly documented. It is learned mostly from incremental release notes and examples found on the Internet. Many syntax forms are discovered by trial and error, or not at all. There is no standard. No guarantee. The language may change at any time and you may find out the hard way. It's certainly easy to start with, but in the long run I'm not sure it suffices.

Well, smart people make their own choices. Myself, I think I'll fiddle with the UnityScript for a bit longer, so I can actually produce something useful, and in the mean time I'll brush up my C# skills. I'm not saying anything new here; it's a generally recommended approach, see http://answers.unity3d.com/questions/7528/how-should-i-decide-if-i-should-use-c-javascript-u.html, for example. It's always good to hear opinions of other, more experienced, people: http://forum.unity3d.com/threads/18507-Boo-C-and-JavaScript-in-Unity-Experiences-and-Opinions. I guess I just had to arrive at my own conclusion. I hope you will do as well.

No comments:

Post a Comment