I fell out of love with Lua

I used to like Lua a lot and now I don’t. I think anyone intending to add a scripting language to their game/engine should seriously consider using Javascript instead.

This opinion isn’t based on anything logical like speed or implementation. Lua has served us really well in Garry’s Mod in terms of both speed and speed of implementation. It’s worked pretty much flawlessly.

Syntax

My real issue with Lua is the syntax. It’s different for the sake of being different. A claim is always made that the language is made to be easy to understand. There’s a lot of places where this falls to bits. Want to add two strings together?

mystring = front + back;

No, that’s too hard to understand right? Lets do this instead.

mystring = front .. back;

Why is that easier for people to learn? Why are // comments harder to understand than — comments?

How do we test if something is not equal to something? Which one of these makes the most sense?

one ~= two
one != two

The ! isn’t used anywhere else on Lua, so there’s no real reason for them to use ~ other than to be contrary.

Bad Habits

By using Lua you’re teaching a bunch of people the wrong way to program. You’re teaching them a language that isn’t going to help them in the future. You’re teaching them that arrays start at 1 not 0.

Missing Features

Lua doesn’t have unicode support. This is a pretty big deal in a modern day scripting language, you shouldn’t have to treat strings like binary data and you shouldn’t have to install addons to support unicode.

No “var++” or “var += blah”. The whole point of scripting languages is to make it easier to code.

No continue keyword.

Javascript is the future

Javascript is used all over the web. It’s only going to end up getting used more and more. Google, Mozilla and Microsoft are all working to make it faster and faster.

Ultimately if we have to teach people a scripting language, lets teach them a language that will serve them in the future. Sure the jump from Lua to Javascript isn’t that great, but if you skip Lua there’s no need to make a jump at all.

