mardi 12 avril 2022

How to elegantly check null check for java method parameter?

How to elegantly check null check for java method parameter?

I have code like this:

    public void updateAccount(String a, int b, double c, float d, List e, Set f, Map g, Collection h, Enum i ...String) {
        if(a != null && !a.isBlank()) {
            this.a = a;
        }
        if(b != null && !b.isBlank()) {
            this.b = b;
        }
        if(c != null && !c.isBlank()) {
            this.c = c;
        }
        if(d != null && !d.isBlank()) {
            this.d = d;
        }
        if(e != null && !e.isBlank()) {
            this.e = e;
        }
        if(f != null && !f.isBlank()) {
            this.f = f;
        }
        if(g != null && !g.isBlank()) {
            this.g = g;
        }
        if(h != null && !h.isBlank()) {
            this.h = h;
        }if(i != null && !i.isBlank()) {
            this.i = i;
        }
        ....
    }

All parameters are checked for null, and if not null, the value of the corresponding field is changed.

I feel that the above method is too hard-coded.

I'm wondering how I can turn this into more efficient code.

Best Regards!

Aucun commentaire:

Enregistrer un commentaire