جودة الكود
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- التسمية
- التماسك (Cohesion)
- الاقتران (Coupling)
- DRY
- KISS
- YAGNI
- روائح الكود (Code smells)
تعلّم أولًا
75 دقيقة قراءة · 3 ساعة تدريب (تقدير)
مخرجات التعلم
- Rename variables, methods, and classes so code reads as intent.
- Assess cohesion and coupling in a class and reduce coupling between classes.
- Apply DRY, KISS, and YAGNI and recognize when they conflict.
- Identify common code smells (long method, large class, feature envy, primitive obsession, duplicated logic) and apply the matching refactoring safely with IDE support.
الدروس
المختبر والتمارين
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)
| ID | Outcome | Task |
|---|---|---|
| JNR-003-EX01 | 1 | Rename int d(int a, int b), boolean f, and class DataManager so they show intent. Invent a context first. |
| JNR-003-EX02 | 1 | Rewrite a name that uses get, fetch, and retrieve for the same idea so it uses one word. |
| JNR-003-EX03 | 2 | A class Order sends emails, saves to a database, and calculates tax. Describe its cohesion and coupling problems. |
| JNR-003-EX04 | 2 | Reduce coupling: change OrderService from new SmtpMailer() to depending on an interface. |
| JNR-003-EX05 | 3 | Give an example where removing duplication (DRY) makes code harder to read (KISS). Say what you would do. |
| JNR-003-EX06 | 4 | Match each smell to its refactoring: long method, feature envy, primitive obsession, duplicated logic, large class. |
| JNR-003-EX07 | 4 | Do 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;Maileris an interface withSmtpMailerimplementing 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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).