DEV Community

Discussion on: New Web Framework?

Collapse
 
tux0r profile image
tux0r

I prefer to write code. I don't trust generated code.

Collapse
 
monknomo profile image
Gunnar Gissel

Maybe I've spent too much time in the Java world with Spring and Hibernate, but I don't mind generated code. The main thing is to always be able to regenerate it, imho. Never modify it, and don't ask it to do anything it isn't designed for

Collapse
 
cjbrooks12 profile image
Casey Brooks

I don't trust generated code either in most cases. It's too easy for scaffolds to fall out of date or require a lot of work to make the generated code do the additional stuff you need.

However, Java's annotation processing API is really nice for compile time code generation, so you know it is always up to date and works exactly as it should. Code generation that gets destroyed and recreated continually can be quite invaluable.

In my opinion, code generation can work but it shouldn't be static generation, only done once. It should be part of your build process.

Collapse
 
_pankajc profile image
Pankaj

Is this dynamic code generation responsible for slower server start ups in spring boot? I'm learning spring boot right now and love it so far except for the slower server start ups.

Thread Thread
 
tux0r profile image
tux0r

Probably. Everything that has to be calculated for each new visitor inevitably leads to additional latency.

Thread Thread
 
cjbrooks12 profile image
Casey Brooks

I'm not too familiar with Spring Boot, but code-gen would make the compilation step take longer, not the server startup. Java code generation libraries (such as Dagger and Butterknife which are common on Android) actually dramatically increase application startup times, because all the hard work has been done at compile-time.

Taking Dagger as an example (dependency injection), the generated code doesn't require Reflection as a runtime-library would. It looks for @Inject annotations in your code, finds the @Modules which provide those dependencies, and writes new Java source files to create the object for you. Super fast, no reflection required. Spring Boot does runtime injection, and so uses Reflection to find the @Inject/@Autowired annotations, and then uses Reflection to call the constructor or set the fields as necessary, which is known to be slow. This is in addition to the many other things Spring is doing on server startup (classloading, resolving routes, connecting to databases, etc.).

Thread Thread
 
stealthmusic profile image
Jan Wedel

A spring boot app with 1000s of beans and component scanning still starts in 1-2 seconds. Most of the startup time is consumed when using Hibernate and flyway eg.