DEV Community

Kristjan Grm
Kristjan Grm

Posted on

your ways of writting lots of duplicate / similar code

I've been developing basic CRUD application. The application itself is fairly simple, but I waste lot of time writting code reading data from database / writting data to database.
It consist of:
FRONTEND
-creating Angular HTML template
-creating typescript interface
-creating Angular Typescript code
-creating Angular Service for said component
-adding additional code for popups, code sharing between components

BACKEND
-creating java class for said component
-creating INSERT, DELETE, SELECT, UPDATE methods
-creating METHODS for getting data for autocompletes

DATABASE
-creating procedure, queries for reading / updating data.

All above is fairly simple and trivial for applications consiting of 10 elements. But for applications that have something like 100 inputs fields, and 20 forms to read from. and every form having few autocompletes etc... Writting everything above by hand becomes quite boring / time intensive.

I hope I wrote my post understandable enough to get my point across

Top comments (2)

Collapse
 
idanarye profile image
Idan Arye

This is a controversial opinion (and sadly - I'm not being ironic here...) but I do believe that developers are allowed to add their own layers of abstraction. Knowing where to put that layer, of course, is the main problem - but as a rule of thumb you usually want a single source of truth - or as close to "single" as you can reasonably manage.

Since the Java classes will be the most painful to auto-generate, I'd make them that source. You want to add annotations on their fields:

public class Person {
    @Caption("First Name")
    private String firstName;

    @Caption("Last Name")
    private String lastName;

    @Caption("Phone")
    @InputPattern("###-###-####")
    private String phone;

    @Caption("Company")
    @Autocomplete
    private Company company

    // ...
}

Of course - these annotations are just examples - you'll need to figure the ones you need to customize your fields.

You'll then use reflection to read these annotations and generate the SQL queries, HTML templates, TypeScript interfaces and everything else you can auto-generate from them.

The most important thing is to know where to stop. You can't auto-generate everything, and trying to do so will lead to too complicated annotations and too complex code for processing them.

Collapse
 
devfanooos profile image
FaN000s

If you do not have complicated business logic you can try to use a code generator. Take a look at jhipster.tech/