本文へスキップ
Skillpedia
一般的なプログラミング40k · #7258

java-refactoring-extract-method

できること

このツールは、Javaコードを整理するのに役立ちます。繰り返しが多かったり長すぎたりするコードの部分を取り出し、より小さく理解しやすい関数にまとめ直します。

使う場面

  • ✓1つのメソッドのコード行数が多すぎるとき。
  • ✓1つのメソッドが複数の役割を担っているとき。
  • ✓コードを読みやすくし、再利用しやすくしたいとき。

スキルのリンク

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

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/github/awesome-copilot/blob/main/skills/java-refactoring-extract-method/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“この長いメソッドを、より小さく分かりやすい部分に分けてください。”

毎週5つの新しいスキル。1分で読めます。

仕組み

AIは、分割できるコードの部分を見つけ出します。それらの部分を、内容が分かる名前の新しい関数として作成し、元のコードを新しい関数の呼び出しに置き換えます。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)を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

よくある質問

なぜコードを整理するのですか?

整理されたコードは読みやすく、エラーを見つけやすく、後から新しい機能を追加しやすくなります。

「リファクタリング」とは何ですか?

プログラムの外からの動作を変えずに、コードの内部構造を改善することです。

「Extract Method」とは何ですか?

コードの一部を取り出して新しいメソッドにする、リファクタリングの代表的な手法です。

詳細

作者による元の説明

Refactoring using Extract Methods in Java Language

repo: github/awesome-copilot

人気

GitHubのスター
40k
ランキング
#7258
ライセンス
MIT
更新日
2026/10/15

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル