Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
Refactor method and remove extra local variable(remove binding)

I have this class:

public class User {
    private String name;
    private int age;
    //getters setters
}

I have a method, which updates a user object:

public void foo(User user) { 
    boolean needUpdate = false;
    
    if(needUpdateName(user.getName())) {
        user.setName("new name"); 
        needUpdate = true;
    }
    
    if(needUpdateAge(user.getAge())) {
        user.setAge(42);
        needUpdate = true; 
    }
    
    if(needUpdate) {
        userRepository.update(user);
    } 
}

It's a simple example, only as an example. How can I refactor this code and remove needUpdate variable?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you want to remove the local variable you would loose the state that you had, so you will have to invoke 2 times the methods needUpdateName() and needUpdateAge()

public void foo(User user) { 
    
    if(needUpdateName(user.getName()) || needUpdateAge(user.getAge()) ) {

       if(needUpdateName(user.getName())) {
         user.setName("new name"); 
       }
       
       if(needUpdateAge(user.getAge())) {
         user.setAge(42); 
       }
       userRepository.update(user);
    } 
}

Another more optimal way would be to introduce 2 new methods

  • boolean updateNameIfNeeded(User user, String newName)
  • boolean updateAgeIfNeeded(User user, Integer newAge)

So they would be something like

private boolean updateNameIfNeeded(User user, String newName) {
        if (needUpdateName(user.getName())){
           user.setName(newName);
           return true;
        } else {
           return false;
        }
}

Same for age

 private boolean updateAgeIfNeeded(User user, Integer newAge) {
        if (needUpdateAge(user.getAge())){
           user.setAge(newAge);
           return true;
        } else {
           return false;
        }
}

So your main method then will become

public void foo(User user) { 

  if(updateNameIfNeeded(user, "new Name") || updateAgeIfNeeded(user, 42) ) {
      userRepository.update(user);
  } 
}

This solution is more optimal because needUpdateName and needUpdateAge functions that make the checks are invoked only once!

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda