java-refactoring-extract-method

oleh github

Refaktorisasi ekstraksi metode Java untuk meningkatkan keterbacaan dan kemudahan perawatan. Mengidentifikasi metode yang melebihi ambang kompleksitas (LOC > 15, pernyataan > 10, kompleksitas siklomatis > 10) dan mengekstrak blok kode logis ke dalam metode pembantu yang terfokus. Menghasilkan kode Java 17 yang dapat dikompilasi sepenuhnya dengan nama metode deskriptif dan komentar dokumentasi satu baris. Mempertahankan semua fungsionalitas asli sambil mengurangi beban kognitif dan meningkatkan kemampuan pengujian melalui metode yang lebih kecil dengan tanggung jawab tunggal.

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

Refactoring Java Methods with Extract Method

Role

You are an expert in refactoring Java methods.

Below are 2 examples (with titles code before and code after refactoring) that represents Extract Method.

Code Before Refactoring 1:

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

Code After Refactoring 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 Before Refactoring 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 After Refactoring 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);
}

Task

Apply Extract Method to improve readability, testability, maintainability, reusability, modularity, cohesion, low coupling, and consistency.

Always return a complete and compilable method (Java 17).

Perform intermediate steps internally:

  • First, analyze each method and identify those exceeding thresholds:
    • LOC (Lines of Code) > 15
    • NOM (Number of Statements) > 10
    • CC (Cyclomatic Complexity) > 10
  • For each qualifying method, identify code blocks that can be extracted into separate methods.
  • Extract at least one new method with a descriptive name.
  • Output only the refactored code inside a single java block.
  • Do not remove any functionality from the original method.
  • Include a one-line comment above each new method describing its purpose.

Code to be Refactored:

Now, assess all methods with high complexity and refactor them using Extract Method

Lebih banyak skill dari github

console-rendering
github
Instruksi untuk menggunakan sistem rendering konsol berbasis tag struct di Go
official
acquire-codebase-knowledge
github
Gunakan keterampilan ini ketika pengguna secara eksplisit meminta untuk memetakan, mendokumentasikan, atau mempelajari basis kode yang sudah ada. Aktifkan untuk perintah seperti "petakan basis kode ini", "dokumentasikan…
official
acreadiness-assess
github
Run the AgentRC readiness assessment on the current repository and produce a static HTML dashboard at reports/index.html. Wraps `npx github:microsoft/agentrc…
official
acreadiness-generate-instructions
github
Menghasilkan file instruksi agen AI yang disesuaikan melalui perintah instruksi AgentRC. Menghasilkan .github/copilot-instructions.md (default, direkomendasikan untuk Copilot di VS…
official
acreadiness-policy
github
Bantu pengguna memilih, menulis, atau menerapkan kebijakan AgentRC. Kebijakan menyesuaikan penilaian kesiapan dengan menonaktifkan pemeriksaan yang tidak relevan, mengganti dampak/tingkat, mengatur…
official
add-educational-comments
github
Tambahkan komentar edukatif ke file kode untuk mengubahnya menjadi sumber belajar yang efektif. Menyesuaikan kedalaman penjelasan dan nada dengan tiga tingkat pengetahuan yang dapat dikonfigurasi: pemula, menengah, dan mahir. Secara otomatis meminta file jika tidak ada yang disediakan, dengan pencocokan daftar bernomor untuk pemilihan cepat. Memperluas file hingga 125% hanya menggunakan komentar edukatif (batas keras: 400 baris baru; 300 untuk file di atas 1.000 baris). Mempertahankan encoding file, gaya indentasi, kebenaran sintaks, dan...
official
adobe-illustrator-scripting
github
Menulis, men-debug, dan mengoptimalkan skrip otomatisasi Adobe Illustrator menggunakan ExtendScript (JavaScript/JSX). Gunakan saat membuat atau memodifikasi skrip yang memanipulasi…
official
agent-governance
github
Kebijakan deklaratif, klasifikasi intensi, dan jejak audit untuk mengontrol akses dan perilaku alat agen AI. Kebijakan tata kelola yang dapat dikomposisikan mendefinisikan alat yang diizinkan/diblokir, filter konten, batas kecepatan, dan persyaratan persetujuan — disimpan sebagai konfigurasi, bukan kode. Klasifikasi intensi semantik mendeteksi perintah berbahaya (eksfiltrasi data, eskalasi hak istimewa, injeksi perintah) sebelum eksekusi alat menggunakan sinyal berbasis pola. Dekorator tata kelola tingkat alat memberlakukan kebijakan pada fungsi...
official