83 thoughts on “I fell out of love with Lua

    1. As a C# developer, I hate Python. It’s not particularly hard to learn or anything, but it bugs me to no end that the language enforces stylistic choices as syntax requirements.

    2. @Masaaki
      I’m a C# developer and I think python is great for small programs (probably for scripting, too).

      Indenting in the right places is hardly a coding style. If there is incorrect indenting, then it’s nice that the compiler picks it up.

      What I would complain about is that it’s a dynamic language, which can be bothersome once the scripting project becomes bigger. I’m sure everybody would love to script in haskell instead. Right, guys? … guys?

    3. @Dave I’m a hobbyiest programmer/game programmer with a uni minor in CS. I until very recently insisted that Python was the master race. Then I started work on two things: A complex timer (for game speedrunners) and a game. I felt like I was spending so much time fixing typing related errors and working with the weird inheritance and looking for libraries/reinventing ways to do things.

      The language I’m probably most familiar with besides Python is Java. I hated Java. The syntax is clearly inferior, the braces felt clunkier and harder to read than whitespace parsing, it lacks any elegant functional support. But then I switched the timer to Java for a cross-platform hotkey library. Suddenly the code became so much easier to write. Part of it is a better IDE (IDEA vs PyCharm), but a lot of it is that Java forces you to write correct code. Not elegant, not necessarily very readable, but correct. And this is the number one issue in getting large projects to scale up. I still hate Java, but it’s better than Python for anything of any size.

    1. I’ve been looking at TypeScript. It’s based on JavaScript, so all your JS code “just works”, but it adds a bunch of sorely needed features.

      Strongly typed variables? Yes. Proper class structure with public and private variables without needing to figure out how to use closure to do it? Yes please. Interfaces and inheritance without needing a framework or writing your own code to manipulate prototypes? Yay! Autocomplete/Intellisense? Yessss.

      Best part is it compiles back to JS (automatically, if you use Visual Studio) so you can use TS anywhere JS works. Not just browsers either, it has autocomplete support for node.js functions.

      Haven’t actually used it yet but I’m going to have to try it at some point.

    1. If you wonder why whitespace shouldn’t be syntax, you’ve never had a coworker who was visually impaired. That crap is terrible for him.

  1. I started out using Pawn as my first scripting language with Lua coming in a short time later. Lua was definitely a over hyped language but it was documented very well, unlike Pawn where it took the community several years to find optimization tricks. Pawn took a C-like approach in it’s syntax and I’ve always felt like it was the best language to learn for a beginner.

    I haven’t used JS in a while but my first experiments with it were always positive. If it makes the development process easier, cleaner and more robust than I’m all for it.

  2. Javascript is lovely, can’t deny that. But the lack of “type” sucks because no “intellisense”, that is where Typescript helps a lot.

    C# is nice and it can be compiled to Javascript.
    F# is also nice and it can be also compiled to Javascript.
    Python can also be compiled to Javascript.

    So yeah, Javascript is like “assembler” of the future.

  3. I’m surprised that someone who has used Lua for so long doesn’t know the reasons behind the syntax decisions. All the ones you are complaining about are well thought out decisions.

    1. Ah, you mean the original language designers didn’t do everything in Lua for no reason?
      No shit?

      Just because they had a reason doesn’t mean we need to like the end result. They could JUST as easily have gone with C-style syntax, and I bet it would be a much better language for it.

  4. Lua may be syntactically weird, but javascript is a semantic mess. “Javascript’s prototype-based inheritance is great!” said no one, ever. Dartlang was designed from the ground up to be semantically simpler than javascript. The saner semantics means it has a higher optimization threshold, comes with a base class library and a package manager. Optional type annotations offers better tooling and analyzing. VM designed by the same folks who created V8. Seriously, go check it out.

  5. In what way is JavaScript more convenient? Sure it does support increment/decrement unary operators and starts its indices at zero. But it also has the unique concept of prototypes, very weak typing, “for each”-loops are somewhat bleeding edge and just look at the comparison table: http://dorey.github.io/JavaScript-Equality-Table/ – it’s a mess.
    Every programming language has its own strength and as you pointed out, JavaScript’s main usage is in the Browser, because it was made for it. Everything else just seems to be a hack in my eyes.

    1. i cannot confirm that. serversided js (if written by professionals) is done in ES6 and mostly incompatible to browsers as long as you dont use bleeding edge testing releases of browsers with harmony flag. change is coming! in my opinion everything will become even more complicated but meh.. finaly js can be written with even less lines of code.

    2. I’m a professional software engineer. We have JavaScript that runs on the server side as well as client side. Have you even heard of Node.js? Use “===” to check type equality. It’s not that hard. Do you even code? Yikes.

  6. @Masaaki
    I’m a C# developer and I think python is great for small programs (probably for scripting, too).

    Indenting in the right places is hardly a coding style. If there is incorrect indenting, then it’s nice that the compiler picks it up.

    What I would complain about is that it’s a dynamic language, which can be bothersome once the scripting project becomes bigger. I’m sure everybody would love to script in haskell instead. Right, guys? … guys?

    1. I frequently use indentation to keep code sections visually tidy, often abusing Visual Studio’s auto formatting. What bugs me about python is that trying to do the same thing there could actually change how my program executes.

  7. Lua has bad syntax and generally the interfaces provided by applications (like WoW) are HORRIBLE (with the exception of GMod, awesome job on that).

    Python is just absolutely awful and pours fuel on the eternal tabs vs. spaces problem in addition to being relatively slow when compared to JavaScript + (last I checked) lacking in the IDE department.

    Ruby is way WAY too slow, memory hogging and “wild” (monkey-patching madness, incomprehensible error jungle, etc.).

    JavaScript is very good once you understand variable scope and how awesome the prototype system is and never not use an OO-approach again. Only downside I can think of right now is the lack of operator overloading, but that could bring a bunch of new problems with it. I’m also suspicious of the upcoming ECMAScript 6 and the whole “let” keyword thing, which I think is going to cause confusion and a bloody mess.

    Duck typing does have it’s downsides though. I would love a good fast static typed scripting language with C-like syntax and IntelliSense level IDE, which is much easier to do and reliable with static typing. C# is a good example, but may be out of the question unless you’re already developing your main application on .NET. It’s very nice to catch syntax errors and bad calls and stuff without running the possibly-very-huge-and-complex application.
    TypeScript seems like a dirty hack, where in the end you need to know the implementation on JavaScript level in order to debug effectively.

  8. You’ve obviously never tried Ada which is a decent programming language with practically the same speed as C/C++ and which everyone rejects because they think it is crusty – they judge it by its syntax – which looks a bit like Lua’s syntax – and not by its actual utility.

  9. Take a look at Duktape guys!
    I think it’s a very promising project.
    Even on alpha stage, I feel it’s pretty mature.

  10. I can’t believe no one has ever thought of just rewriting some of the lua source to fix these semantic issues for c-like syntax.

  11. “Bad Habits

    By using Lua you’re teaching a bunch of people the wrong way to program. You’re teaching them a language that isn’t going to help them in the future. You’re teaching them that arrays start at 1 not 0.”
    Sorry but I was able to move over to C++ I never would have if I did not learn Lua.

    To be honest Lua kind beats Java the code is short and far less intimidating to new users.

    For example
    Lua:
    function lol (cake)
    somecake = lolz
    if (cake == somecake) then
    die()
    else
    yay()
    end
    end
    lol(cake)

    JS:
    function factorial(n) {
    if (n === 0) {
    return 1;
    }
    return n * factorial(n – 1);
    }

    Sorry but Lua is just down right better.

  12. I agree with the “Bad Habits” and “Missing Features” parts. But Javascript is certainly NOT the future, but the past. Even though Javascript is being “faster and faster”, it wont reach LuaJIT’s speed. Also javascript is missing some cool stuff such as metatables. And I really like lua’s “then” and “end” instead of {}, which makes the code smooth & clear, more “human”.
    I don’t know who told you that javascript is “the future”, but I would actually suggest to give him a rope and a stool for his birthday.

  13. The future is not Javascript. That’s like saying Ubuntu is the future of Linux. Some hipster will come out with a new language and other hipsters will jump onto it so they can learn it before it’s mainstream and be hip.
    Coding languages = real life languages. there is no “ultimate” language.

  14. Garry, I began by hating Lua, but now I’m in love.
    What really makes me love Lua these days is the metaprogramming, specifically metatables.
    It really is an agile and flexible little language, and it’s very close to Lisp.
    Everything you think you’d be able to do in Lua, you certainly can.
    Want to declare a variable’s value using a conditional? Sure thing!
    Want to do the equivalent of python list-comprehensions? Sure.
    Closures? No problem!
    Recursion? I do tail recursion easily!

    Javascript is as over-rated as node.js, it’s a fast language but it’s hardly elegant compared to Lua.
    You might like developing your own Lisp for RUST, it certainly would make things simpler when it comes to serialization. Hell, if you don’t, I will (as a mod that acts as a mod loader).

  15. “This opinion isn’t based on anything logical like speed or implementation. Lua has served us really well in Garry’s Mod in terms of both speed and speed of implementation. It’s worked pretty much flawlessly.”
    What speed? Garry’s Mod is the slowest and most bugged game I ever played! I have to spend few hours to play it for 1 hour! Why I have to download addons that I have to play on server? (fully updated) It even causes Steam crash. Did anyone think about fixing it? Optimizing for slower computers? (last thing won’t work, played on computer 4x better than mine, still doesn’t work…)

  16. Sorry, but citing minuscule syntax differences as primary concerns about a language is probably the most moot criticism one could ever make. Pretty much only the lack of “var += blah” is the only valid concern, and even that is not a huge problem.

    Also, JavaScript’s broken comparison operators, lack of proper block scoping, and the fact that its prototypes are pretty much a gimped version of Lua’s metatables make it hard for me to find it as a better alternative to Lua.

    All in all, sorry, but I find it hard to take this post seriously.

  17. Javascript is taking over the web, surely, but there are some thing that tick me off about it. The worst part I think is the way to create classes. Or rather how you _don’t_ create them – you create factories for objects.

    There are other things too, some of them can be pain, but most of them are just amusing :p

    Pics related:

    And to close off: http://puu.sh/geEw7.jpg

  18. Lost a lot of respect for you because of this post.

    Apparently you have no understanding of the history of programming language families. You say the syntactic choices of lua are inherently wrong without taking the time to grasp that lua is descended from ML-type languages, not C. Better call out the lisp’ers too, they must surely be programming the ‘wrong’ way.

    And recommending JS over lua? That is just laughable. Ever try embedding a JS engine? Ever even compiled one?

  19. Garry, you’re an idiot. Your complaints about Lua are utterly asinine and Javascript suffers from much worse, real issues like the fact that it moves code around in an opaque and offensive fashion.

    Honestly, I’ve never really expected many intelligent things to come from you. This post is reinforcing that belief.

    1. Lua silently returns nil for non-existent table slots, potentially propagating runtime errors far from their source, requiring external source analyzers. Its use of longjmp is incompatible with some automatic memory management systems.

  20. A few things:
    * Lua’s syntax isn’t different “for the sake of being different”, it just comes from non-C languages. (e.g. Modula, a dialect of Pascal)
    * 5.3 has UTF-8 support, but somebody already mentioned that.
    * Saying “It’s the future” is a shit excuse to justify anything. That crap is what made headaches like .NET and UEFI so popular.

      1. I never once vouched that the JVM was better than .NET. I come from a low-level sysadmin background, so I never cared for either much. Classes being entry points makes me want to vomit, and while I don’t particularly dislike OOP, I hate it when languages promote it as some sort of end-all solution to every problem. That’s all just a matter of preference, though.
        But since you brought it up, I will say that .NET took the _one_ advantage of using precompiled bytecode on a process virtual machine and completely dragged it through the mud. The reason people I have talked to like Java (or more specifically, the JVM) is because binaries are cross-platform, and still have comparable performance. .NET takes that and completely destroys it by trying to make it Microsoft-exclusive. Sure, there’s always Mono, but it has always been a cut below the Microsoft implementation in every comparable aspect, not to mention being ridiculously slower on non-Windows systems.
        Since I’m commenting, I should probably mention that Modula isn’t technically a “dialect” of Pascal, but I think anyone with Google could’ve inferred that much. Also, there are plenty of languages that have 1-based arrays; Smalltalk and Fortran, for instance. (inb4 “Fortran is a dead language hurr”) And even then, you can still _assign_ values to 0. Thinking of Lua’s arrays as the same as arrays in languages like C is a vast misunderstanding of the language itself.

  21. The worst thing about Javascript is its name, it misleads a bunch of ignorant morons and their mothers into thinking it’s somehow related to Java

  22. I once spent a week looking for a bug in a piece of JavaScript code that was caused by the + operator being treated as concatenation rather than addition. It thought 1 + 1 was 11. I like JavaScript, but using .. for concatenation in Lua makes far more sense; a strongly typed language like Java or something can get away with a + because everything is type-checked.

  23. A lot of the reasoning for Lua seems to come from Matlab and Fortran (1 based arrays, loop syntax, string concatenation), but there is a lot of Lisp concepts too (recursion, anonymous functions). Lua was originally used in control systems for industrial plants, when there were export restrictions on US products.

    If you’re target platform is only the desktop or server, then there are other languages better suited. But for embedding in a game or microprocessor, Lua has strong points.

  24. All your points aren’t much important to me, though I think that Lua is awful, too.. as well as JS
    Array indexing, string concatenation isn’t the issues for me

    Much bigger issue to me is
    – damn freedom everywhere
    – no way to simply look what arguments some imported function accepts
    – not obvious, write-only code
    – global, uncontrollable state a one can add ANYWHERE

  25. I know this is old but… Garry… why :(

    Javascript has so many quirks it’s literally a step up from reading assembly or the obfuscated mess that is most Haskell code.

  26. Lol your a fool right off the bat. In JavaScript you often need to add numbers in the format of var1 -(var2*-1) so that it treats it as a non string. Fool.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s