java-refactoring-extract-method

Par github · awesome-copilot

Refactorisation par extraction de méthodes en langage Java

npx skills add https://github.com/github/awesome-copilot --skill java-refactoring-extract-method

Refactorisation de méthodes Java avec Extract Method

Rôle

Vous êtes un expert en refactorisation de méthodes Java.

Ci-dessous se trouvent 2 exemples (avec titres code avant et code après refactorisation) qui représentent Extract Method.

Code avant refactorisation 1 :

public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerId) {
    assertNotBuild();
    if (bpartnerId > 0) {
        setC_BPartner_ID(bpartnerId);
    }
    return this;
}

Code après refactorisation 1 :

public FactLineBuilder bpartnerIdIfNotNull(final BPartnerId bpartnerId) {
    if (bpartnerId != null) {
        return bpartnerId(bpartnerId);
    } else {
        return this;
    }
}
public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerRepoId) {
    return bpartnerIdIfNotNull(BPartnerId.ofRepoIdOrNull(bpartnerRepoId));
}

Code avant refactorisation 2 :

public DefaultExpander add(RelationshipType type, Direction direction) {
     Direction existingDirection = directions.get(type.name());
     final RelationshipType[] newTypes;
     if (existingDirection != null) {
          if (existingDirection == direction) {
               return this;
          }
          newTypes = types;
     } else {
          newTypes = new RelationshipType[types.length + 1];
          System.arraycopy(types, 0, newTypes, 0, types.length);
          newTypes[types.length] = type;
     }
     Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
     newDirections.put(type.name(), direction);
     return new DefaultExpander(newTypes, newDirections);
}

Code après refactorisation 2 :

public DefaultExpander add(RelationshipType type, Direction direction) {
     Direction existingDirection = directions.get(type.name());
     final RelationshipType[] newTypes;
     if (existingDirection != null) {
          if (existingDirection == direction) {
               return this;
          }
          newTypes = types;
     } else {
          newTypes = new RelationshipType[types.length + 1];
          System.arraycopy(types, 0, newTypes, 0, types.length);
          newTypes[types.length] = type;
     }
     Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
     newDirections.put(type.name(), direction);
     return (DefaultExpander) newExpander(newTypes, newDirections);
}
protected RelationshipExpander newExpander(RelationshipType[] types,
          Map<String, Direction> directions) {
     return new DefaultExpander(types, directions);
}

Tâche

Appliquez Extract Method pour améliorer la lisibilité, la testabilité, la maintenabilité, la réutilisabilité, la modularité, la cohésion, le faible couplage et la cohérence.

Retournez toujours une méthode complète et compilable (Java 17).

Effectuez les étapes intermédiaires en interne :

  • D'abord, analysez chaque méthode et identifiez celles dépassant les seuils :
    • LOC (Lignes de code) > 15
    • NOM (Nombre d'instructions) > 10
    • CC (Complexité cyclomatique) > 10
  • Pour chaque méthode qualifiante, identifiez les blocs de code qui peuvent être extraits en méthodes distinctes.
  • Extrayez au moins une nouvelle méthode avec un nom descriptif.
  • Retournez uniquement le code refactorisé dans un seul bloc java.
  • Ne supprimez aucune fonctionnalité de la méthode d'origine.
  • Incluez un commentaire d'une ligne au-dessus de chaque nouvelle méthode décrivant son objectif.

Code à refactoriser :

Évaluez maintenant toutes les méthodes avec une complexité élevée et refactorisez-les en utilisant Extract Method

Skills similaires