انتقل إلى المحتوى
أمين أبوزيد
JNR-003

جودة الكود

  • 7 درس
  • 75 دقيقة قراءة
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • التسمية
  • التماسك (Cohesion)
  • الاقتران (Coupling)
  • DRY
  • KISS
  • YAGNI
  • روائح الكود (Code smells)

تعلّم أولًا

75 دقيقة قراءة · 3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Rename variables, methods, and classes so code reads as intent.
  2. Assess cohesion and coupling in a class and reduce coupling between classes.
  3. Apply DRY, KISS, and YAGNI and recognize when they conflict.
  4. Identify common code smells (long method, large class, feature envy, primitive obsession, duplicated logic) and apply the matching refactoring safely with IDE support.

الدروس

  1. 01الكود يُقرأ أكثر مما يُكتب10 د قراءة
  2. 02التسمية11 د قراءة
  3. 03التماسك والاقتران11 د قراءة
  4. 04DRY وKISS وYAGNI وحدودها11 د قراءة
  5. 05فهرس لروائح الكود (Code smells)10 د قراءة
  6. 06إعادة الهيكلة الآمنة باستخدام IDE11 د قراءة
  7. 07عرض عملي: إعادة هيكلة دالة طويلة مع بقاء الاختبارات خضراء11 د قراءة

المختبر والتمارين

Lab

JNR-003-LAB1 · Clean the checkout

Outcomes: 1–4 · Time: ~2.5 h Refactor a provided 300-line CheckoutService that already has tests. Rename, extract methods and classes, remove duplication, and replace primitives with small types. Acceptance: tests stay green throughout; each commit is one named refactoring (for example "Extract calculateTax"); no behavior change.

Exercises (assessment: smell-spotting review of an unseen class with proposed refactorings, plus six exercises)

IDOutcomeTask
JNR-003-EX011Rename int d(int a, int b), boolean f, and class DataManager so they show intent. Invent a context first.
JNR-003-EX021Rewrite a name that uses get, fetch, and retrieve for the same idea so it uses one word.
JNR-003-EX032A class Order sends emails, saves to a database, and calculates tax. Describe its cohesion and coupling problems.
JNR-003-EX042Reduce coupling: change OrderService from new SmtpMailer() to depending on an interface.
JNR-003-EX053Give an example where removing duplication (DRY) makes code harder to read (KISS). Say what you would do.
JNR-003-EX064Match each smell to its refactoring: long method, feature envy, primitive obsession, duplicated logic, large class.
JNR-003-EX074Do an Extract Method with the IDE on a given block, run the tests, and name the commit.
أظهر الحلول
  • EX01 Example: int totalQuantity(int cartA, int cartB), boolean isPaid, class PriceCalculator.
  • EX02 Choose one verb, for example find, and use it everywhere.
  • EX03 Low cohesion (three unrelated jobs); high coupling to mail, database, and tax code. Split into three classes.
  • EX04 private final Mailer mailer; set in the constructor; Mailer is an interface with SmtpMailer implementing it.
  • EX05 Two methods that look alike are merged with a boolean flag and branches. Leave the duplication until the third case or the real shared idea is clear.
  • EX06 Long method -> Extract Method; feature envy -> Move Method; primitive obsession -> introduce a record; duplicated logic -> Extract Method; large class -> Extract Class.
  • EX07 Select the block, Extract Method, name it by intent, run the tests; commit "Extract calculateShipping".
  • Outcome 4 is also covered by the assessment review and quiz Q07–Q10.

الاختبار

Outcomes: O1 naming · O2 cohesion and coupling · O3 DRY, KISS, YAGNI · O4 smells and refactoring.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Which name shows intent best?O1
  2. 2A boolean method should read like:O1
  3. 3High cohesion means:O2
  4. 4Low coupling means a class:O2
  5. 5A good way to reduce coupling is to:O2
  6. 6YAGNI means:O3
  7. 7DRY can conflict with:O3
  8. 8Using double for money is:O4
  9. 9The fix for a long method is:O4
  10. 10Before refactoring you should:O4

الجزء العملي

Seven exercises JNR-003-EX01 to EX07, the smell-spotting review of an unseen class, and the lab. Pass: at least 8 of 10 quiz answers, six exercises correct, and a lab history where every commit is one named refactoring with tests green.

أخطاء شائعة

Refactoring without tests; abstracting after one occurrence; cosmetic-only changes.

التقييم

Smell-spotting review of an unseen class, with proposed refactorings.

الصلة بالمسار الوظيفي

Code reviews (JNR-053) and technical debt (SRC-017).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.