DEV Community

Discussion on: Kotlin - The Good, the Bad and the Ugly

Collapse
 
kayis profile image
K

"Imagine frameworks such as Spring or Hibernate, which generate bytecode at runtime that extends your class."

Heavy use of code-generation is always a red flag to me.

Collapse
 
martinhaeusler profile image
Martin Häusler

I think we have to differentiate between source code generation and byte code generation here. Both are entirely different beasts with individual pros and cons. In this case we talk only about byte code generation. At the very least, this is code you will never see, which will never go out-of-sync with your source code (because it is generated at application startup).

Overall, I would strongly prefer to work entirely without code generation in any shape or form, byte or source. But in the Java server world, the hard truth is: pretty much any notable server-side framework (JPA, Spring, Spring Data...) has to generate bytecode to achieve its goals in one way or another. You can't avoid it. And usually it doesn't matter, because you never see it happening. As long as you don't declare your classes explicitly as final (which I hardly ever see people doing), you'll be fine. With Kotlin, it is just the other way around: declare your class as open, or face inexplicable exceptions at runtime. Like I said in another comment, the Kotlin language developers must have seen this coming, I can't imagine that this came as a surprise to them.