mercredi 15 juillet 2020

springboot 2.3.1 avoid nested if in service file

i have a service file that controller call

so i have controller -> service -> database

my service has this logic:

public ResponseEntity<String> method(@PathVariable("value") Float value) {
    If my valueX is < 1.8 // use in 4 service file
         // log
         // set some values
    If my valueX is < 0.8 // use in 2 service file
         // log
         // set some values
    else if my ValueX > 2 // use 1 time
         stop script
    // rest of the code
}

The problem is that the first 2 if are use somewhere else,

ideal i like

public ResponseEntity<String> method(
   @PathVariable("value") Float value
   @SomethingIfValueXLess1_8,
   @SomethingIfValueXLess0_8,
   @SomethingIfValueXLess2ThenStop,
) {
    // rest of the code
}

but i dont know how

Aucun commentaire:

Enregistrer un commentaire