Langsung ke konten
Skillpedia
Pemrograman umum40k · #7258

java-refactoring-extract-method

Fungsinya

Tool ini membantu merapikan kode Java Anda. Tool ini mengambil bagian kode yang berulang atau terlalu panjang, lalu mengubahnya menjadi fungsi-fungsi yang lebih kecil dan lebih mudah dipahami.

Kapan dipakai

  • ✓Saat sebuah method memiliki terlalu banyak baris kode.
  • ✓Saat sebuah method melakukan lebih dari satu tugas.
  • ✓Untuk membuat kode lebih mudah dibaca dan digunakan ulang.

Link skill

https://github.com/github/awesome-copilot/blob/main/skills/java-refactoring-extract-method/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/github/awesome-copilot/blob/main/skills/java-refactoring-extract-method/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit →

Contoh permintaan ke AI

“Ubah method yang panjang ini menjadi bagian-bagian yang lebih kecil dan jelas.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

AI mengidentifikasi bagian kode yang dapat dipisahkan. Ia membuat fungsi baru dengan nama yang deskriptif untuk bagian-bagian tersebut, lalu mengganti kode asli dengan pemanggilan ke fungsi-fungsi baru itu.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

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

Pertanyaan umum

Mengapa kode perlu dirapikan?

Kode yang rapi lebih mudah dibaca, lebih mudah menemukan error di dalamnya, dan lebih mudah menambahkan fitur baru di kemudian hari.

Apa itu 'refactoring'?

Refactoring adalah memperbaiki struktur internal kode tanpa mengubah apa yang dilakukannya dari sisi luar.

Apa itu 'Extract Method'?

Ini adalah teknik refactoring tertentu, di mana sebuah blok kode dipindahkan ke method baru.

Detail

Deskripsi asli dari pembuat

Refactoring using Extract Methods in Java Language

repo: github/awesome-copilot

Popularitas

Bintang GitHub
40k
Peringkat
#7258
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa