Generation C Using Code Generation to Make Software




























- Slides: 28

Generation C Using Code Generation to Make Software Development More Fun Iași, October 2016 by Patrice Brend’amour - Product Manager G 3 - Compu. Group Medical

Imagine you have a REALLY cool idea! Iași, October 2016

I want to create the next Twitter Iași, October 2016

But instead of this YAY! Let’s start implementing all those cool ideas I have! Iași, October 2016

You often have to do this I hate doing all this infrastructure work! Create test mocks Create DAOs Convert objects Duplicate objects on client & server Map database results to objects Create “copy&paste” tests for everything Iași, October 2016

An Example: Converters Iași, October 2016

Pretty boring, right? Iași, October 2016

So, what’s the solution? Just live with it? Add more abstractions? Magic? Hire an intern? Throw a temper tantrum? Iași, October 2016

What if we just lived with it? Pro • Works • “It’s our job” - “I fully control what it’s doing” Con • A lot of work • Boring • Error prone Iași, October 2016

Ever seen this? Iași, October 2016

So hiring an intern it is? Pro • (More) Work will get done Con • Costs money • Will not scale • Error prone Iași, October 2016

Abstractions are the way to go, right? Pro • Less duplicate code • Looks “professional” & clean Con • Doesn’t solve all our issues • Debugging gets harder • Increases complexity - Complexity kills! Iași, October 2016

So, what’s the solution? Just live with it? Add more abstractions? Magic? Hire an intern? Throw a temper tantrum? Iași, October 2016

Goals • Have simple, readable and debuggable code • Reuse existing information, i. e. use the same information in multiple places within a project • Limit the amount of tests to a meaningful set • Speed up development - eliminate the boring parts Iași, October 2016

Well, maybe we can automate this… Scripting? Code Generation Something else? Iași, October 2016 Runtime interpretation?

“Source code generation is the process of generating source code based on an ontological model such as a template and is accomplished with a programming tool such as a template processor or an integrated development environment (IDE). ” – Wikipedia Iași, October 2016

Why Code Generation? • It allows us to reliably create standardized source code artifacts • Generated code (ideally) looks, feels and behaves just like handwritten one • Less error prone as it is based on templates • Code is easier to change Iași, October 2016

Idea Template Code Model Template Iași, October 2016 Code

The Model • Provides the information for our code generator • Can really be anything that provides structured information, e. g. • a JSON structure • a DSL model instance • even a set of classes Iași, October 2016

The Template(s) • Provides the blueprint for the “to be generated” source code • May contain complex logic “IF attribute is mandatory, generate the validation for it” Iași, October 2016

The Generator • Takes the model and template as input and generates the source code • This is where the ‘magic’ happens! • Many forms of such generators exist, depending on the domain and language Iași, October 2016

Demo time powered by Iași, October 2016

A few more examples Things where Code Generation might help you: • • Creating SQL scripts Creating Tests (esp. integration & database tests) Providing REST APIs Generating API Documentation Creating Event Classes (e. g. for Event Sourcing) Converting objects Managing Translations … Iași, October 2016

Lessons learned • Code generation can replace up to 60% of your handwritten code. The boring parts! • Once you’ve started using it, you really don’t want to go back • The more you use it, the more places you find where you could also use it • It’s fun! Iași, October 2016

To sum up • Code generation is really cool and useful • It uses models and templates to automatically generate standardized source code • As the code is based on templates you can easily change it • With our goals in mind (readability, simplicity and debuggability), generated code can be just like handwritten one. Just way more reliable Iași, October 2016

How to continue from here? • Spread the word about Code Generation! • Try one of the many tools (e. g. Swagger, Spring Roo, Eclipse Acceleo, …) • Or create your own generator! It’s fun, too : ) Iași, October 2016

Iași, October 2016

patrice. brendamour@cgm. com https: //linkedin. com/in/pbrendam our http: //github. com/drallgood Iași, October 2016