185 points · by ingve · 10y ago · 102 comments
Some highlights:
- Great type system, with type inference.
- Full Java interop. You can literally copy/paste Java code from Stackoverflow and it's automatically translated to Kotlin. It doesn't feel like 2 different languages. Working with Java libraries doesn't feel awkward or foreign, like in many other JVM languages.
- Beautiful syntax that allows to construct some pretty advanced DSL, yet because it's statically typed the IDE can always resolve the DSL constructs and take you "under the hood" to the actual code that's going to be executed. This is in contrast to languages such as Ruby where most of the time when you encounter a DSL construct there's no easy way to understand what's going on.
What I like to know is the following:
What are some of the challenging issues of using Kotlin?
Did you start from Kotlin or migrated from java to Kotlin?
What drove the decision to use Kotlin in such a visible app?
What are the top 3 - 5 improvements or things you want to see?
Thanks
1. Kotlin issues:
- Libraries that use reflection (such as Gson) can break some assumptions that Kotlin makes (for example, a read-only field may actually change). There's maybe 2 or 3 little things like that that may catch you by surprise, but once you figure them out it's easy to work around them.
- Stack traces in lambdas are obscure sometimes. Sometimes you won't get the line number.
- Proguard integration has proven challenging.
2. We started from Kotlin.
3. Learning Clojure and Haskell has made me yearn for more functional languages and better type systems. Kotlin has that, in a neat syntax that's very beginners friendly.
I'm firmly convinced that a good programming language needs:
- Strong library ecosystem
- Static type system + type inference. (I won't get into the static/dynamic argument here. Suffice to say that there's a whole class of bugs that you can avoid with static typing, and type inference makes the "but I hate these types cluttering my code" argument irrelevant)
- Killer IDE support (essential for productivity. If you're writing a large application without using an IDE that gives you code inspection and navigation, you're wasting your time)
Kotlin has these 3 things.
4. Top improvement: can't wait for Kotlin 1.1 to be released, and start using coroutines. Other than that, I'm happy with it.
At the moment I use vals, but it still requires me to mark injected dependencies as nullable (and coerce them with !! on use), which feels like I'm fighting the type system.
It would be really nice if there was some sort of "special" null that was of the non-nullable type, but could only be used when initializing variables.
If/when I finally need to get around to learning to make an Android app it's definitely the first place I'll look.
Apps written in Xamarin and kotlin work very differently :
-with Xamarin you have to add the Xamarin runtine and depend on its integration. This brings several downsides : http://www.slideshare.net/KonstantinLoginov/we-are-ditching-...
-Kotlin outputs bytecode, so from the outside, it is just the same as any android app written in java. The output is a standard android app with one more library (the standard kotlin lib with all its base methods)
I also don't get your point about how lame Xamarin apps look. Xamarin apps use native layout components (could be wrapped with lowest common denominator abstractions in case of Xamarin.Forms, but it's optional), so they're 100% capable of looking the same way your native app would.
I have also found that if you are using LibGDX Java game framework, it's very trivial to convert it to Kotlin. I have been doing that pretty extensively and it has been very pleasant. Feel free to check my GH for some examples. Most notably, the Bayesian Classifier.
Also be sure to check out the Kotlin slack room. It is filled with a bunch of great and active people.
But most importantly, just go try it. If you come from Java and are hesitant to leave given all the nice libraries available in Java, Kotlin is for you.
It's something that I tend to add in as needed or when I have decided that a game I'm working on warrants this level of engineering. (i.e. I wouldn't care about it as much if I'm just programming pong or Tetris.)
I have found myself in this boat many times. I have coded games in Purebasic, TI-83/89 Basic, Z80, C/C++, Java, Kotlin, Javascript, Haskell, Swift, etc. If you are an indie/hobby developer, don't worry about the language too much. Just do it in what's comfortable and enjoy the process. That alone will increase the odds of you completing the game by 50% (random number).
1. Allocations of any kind are often the most expensive operation per frame, or at least some of the ones that are fixable (you can't just not have physics in some games). As such, avoid allocations per frame.
2. Notice transition screens, loading screens before levels, brief artistic animated overlays (things like ROUND 1 - READY!). These just aren't for fun or to hide just disk IO, they can actually be used as a time to do controlled allocations. Let's say you're doing a Metroidvania for instance - perfect time when changing areas to load new stuff.
3. Use fixed-sized data structures like arrays. Vectors, Lists, any kind of dynamically resizing data structures are generally your enemy. Sure you can use them in non-essential parts, but it's an easy thing most of the time. These help you control the amount of allocations, and moreover ensure that your game runs predictably, especially when pushed hard. You shouldn't be letting your game get into insane states that would allow allocating 5 billion bullet objects even if your vector and a machine with 64 gigs of rams can handle it. Obviously some exceptions, but there's sound reasoning.
4. Operate on fixed-sized data structures in sequence. It's harder on the JVM, but in most platforms you can start trying to use this to help the runtime optimize for you, or better yet, get things to fit nicely into cache lines. Things like "events" and "callbacks" may seem helpful, but they are actually your enemy, even on the JVM. You are better off using a queue or mailbox approach if you need more performance and less GC surprises.
5. Pool. But don't just pool and allocate, make sure you are pooling efficiently. On some platforms, this means tightly packed structures with no holes. One cheat you can do is actually use an ID system and work with that to index into an array-based pool by getting via index instead of a search. Using dicts/maps for this is the wrong approach for so many reasons. There's an older GDC presentation about this somewhere I can't remember, but it talks about this in relation to entity systems and uses the term prius a lot. Free list might be something else on your google for this.
6. Pack your textures when you can. Do things like use a sprite sheet. It's not just the GC pauses that are the enemy on the JVM, but also hitting the file system. This will help reduce it at the cost of potentially more memory for a single texture.
7. Don't go wild with classes. Try a data-based approach. This cuts down on GC and is the basis for some flexible architectures like entity systems. Of course you may still need classes as containers if you are using Java, but you'll end up with less ugliness and perhaps less classes and instantiations of those classes this way.
8. Check out your JVM flags and tweak things more if you must. Check out https://docs.oracle.com/cd/E40972_01/doc.70/e40973/cnf_jvmgc... for an example. Not the best guide, but my first google hit to just demonstrate. On a semi-related note, don't do stupid stuff like reflection or dynamically load in things that are just going to make the JVM angry. Simple is better than clever here.
9. Use a good time step for your game loop where you separate your sim time. It doesn't necessarily fix GC pauses, but it does ensure that if things are running slow you can respond and interpolate.
10. Related to the last point, try to do more per frame and don't assume all work is the classic game loop of render per tick. Naughty Dog did an interesting presentation on The Last of Us port to PS4 you can read about where they talk about optimizing the way they process frames. If your game permits similar approaches, it gives you a chance to squeeze out some performance this way.
I have many more tips, but this is just off the top of my head. Some of these might seem like micro-optimizations, but they actually end up shaping your game's architecture so it is definitely important to think about it early if you are serious about not putting yourself in a performance corner.
You can certainly develop games on the JVM. I personally don't recommend it unless it's for pleasure. C or C++ is still the way to go. I am sure you can have fun with Rust too doing it, but there's just so much ecosystem and people talent tied up in the former choices that it effectively makes anything else far behind. That said, you can write a good game in any language, and if you are just sane about your game it will be fine. Even with GC pauses, for some type games, ex: strategy that isn't real-time, your users would barely notice and it wouldn't effect gameplay. In short, be pragmatic no matter the language, stick to some of the above, and enjoy yourself and just get it built. Language is the least of your worries in the end, but still worth paying attention to on some level.
I've casually read a lot of people's advice on game dev, and while some of it is reflected in what you said, your comment kind of ties it all together. And it confirms something I've been thinking for a few months (possibly years)...
Everything needed to write a simple game is right there in C, using structs, pointers, and functions! Anything beyond that is over-engineering. And the conveniences that languages like Java bring are just simply not worth it in this context.
Because as far as I can tell, writing a game is basically writing a tight loop that enumerates arrays and calls functions on them. There's basically no need for any OOP beyond the simplistic method of using structs, pointers, and functions.
And considering I've just had 4 years of practice using data and functions (full time job writing Clojure), I think I'm a lot more comfortable with this pseudo-OOP approach than I would be trying to jimmy-rig Java to basically be C.
So that's what I'll probably do. Write a SNES-style metroidvania in C using SDL2. (Although I'm open to alternative suggestions, but from what I've read, SDL2 is probably best for this job.)
Thanks for your advice and for bearing with me as I think this through in this comment :)
[1]: https://2016.javazone.no/program/kotlin-ready-for-production
https://www.techempower.com/benchmarks/#section=data-r12&hw=...
On the other hand, it's controlled by Jetbrains—the company that now offers a "yearly license" of many of its compilers at the same price point it once sold them for, after which time they forcibly downgrade the software their users have paid for. It's not illegal and some even agree with the practice on an ethical level, but it's clear that Jetbrains is all about hoarding as much consumer surplus for themselves as they can.
If only Kotlin were truly an open project and well supported outside of the Jetbrains marketplace, e.g. by VS Code, I'd be cheering it on.
-former webstorm/rubymine customer
I'm not sure what more is required to make it truly open.
Once Jetbrains no longer controls Kotlin's future and most Kotlin users are not Jetbrains customers, then I'd consider it truly open.
VS Code is miles ahead of Jetbrains offerings and has no yearly subscription fee.
MS was indeed one of my least favorite companies half a lifetime ago, but they've changed business models and are now pursuing a growth strategy (particularly wrt services) and have abandoned the fight to capture consumer surplus at all costs.
And yeah don't forget Eclipse Kotlin plugin, fully open-sourced)
JetBrains open-sourced decades of their hard work, building world's best IDE. And you can benefit from it for free.
You don't even have to pay for webstorm/rubymine. Just install plugins you need to Intellij CE)))
UPD. When I pay for their job, I receive real stuff and not some obfuscated shit. The stuff that will be compiling even when the subscription ends.
What happened? Could you please elaborate?
The tooling is still a bit rough around the edge compared to the base Android one, mostly because the android tools team really does an awesome job.
It is still useable though, and the language is so much better than java that it is a breeze to use.
The only downside is that you have to interface it with java code, which brings a couple of issues.
I see a few people have adopted it - what do you guys see as the future of the language? Do you think java8+Lombok is moving in a direction that makes kotlin useless ?
Great language, greater IDE.
Kotlin is missing all of the most powerful Scala features. No type classes, monadic comprehension, higher kinded types, adhoc polymoprhism. :(
I do like many of the more powerful features that Scala has, but Kotlin strikes a right balance of simplicity and power in my opinion.
That being said, yes Scala has a steeper learning curve than say, Kotlin or Go. But if you're going to devote the I don't know, next ten years of your life to working in a particular language, does it really matter if it takes two months vs. four months to ramp up?
Plenty of "ok-ish, but not too hard to learn" languages proliferate in that kind of climate, like go for small web services (where Erlang was too much of an investment). The Unix approach of 1001 ad hoc sub-languages (awk, pic, ed...) adapted.
Quite different from e.g. a J2EE developer's approach, who tends to think of career-defining technologies and languages, where Scala seems like a wise investment.
1 - Install plugin into Android Studio
2 - Configure the Gradle plugin
Enjoy Android development within Studio.
Scala:
Follow this several pages long tutorial:
https://www.voxxed.com/blog/2016/10/setting-up-scala-on-andr...
Try to enjoy while working around issues with Studio integration and common Android libraries.
A programming with more features is not necessarily better. The limited features of Kotlin (compared to Scala) are an advantage, if you change your point of view. Some advantages:
* easier to learn (a Java dev will be more productive than in Java in a day or two)
* easier to maintain since Kotlin doesn't allow so much hard to understand code constructs
* fewer discussions about the one true style or way to do something
* it's easier to build good tooling
* much faster compilation
* smoother Java interop
* performance implications are more predictable