Skip to content
Ameen Abozaid
JNR-003

Code quality

  • 7 Lessons
  • 75 min of reading
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Naming
  • cohesion
  • coupling
  • DRY
  • KISS
  • YAGNI
  • code smells

Learn first

75 min of reading · 3 h of practice (estimate)

Learning outcomes

  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.

Lessons

  1. 01Code is read more than written10 min read
  2. 02Naming11 min read
  3. 03Cohesion and coupling11 min read
  4. 04DRY, KISS, YAGNI — and their limits11 min read
  5. 05A catalog of code smells10 min read
  6. 06Safe refactoring with the IDE11 min read
  7. 07Demo: refactoring a long method under green tests11 min read

Lab and exercises

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.
Show solutions
  • 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.

Quiz

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

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  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

Practical part

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.

Common mistakes

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

Assessment

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

Career relevance

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

Type to search levels, phases, modules and lessons.