Skillpedia
برمجة عامة40k · #7258

java-refactoring-extract-method

ما الذي تفعله

تساعد هذه المهارة على تنظيم كود Java. تأخذ أجزاء الكود المتكررة أو الطويلة جداً وتحوّلها إلى دوال أصغر وأسهل فهماً.

متى تستخدمها

  • ✓عندما تحتوي طريقة على أسطر كثيرة جداً من الكود.
  • ✓عندما تقوم الطريقة بأكثر من مهمة واحدة.
  • ✓لجعل الكود أسهل قراءة وإعادة استخدام.

رابط المهارة

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

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/github/awesome-copilot/blob/main/skills/java-refactoring-extract-method/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“حوّل هذه الطريقة الطويلة إلى أجزاء أصغر وأوضح.”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

يحدد الذكاء الاصطناعي أجزاء الكود التي يمكن فصلها. ثم ينشئ دوالاً جديدة بأسماء وصفية لهذه الأجزاء، ويستبدل الكود الأصلي باستدعاءات لهذه الدوال الجديدة.

المحتوى التقني (ملف 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

الأسئلة الشائعة

لماذا أنظّم الكود؟

الكود المنظم أسهل في القراءة، وفي العثور على الأخطاء، وفي إضافة ميزات جديدة لاحقاً.

ما هي «إعادة الهيكلة» (refactoring)؟

هي تحسين البنية الداخلية للكود دون تغيير ما يفعله من الخارج.

ما هي تقنية «Extract Method»؟

هي تقنية محددة من تقنيات إعادة الهيكلة، يُنقل فيها جزء من الكود إلى طريقة جديدة.

التفاصيل

الوصف الأصلي من المؤلف

Refactoring using Extract Methods in Java Language

repo: github/awesome-copilot

الشعبية

نجوم GitHub
40k
الترتيب
#7258
الترخيص
MIT
آخر تحديث
15‏/10‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة