- Home /
What does one language have over the other? (functionality)
Why is it that in some example projects, the same programmer uses C# scripts and Javascript scripts at the same time? Is it because of functionality differences? I have been thinking of this for a while but could not have a clue.
Thanks in advance!
Answer by Statement · Aug 13, 2011 at 09:52 AM
Flavor. JavaScript/UnityScript offer much cleaner code for coroutines for example where others prefer C# since they knew it before they came here, or they enjoy the syntax better. There are ups and downs with both languages. I wouldn't worry too much of which language one would pick for personal use, but there often comes a slight headache as soon you start mixing the languages in a project.
I am mainly a C# guy. I prefer the elegance JS offer for some things, like coroutines. That said, I don't use JS in my professional work other when I have to use with thrid party code. Mostly because I love Visual Studio & C# in general.
C# support extension methods, where JS does not. That said it is still possible to call the methods in a more verbose way in JS. This makes C# more ideal when working with Linq, or other systems that benefit from extension methods.
JS allow implicit assignment (not sure what the term is for that) when you for example want to do something like this:
// Fine in JS, but not in C#
transform.position.x += 1;
// C# way:
Vector3 temp = transform.position;
temp.x +=1;
transform.position = temp;
Yes, that javascipt feature is quite handy - I'm forever having to make copies of transform.position! :)
Answer by testure · Aug 13, 2011 at 06:21 AM
Probably because the programmer isn't very experienced and only knows how to do certain things in each language.
But to answer your question- the main difference in unity for C# and Javascript/UnityScript is generics. Most casual unity developers aren't even going to know what that is, so for all intents and purposes, nothing :P
I dont think I am a casual unity developer, but I don't really understand what you're saying about generic differences. Would you care to elaborate a bit?
You can't declare generics in JS, only use (instantiate) them. It's one of the few things I use C# for, since I prefer the cleanliness and clarity of JS code. I do however use #pragma strict
since I definitely don't want the looseness JS allows otherwise. As you say, it can be a headache mixing, so I only have "plugin" type stuff (indeed, in Plugins/
) in C#, not project-specific stuff.
I disagree.. In my opinion, if you have sufficient experience in C# there is absolutely no benefit to using JS. Conversely to that point- if one has sufficient skill in JS and has no need of generics, there is no benefit to using C#. Cleanliness is a moot topic, because you can read C# just fine when you've been looking at it for years and not going back and forth between syntactical rules. Not to mention the 'cleanliness' thing is a matter of opinion, I personally think JS code in unity looks sloppy- but that's just me. I'm all about functionality, and if it can do the job so be it.
As far as bouncing around between languages, I can see no benefit to it at all, other than preserving a personal preference to use one over another (as it seems to be in your case). Not to mention there are obvious downsides when you work on a $$anonymous$$m of developers that are just using whatever language they want, whenever they want ;)
@testure - that shows how inexperienced you are with the benefits of JS ;-)
But seriously:
Generics are essential for performance.
Cleanliness does matter - coding efficiency always matters, or else we'd all be using assembler since "it can do everything that other languages can do". In C#, you need to write and maintain up to 10 times as much code to achieve the same thing.
In C#, you have to type twice as much code whenever you need to access members of transform.position
, you have to write 10 times as much when you need a 2-line class (eg. a pure property class), and when you have to use yield
, well, then you actually feel like you're program$$anonymous$$g in assembler.
Yes, Extension methods can make a few things more concise in C#.
Yes, there are logistical considerations. $$anonymous$$y rule is simple: everything in JS except generic utility classes - even a large $$anonymous$$m could follow that if necessary. It depends on the background of the $$anonymous$$m of course - are they web JS developers who need to learn how JS should have been (i.e. J# / Unity's way), or are they C# programmers? Considering Unity's core developer targets, they could easily be either (and both).
If Unity made a few changes, like:
magic $$anonymous$$onoBehaviour wrapper for C# as it does for JS
add x, y, z pass-through members to Transform and similarly for other awkward classes
Coupled with using C# var
(which @testure may also despise), the gap would certainly narrow. I can't see what can be done about yield
though.
Anyway, the point is, insulting third-parties with self-aggrandizement and contradiction is, in my opinion, a poor answer to the question. Unlike most of your other answers.
To imply that C# and Javascript's capabilities of generics are the same is also 'plainly wrong'.
Go try to create a generic class in Javascript, then come back and tell us all how well it worked out for you.
"the main difference in unity for C# and Javascript/UnityScript is generics" is a true statement, regardless of what kind of semantic argument you want to pull out of it. And for the record- Warwick already made your point about being able to use generics in javascript, and nobody disagreed with it.
I apologize if I've ratcheted things up in this thread, I will consider this my last thought on the matter. I just feel like a quick answer I posted is being over-scrutinized and attacked for pedantic reasons.. but I've said all I need to on the matter so it doesn't merit any further involvement from me.
Your answer
Follow this Question
Related Questions
Can't make script work. & Script help. 1 Answer
set a var to a function in another script 2 Answers
Accessing function from another script won't work 1 Answer
Call GUITexture and script 1 Answer