mercredi 13 décembre 2023

How to set fields for an object based the object circumstances, without relying on boolean flags?

We have a huge object which sets one by one a lot of fields. However, we've come to a point where we set 3 flags in order to create it based on what we need, which break clean code rules. Basically it looks something like this:

method(boolean flag1, boolean flag2, boolean flag3){
    if (flag1){
        field1 = customValue1;
    }
    if (flag2){
        field2 = customValue2;
    }
    if (flag3){
        field3 = customValue3;
    }

How can I refactor this so it follows the standard (get rid of all the flags)?

Making a separate method for each case wouldn't be alright I think because I would have a LOT of code duplicates (this method has over 200 lines of code due to the number of fields and the way the business logic works in general). Then I thought of creating some sort of Supplier object for each possible case, but... just no. What are my options?

Aucun commentaire:

Enregistrer un commentaire