๐Ÿงฉ Lecture 04 ยท Design โ€” Modularity

Modularity, Cohesion & Coupling

ูƒูŠู ู†ู‚ูŠุณ ุฌูˆุฏุฉ ุงู„ูƒูˆุฏุŸ 3 ู…ู‚ุงูŠูŠุณ: Cyclomatic Complexity (ู…ุฏู‰ ุชุนู‚ูŠุฏ function)ุŒ Cohesion (ุฏุงุฎู„ ุงู„ู€ module), Coupling (ุจูŠู† ุงู„ู€ modules). ุงู„ู€ goal: High Cohesion + Low Coupling.

6
Chapters
7
Cohesion Levels
6
Coupling Levels
4
Exam Qs
01

Design Knowledge Area in SWEBOK

ุงู„ู€ Design = ุชุญูˆูŠู„ ุงู„ู€ requirements ู„ู€ system structure ู…ู†ุธู… ูˆูุนู‘ุงู„ ูŠุญู‚ู‚ ุงู„ู€ FR ูˆุงู„ู€ NFR.

๐Ÿ“š Design Topics ููŠ SWEBOK

๐Ÿ—๏ธ Fundamentals

Modularity, abstraction, separation of concerns.

๐Ÿ”„ Design Process

High-level (architecture) + Low-level (components).

๐Ÿ“ Principles

Patterns, modular design, maintainability, scalability.

๐Ÿ“Š Models

UML, ER diagrams, class & sequence diagrams.

โœ… Evaluation

Reviews, prototyping, metrics.

๐Ÿ›๏ธ Architecture

System structure + NFR (perf, security).

๐ŸŽฏ What do we need to design?
  • Higher-level design (Architecture)
  • Module-level design (Classes, interactions)
  • Algorithms
  • Database
  • User Interface
02

What is Modularity?

ู‚ู„ุนุฉ ู…ู† ุงู„ุฑู…ู„ vs ู‚ู„ุนุฉ LEGO. ุงู„ุฃูˆู„ู‰ ู„ูˆ ุฎุฑุจุช ูƒู„ู‡ุง ู‡ุชุฑูˆุญ. ุงู„ู€ LEGO ุชู‚ุฏุฑ ุชุณุชุจุฏู„ ู‚ุทุนุฉ ูˆุชุนูŠุฏ ุชุดุบู‘ู„ ุงู„ุจุงู‚ูŠ.

๐Ÿ“– ุชุนุฑูŠู Modularity
David Parnas, 1972:"... a mechanism for improving the flexibility and comprehensibility of a system while allowing the shortening of its development time."
  • ุงู„ู€ Software ุงู„ู…ู€ modular: ุงู„ุฃุฌุฒุงุก ู…ู…ูƒู† ุชุชุจุฏู„ ุฃูˆ ุชุชุนุงุฏ ุงุณุชุฎุฏุงู…ู‡ุง ููŠ software ุชุงู†ูŠ.
  • ุงู„ู€ Modularity ุจุชุฎู„ูŠ ุงู„ู€ software ุฃุณู‡ู„ ููŠ ุงู„ูู‡ู… ู„ุฃู† ูƒู„ ุฌุฒุก ุตุบูŠุฑ ูŠุชุฏุฑุณ ู„ูˆุญุฏู‡.
๐Ÿงฉ What is a Module?

ุฃูƒุชุฑ ู…ู† ุชุนุฑูŠูุŒ ูƒู„ู‡ู… ุจูŠูˆุตู„ูˆุง ู„ู†ูุณ ุงู„ููƒุฑุฉ:

  • "A program unit that is discrete and identifiable with respect to compiling, combining, and loading."
  • "A logically separable part of a program."
  • "A set of source code files under version control that can be manipulated as one."
  • "A collection of both data and the routines that act on it." (object-oriented view)
03

Module Complexity

3 ู…ู‚ุงูŠูŠุณ ู„ุฌูˆุฏุฉ ุงู„ู€ module: Cyclomatic Complexity, Cohesion, Coupling.

๐ŸŒช๏ธ Module Complexity

ุงู„ุฏุฑุฌุฉ ุงู„ู„ูŠ ุจูŠู‡ุง design ุฃูˆ code ุตุนุจ ุงู„ูู‡ู… ุจุณุจุจ:

  • ูƒุชุฑ ุงู„ู€ components
  • ูƒุชุฑ ุงู„ู€ relationships ุจูŠู†ู‡ู…

ุฃูˆ ุงู„ุฏุฑุฌุฉ ุงู„ู„ูŠ ุจูŠู‡ุง implementation ุตุนุจ ุงู„ู€ understand and verify.

โœจ Module Simplicity

ุงู„ุนูƒุณ: ุงู„ุฏุฑุฌุฉ ุงู„ู„ูŠ ุจูŠู‡ุง design ุฃูˆ code straightforward ูˆุณู‡ู„ ุงู„ูู‡ู….

ู‡ุฏูู†ุง: ุชุตู…ูŠู… modules ุจุณูŠุทุฉ.

๐Ÿ“ Measures of Module Complexity โ€” 3 ู…ู‚ุงูŠูŠุณ
  1. McCabe Cyclomatic Complexity โ€” ุชุนู‚ูŠุฏ function ูˆุงุญุฏุฉ.
  2. Coupling โ€” ุงุนุชู…ุงุฏ ุงู„ู€ modules ุนู„ู‰ ุจุนุถู‡ุง.
  3. Cohesion โ€” ุชู…ุงุณูƒ ุงู„ู€ module ู…ู† ุฌูˆู‘ู‡.
04

Cyclomatic Complexity โ€” McCabe 1974 โญ

ุนุฏุฏ ุงู„ู…ุณุงุฑุงุช ุงู„ู…ุณุชู‚ู„ุฉ ููŠ piece ู…ู† ุงู„ูƒูˆุฏ. ูƒู„ ู…ุง ุฒุงุฏุŒ ูƒู„ ู…ุง ุงู„ูƒูˆุฏ ุฃุตุนุจ ูˆุฃูƒุชุฑ ุนุฑุถุฉ ู„ู„ู€ bugs.

๐Ÿงฎ 3 ุทุฑู‚ ู„ุญุณุงุจ V(G)

1. Decisions + 1

ุนุฏุฏ ุงู„ู€ binary decisions (if, while, case) + 1.

2. Closed Areas + 1

ุนู„ู‰ ุงู„ู€ Control Flow Graph: ุนุฏุฏ ุงู„ู…ู†ุงุทู‚ ุงู„ู…ุบู„ู‚ุฉ (enclosed regions) + 1.

3. Edges โˆ’ Nodes + 2P

V(G) = E โˆ’ N + 2P
P = ุนุฏุฏ ุงู„ู€ separate sub-graphs (ุนุงุฏุฉ = 1).

โš ๏ธ ู…ู„ุงุญุธุฉ ู…ู‡ู…ุฉ"Simple decision" = ุงุฎุชูŠุงุฑ ุจูŠู† ุฎูŠุงุฑูŠู† ูู‚ุท. ู„ูˆ ููŠู‡ switch ุจู€ 3 casesุŒ ุงุนุชุจุฑู‡ ู…ุซู„ 2 simple decisions (ุชุชูุฑุน ุดุฌุฑุฉ binary).
๐Ÿ”ข ู…ุซุงู„: ุญุณุงุจ V(G) ู…ู† graph

ุนู„ู‰ Control Flow Graph ู…ุนูŠู†:

Edges9
Nodes7
P1
V(G) = 9 โˆ’ 7 + 2(1) = 4
๐Ÿ’ป ู…ุซุงู„: ุญุณุงุจ V(G) ู…ู† ูƒูˆุฏ
C++ ยท categorize()
std::string categorize(int value) {
  if (value > 100) {              // decision 1
    return "Very High";
  } else if (value > 75) {        // decision 2
    return "High";
  } else if (value > 50) {        // decision 3
    return "Medium High";
  } else if (value > 25) {        // decision 4
    return "Medium Low";
  } else if (value > 0) {         // decision 5
    return "Low";
  } else {
    return "Negative";
  }
}
// V(G) = 5 decisions + 1 = 6
๐Ÿ“Š ุงูŠู‡ ูŠุนุชุจุฑ "ูƒูˆูŠุณ"ุŸ
V(G)ุงู„ุชู‚ูŠูŠู…ุงู„ุชูˆุตูŠุฉ
1โ€“10Manageableุณู‡ู„ ุงู„ูู‡ู… ูˆุงู„ุงุฎุชุจุงุฑ. ุงู„ุฃู‡ุฏุงู ุงู„ู…ุซู„ู‰.
11โ€“20Moderateู…ุญุชุงุฌ testing ุฃูƒุชุฑุŒ review ุนู†ุงูŠุฉ. ู…ู…ูƒู† refactoring.
21โ€“50High Complexityุตุนุจ ุงู„ูู‡ู…/ุงู„ุตูŠุงู†ุฉ. ุบุงู„ุจุงู‹ candidate ู„ู„ู€ refactor.
> 50Very Complex๐Ÿšจ Red flag โ€” ู„ุงุฒู… refactoring.
๐Ÿ’ก ุชูˆุตูŠุงุช McCabe
1. ุญุงูุธ ุนู„ู‰ V(G) < 10 ุฎุตูˆุตุงู‹ ู„ู„ูƒูˆุฏ ุงู„ุญุฑุฌ.
2. ุงุณุชุฎุฏู… complexity ูƒู€ guideline ู„ู„ู€ refactoring.
3. ุจุนุถ ุงู„ู€ algorithms (statemachine, business rules) ู„ูŠู‡ุง complexity ุนุงู„ูŠุฉ ุจุทุจูŠุนุชู‡ุง.
๐Ÿ›ฃ๏ธ ุงู„ุนู„ุงู‚ุฉ ุจู€ Basis Path Testing

ุนุฏุฏ test cases ุงู„ู…ุทู„ูˆุจุฉ ู„ู€ full path coverage = V(G).

ู„ูˆ V(G) = 4ุŒ ู…ุญุชุงุฌ 4 test cases ู„ุชุบุทูŠุฉ ูƒู„ ุงู„ู…ุณุงุฑุงุช ุงู„ู…ุณุชู‚ู„ุฉ.

ุนุฏุฏ test cases ู„ู„ู€ statement coverage ู…ู…ูƒู† ูŠูƒูˆู† ุฃู‚ู„ ุจูƒุชูŠุฑ โ€” ุฃุญูŠุงู†ุงู‹ test case ูˆุงุญุฏ ุจูŠู…ุฑ ุจูƒู„ ุงู„ู€ statements (ู„ูˆ ุงู„ูƒูˆุฏ sequential).

05

Cohesion โ€” ุชู…ุงุณูƒ ุงู„ู€ Module โญ

ู…ุฏู‰ ุงุฑุชุจุงุท ุงู„ู€ functions ุฏุงุฎู„ module ูˆุงุญุฏ ุจุจุนุถู‡ุง. High cohesion = ุฌูŠุฏ. 7 ู…ุณุชูˆูŠุงุช ู…ู† ุงู„ุฃุณูˆุฃ (Coincidental) ู„ู„ุฃูุถู„ (Functional).

๐Ÿ“– ุงูŠู‡ ูŠุนู†ูŠ CohesionุŸ

Cohesion = ุฏุฑุฌุฉ ุงุฑุชุจุงุท ุงู„ู€ elements ุฏุงุฎู„ module ูˆุงุญุฏ ุจุจุนุถ. ู„ูˆ ุงู„ู€ functions ูƒู„ู‡ุง ุจุชุนู…ู„ ุญุงุฌุฉ ูˆุงุญุฏุฉ related โ†’ High cohesion.

ุงู„ู‡ุฏู: High cohesion + Low coupling.

๐Ÿ“ˆ Levels of Cohesion (ู…ู† ุงู„ุฃูุถู„ ู„ู„ุฃุณูˆุฃ)
7
๐Ÿ† Functional BEST
ูƒู„ ุงู„ู€ elements ููŠ ุงู„ู€ module ุจุชุณุงู‡ู… ููŠ ูˆุธูŠูุฉ ูˆุงุญุฏุฉ well-defined. ู…ุซู„ุงู‹: StringUtils ููŠู‡ toUpper, toLower, trim โ€” ูƒู„ู‡ุง string operations.
6
Sequential
ุงู„ู€ output ู…ู† function = input ู„ู„ู€ next. ู…ุซุงู„: parseInput() โ†’ validate() โ†’ process().
5
Communicational
ุงู„ู€ functions ุจุชุดุชุบู„ ุนู„ู‰ ู†ูุณ ุงู„ู€ data. ู…ุซุงู„: ูƒู„ ุนู…ู„ูŠุงุช ุงู„ู€ student record (read, update, delete) ููŠ module ูˆุงุญุฏ.
4
Procedural
ุงู„ู€ functions ุจุชู†ูู‘ุฐ ุจุชุฑุชูŠุจ ู…ุนูŠู† ูƒุฌุฒุก ู…ู† procedure. ู…ุซุงู„: open file โ†’ read โ†’ close.
3
Temporal
ุงู„ู€ functions ุจุชู†ูู‘ุฐ ููŠ ู†ูุณ ุงู„ูˆู‚ุช ุจุณ ู…ุด related. ู…ุซุงู„: initializeApp() ุงู„ู„ูŠ ุจูŠุนู…ู„ load ู„ู„ู€ configs, connect ู„ู„ู€ DB, start ู„ู„ู€ logger ูƒู„ู‡ุง ููŠ ุงู„ู€ startup.
2
Logical
ุงู„ู€ functions ุจุชุนู…ู„ ุญุงุฌุงุช similar logically ุจุณ ุจู€ implementations ู…ุฎุชู„ูุฉ. ู…ุซุงู„: handleInput(type) ุงู„ู„ูŠ ุจูŠุนู…ู„ switch ุนู„ู‰ type ูˆุงู„ู€ handlers ู…ุฎุชู„ูุฉ ุฌุฏุงู‹.
1
๐Ÿ˜ˆ Coincidental WORST
ุงู„ู€ elements ู…ุงู„ู‡ุงุด ุนู„ุงู‚ุฉ ุจุจุนุถ. ุงุชุญุทูˆุง ููŠ module ุจุงู„ุตุฏูุฉ. ู…ุซุงู„: ู…ู„ู ููŠู‡ parseDate() ูˆ sendEmail() ูˆ encryptPassword().
๐Ÿ“Œ Winter 2025 Q1.06ู„ูˆ ุนู†ุฏูƒ StringUtils ููŠู‡ุง static functions ูƒู„ู‡ุง ุจุชุนุงู„ุฌ Strings โ€” ุงู„ุฌูˆุงุจ: Functional cohesion (ุงู„ุฃูุถู„).
๐Ÿง  Mnemonic

ู…ู† ุงู„ุฃูุถู„ ู„ู„ุฃุณูˆุฃ:

Functional > Sequential > Communicational >
Procedural > Temporal > Logical > Coincidental

ุฃูˆ: Fancy Sandwich Cooks Prefer Tasty Lemon Cake ๐Ÿฐ

06

Coupling โ€” ุงุนุชู…ุงุฏ ุงู„ู€ Modules ุนู„ู‰ ุจุนุถู‡ุง โญ

ู…ุฏู‰ ุงุนุชู…ุงุฏ module ุนู„ู‰ module ุชุงู†ูŠ. Low coupling = ุฌูŠุฏ. ุงู„ุฏูƒุชูˆุฑ ุจูŠุฐูƒุฑ 5 ุฃู†ูˆุงุน: Common Environment ยท Content ยท Control ยท Data ยท Pathological.

๐Ÿ“– ุงูŠู‡ ูŠุนู†ูŠ CouplingุŸ

Coupling = ุฏุฑุฌุฉ ุงู„ุงุนุชู…ุงุฏ ุจูŠู† modules ู…ุฎุชู„ูุฉ. ู„ูˆ module ุจูŠุนุฑู ุชูุงุตูŠู„ ุฏุงุฎู„ูŠุฉ ููŠ module ุชุงู†ูŠ โ†’ High coupling (ู…ุด ุฌูŠุฏ).

ุงู„ู€ goal: ูƒู„ module ู…ุณุชู‚ู„ ุจู‚ุฏุฑ ุงู„ุฅู…ูƒุงู† โ€” ู„ูˆ ุบูŠุฑุช module ูˆุงุญุฏุŒ ุงู„ุจุงู‚ูŠ ู…ุง ูŠุชุฃุซุฑุด.

๐Ÿ“‰ 5 ุฃู†ูˆุงุน Coupling (ุญุณุจ ุงู„ุณู„ุงูŠุฏุงุช)
ุงู„ู†ูˆุนุงู„ุชุตู†ูŠูุงู„ูˆุตู
๐Ÿ˜ˆ ContentHigh โ€” WorstModule ุจูŠู€ access/modify ู…ุจุงุดุฑุฉู‹ ู„ู„ู€ internals ุจุชุงุน module ุชุงู†ูŠ. ุฃุนู„ู‰ ุฏุฑุฌุฉ coupling. ุจูŠูƒุณุฑ ุงู„ู€ encapsulation. fragile ุฌุฏุงู‹.
PathologicalHighModule ุจูŠุนุชู…ุฏ ุนู„ู‰ ุงู„ู€ internal implementation ุจุชุงุน module ุชุงู†ูŠ / ู…ุชุดุงุจูƒูŠู† ุจุดุฏุฉ. ุณู„ูˆูƒ unpredictable. ู…ุคุดุฑ ุนู„ู‰ ุชุตู…ูŠู… ุณูŠุฆ.
Common EnvironmentMediumุงู„ู€ Modules ุจุชุดุชุฑูƒ ููŠ global data / environment. ุฃูŠ ุชุนุฏูŠู„ ููŠ ุงู„ู€ global ุจูŠุฃุซุฑ ุนู„ู‰ ุงู„ูƒู„. ุจูŠู‚ู„ู‘ู„ ุงู„ู€ encapsulation.
ControlMediumModule ุจูŠุชุญูƒู… ููŠ module ุชุงู†ูŠ ุนู† ุทุฑูŠู‚ flags / control variables. ู…ุฑูˆู†ุฉ ููŠ ุงู„ุชู†ููŠุฐ ุจุณ ุจุชู‚ู„ู‘ู„ ุงู„ู€ clarity.
๐Ÿ† DataLow โ€” Bestุงู„ู€ Modules ุจุชุชูˆุงุตู„ ุนู† ุทุฑูŠู‚ parameters ุจุณ. ู…ููŠุด shared internal state. ุงู„ู†ูˆุน ุงู„ู…ุฑุบูˆุจ.
๐Ÿ“Š ู…ู„ุฎุต ุงู„ุฏูƒุชูˆุฑ (3 tiers) Low (Data) = ุฌูŠุฏุŒ ุจูŠุญุณู‘ู† ุงู„ู€ maintainability/modularity ยท Medium (Common Environment, Control) = tradeoffs ู…ุฎุชู„ุทุฉ ยท High (Content, Pathological) = fragile ูˆุตุนุจ ุงู„ุตูŠุงู†ุฉ.
๐Ÿ’ป ู…ุซุงู„ ุนู„ู‰ Common Environment Coupling
JavaScript โ€” Bad coupling
let counter = 0;             // โš ๏ธ Global shared state

class ModuleA {
  increment() { counter++; }   // writes global
}

class ModuleB {
  display() {
    console.log("Count: " + counter);  // reads global
  }
}
๐Ÿ“Œ Winter 2025 Q1.07ุงู„ูƒูˆุฏ ุฏู‡ Common Environment Coupling โ€” ุงู„ู€ modules ุจุชุดุชุฑูƒ ููŠ counter global. ู„ูˆ ModuleC ุถูŠูุชู‡ ุจุชู…ุณุญ ุงู„ู€ counterุŒ ModuleA ูˆ ModuleB ูƒู„ู‡ู… ู‡ูŠุชุฃุซุฑูˆุง.

โœ… ุงู„ุญู„: Data Coupling

JavaScript โ€” Good coupling
class Counter {
  constructor() { this.value = 0; }
  increment() { this.value++; }
  get() { return this.value; }
}

function display(count) {     // only receives what it needs
  console.log("Count: " + count);
}
โ˜ ๏ธ ู…ุซุงู„ ุนู„ู‰ Pathological Coupling

module ุฎุงุฑุฌูŠ ุจูŠุชุญูƒู… ููŠ ุงู„ู€ internal state ุจุชุงุน module ุชุงู†ูŠุŒ ูˆุงู„ุณู„ูˆูƒ ุจูŠุนุชู…ุฏ ุนู„ู‰ ุงู„ู€ state ุฏู‡ โ†’ unpredictable.

C++ โ€” Pathological
class ModuleA {
  int state = 0;
public:
  void setState(int s) { state = s; }
  void execute() {
    if (state == 1)      { /* action 1 */ }
    else if (state == 2) { /* action 2 */ }
    else               { /* default */ }
  }
};

class ModuleB {
public:
  void changeModuleAState(ModuleA& a, int n) {
    a.setState(n);   // โš ๏ธ B ูŠุชุญูƒู… ููŠ state ุจุชุงุน A ู…ู† ุจุฑู‡
  }
};

// main: B ุจูŠุบูŠู‘ุฑ ุญุงู„ุฉ A ู‚ุจู„ ู…ุง A.execute() โ€” ุณู„ูˆูƒ A ุจูŠุนุชู…ุฏ ุนู„ู‰ B
moduleB.changeModuleAState(moduleA, 1);
moduleA.execute();
๐Ÿ”ด ู„ูŠู‡ ุฏู‡ PathologicalุŸุณู„ูˆูƒ ModuleA.execute() ุจูŠุนุชู…ุฏ ูƒู„ูŠุงู‹ ุนู„ู‰ state ุงุชุญุทู‘ ู…ู† module ุชุงู†ูŠ (ModuleB). ุงู„ุงุชู†ูŠู† ู…ุชุดุงุจูƒูŠู†ุŒ ูˆุฃูŠ ุชุบูŠูŠุฑ ููŠ ุชุณู„ุณู„ ุงู„ู€ calls ุจูŠุบูŠู‘ุฑ ุงู„ู†ุชูŠุฌุฉ ุจุดูƒู„ ุบูŠุฑ ู…ุชูˆู‚ุน.
๐ŸŽฏ The Golden Rule
High Cohesion + Low Coupling

ูƒู„ module:

  • โœ… ุนู†ุฏู‡ ู…ุณุคูˆู„ูŠุฉ ูˆุงุญุฏุฉ ูˆุงุถุญุฉ (High cohesion - Functional).
  • โœ… ุจูŠุนุชู…ุฏ ุนู„ู‰ ุจุงู‚ูŠ ุงู„ู€ modules ุจุฃู‚ู„ ู‚ุฏุฑ (Low coupling - Data only).
  • โœ… ุณู‡ู„ ุชุณุชุจุฏู„ู‡ุŒ ุชุฎุชุจุฑู‡ุŒ ุชูู‡ู…ู‡ ู„ูˆุญุฏู‡.
๐ŸŽฏ

Exam Question Bank โ€” Lecture 4

4 ุฃุณุฆู„ุฉ ุญู‚ูŠู‚ูŠุฉ ู…ู† Winter 2025 ุนู„ู‰ Cyclomatic Complexity, Cohesion, Coupling, White-box Testing.

Question 1 Winter 2025 ยท Q1.05
What is the average Cyclomatic Complexity per function in class StringUtils?
(The class has multiple static String methods, each with at most 1 if/branch.)
A 2
B 3
C 4
D 6
E 12
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: A โ€” 2 ูƒู„ function ููŠู‡ุง branch ูˆุงุญุฏ ุจุณูŠุท (1 decision)ุŒ ูุงู„ู€ V(G) ู„ูƒู„ function = 1 + 1 = 2. ุงู„ู€ average ู‡ูˆ 2.
ุงู„ุฅุฌุงุจุงุช ุงู„ุชุงู†ูŠุฉ ุจุชูุชุฑุถ branches ุฃูƒุชุฑ ู…ู…ุง ุงู„ูƒูˆุฏ ุญู‚ูŠู‚ูŠ ุจูŠุญุชูˆูŠ. ู„ูˆ ุงู„ู€ function ู„ูŠู‡ุง n simple decisionsุŒ V(G) = n+1.
Question 2 Winter 2025 ยท Q1.06
What type of cohesion does StringUtils have? (StringUtils contains multiple static methods all dealing with String operations: toUpper, toLower, etc.)
A Functional
B Sequential
C Temporal
D Communicational
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: A โ€” Functional ูƒู„ ุงู„ู€ functions ููŠ StringUtils ุจุชุณุงู‡ู… ููŠ ูˆุธูŠูุฉ ูˆุงุญุฏุฉ well-defined: String manipulation. ุฏู‡ ุงู„ู€ best level of cohesion.
B (Sequential) โ€” ู„ู…ุง output method ุจูŠูƒูˆู† input ู„ู„ุชุงู†ูŠ. ู‡ู†ุง ุงู„ู€ functions independent. ยท C (Temporal) โ€” ู„ู…ุง ุงู„ู€ functions ุจุชู†ูุฐ ููŠ ู†ูุณ ุงู„ูˆู‚ุช (ู…ุซู„ init). ยท D (Communicational) โ€” ู„ู…ุง ุงู„ู€ functions ุจุชุดุชุบู„ ุนู„ู‰ ู†ูุณ ุงู„ู€ data. ู‡ู†ุง ูƒู„ function ุจุชุงุฎุฏ String ู…ุฎุชู„ู.
Question 3 Winter 2025 ยท Q1.07
What kind of coupling is shown in this JS code?

let counter = 0;
class ModuleA { increment() { counter++; } }
class ModuleB { display() { console.log("Count: " + counter); } }
A Data
B Content
C Common (Environment)
D Control
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: C โ€” Common Environment ุงู„ู€ counter ู‡ูˆ global variable ุจุชุดุงุฑูƒ ููŠู‡ ModuleA ูˆ ModuleB. ุฃูŠ module ูŠุนุฏู‘ู„ ููŠ ุงู„ู€ counter ูŠุชุฃุซุฑ ุงู„ุจุงู‚ูŠ. ุฏู‡ exactly ุชุนุฑูŠู ุงู„ู€ Common Coupling.
A (Data) โ€” ู„ูˆ ุงู„ู€ counter ูƒุงู† parameter ุจูŠุชู…ุฑุฑ ู„ู„ู€ functionุŒ ูƒุงู† Data coupling. ยท B (Content) โ€” ู„ูˆ module ูƒุงู† ุจูŠุฏุฎู„ ุนู„ู‰ internals ู„ู€ object/class ุชุงู†ูŠ ู…ุจุงุดุฑุฉู‹ (ุฃุณูˆุฃ ู†ูˆุน). ยท D (Control) โ€” ู„ู…ุง function ุจุชู…ุฑุฑ flag ู„ู„ุชุงู†ูŠุฉ ุชุชุญูƒู… ููŠ ุงู„ู€ logic.
Question 4 Winter 2025 ยท Q1.03
What is the minimum number of test cases needed to achieve:
(1) FULL statement coverage, AND
(2) FULL path coverage (not branch coverage)
for a sequential code block with one chained if-else-if structure with 4 distinct paths?
A 2 for statement, 4 for path
B 1 for statement, 2 for path
C 2 for statement, 2 for path
D 1 for statement, 4 for path
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: D โ€” 1 for statement coverage, 4 for path coverage
  • Statement coverage: ู„ูˆ ุงู„ูƒูˆุฏ ููŠู‡ ุนุจุงุฑุงุช executed ู…ุฑุฉ ุนู„ู‰ ุงู„ุฃู‚ู„ ุจู€ input ูˆุงุญุฏุŒ test case ูˆุงุญุฏ ูƒูุงูŠุฉ.
  • Path coverage: ู…ุญุชุงุฌ test case ู„ูƒู„ independent path. ุนุฏุฏ ุงู„ู€ paths = V(G). ู„ูˆ V(G) = 4ุŒ ู…ุญุชุงุฌ 4 test cases.
ุงู„ู€ statement coverage ุฏุงูŠู…ุงู‹ โ‰ค path coverage. ุงู„ุฅุฌุงุจุงุช ุงู„ู„ูŠ ุจุชุณูˆูŠ ุจูŠู†ู‡ู… ุฃูˆ ุชู‚ู„ุจ ุงู„ุชุฑุชูŠุจ ุบู„ุท.
๐Ÿ“‹

Cheat Sheet

ูƒู„ ุงู„ู…ุนุงุฏู„ุงุช ูˆุงู„ู…ุณุชูˆูŠุงุช ูˆุงู„ุชุนุฑูŠูุงุช.

๐Ÿงฎ Cyclomatic Complexity

Method 1
simple decisions + 1
Method 2
closed regions in CFG + 1
Method 3
V(G) = E โˆ’ N + 2P
Range 1โ€“10
Manageable
Range 11โ€“20
Moderate, may refactor
Range > 20
High, refactor ๐Ÿ”ด

๐Ÿ“ˆ Cohesion โ€” Best to Worst

7. Functional
๐Ÿ† single well-defined task
6. Sequential
output of A = input of B
5. Communicational
operate on same data
4. Procedural
execute in sequence
3. Temporal
execute at same time
2. Logical
similar logic, diff impl
1. Coincidental
๐Ÿ˜ˆ no relation

๐Ÿ“‰ Coupling โ€” 5 Types (El-Ramly)

Content
๐Ÿ˜ˆ High โ€” access internals directly
Pathological
High โ€” controls another's internal state
Common Env
Medium โ€” shared global data
Control
Medium โ€” flag controls logic
Data
๐Ÿ† Low โ€” pass only what's needed

๐ŸŽฏ Golden Rules

Goal
High cohesion + Low coupling
V(G) target
< 10 for critical code
Path coverage
test cases = V(G)
Module = ?
discrete + identifiable + reusable
Parnas, 1972
First defined modularity
McCabe, 1974
First defined cyclomatic complexity
โšก

Rapid Revision

Flashcards ยท Common Mistakes ยท What the doctor loves to ask.

7 levels of Cohesion (best to worst)ุŸ
tap to flip
Functional ยท Sequential ยท Communicational ยท Procedural ยท Temporal ยท Logical ยท Coincidental
5 types of Coupling (El-Ramly)ุŸ
tap to flip
Common Environment ยท Content ยท Control ยท Data ยท Pathological
V(G) formulaุŸ
tap to flip
V(G) = Edges โˆ’ Nodes + 2P (or: decisions + 1)
Goal of modular designุŸ
tap to flip
High Cohesion + Low Coupling
Best cohesion typeุŸ
tap to flip
Functional ๐Ÿ†
Best coupling typeุŸ
tap to flip
Data ๐Ÿ† (only needed primitives)
Code uses global variable shared between modulesุŸ
tap to flip
Common (Environment) Coupling
Test cases needed for path coverageุŸ
tap to flip
Equal to V(G)
Recommended max V(G)ุŸ
tap to flip
< 10 for critical code

๐Ÿšจ Common Mistakes

1. ุฎู„ุท ุชุฑุชูŠุจ CohesionFunctional ู‡ูŠ ุงู„ู€ best โ€” ู…ุด Coincidental. ุงู„ู€ ladder ุตุนูˆุฏ ู…ู† coincidental ู„ู€ functional.
2. ุฎู„ุท ุชุฑุชูŠุจ CouplingData ู‡ูˆ ุงู„ู€ best โ€” ู„ุฅู† ุงู„ู…ุนู„ูˆู…ุงุช ุงู„ู„ูŠ ุจุชู…ุฑุฑ ุจุณ ุงู„ู„ูŠ ู…ุญุชุงุฌู‡ุง. Content ู‡ูˆ ุงู„ุฃุณูˆุฃ (access internals).
3. ู†ุณูŠุงู† Pathological couplingุงู„ุฏูƒุชูˆุฑ ุจูŠุฐูƒุฑ 5 ุฃู†ูˆุงุน: Common Environment ยท Content ยท Control ยท Data ยท Pathological. (ู…ุด External/Stamp โ€” ุฏูˆู„ ู…ู† taxonomies ุชุงู†ูŠุฉ ู…ุด ููŠ ุณู„ุงูŠุฏุงุชู‡).
4. V(G) = decisionsุŒ ู…ุด decisions + 1ุงู„ุตูŠุบุฉ ู‡ูŠ decisions + 1. ู…ุด ุจุณ ุนุฏุฏ ุงู„ู€ ifs.
5. ุชุฎูŠู„ ุฅู† switch ุจู€ 3 cases = 1 decisionู„ุฃ โ€” ูƒู„ case = decision ู„ูˆุญุฏู‡ ุชู‚ุฑูŠุจุงู‹ (binary tree of decisions). switch ุจู€ N cases โ‰ˆ N-1 simple decisions.

โญ What Dr. El-Ramly Loves to Ask

๐Ÿ”ฅ ุงู„ุฃุณุฆู„ุฉ ุงู„ู…ุชูƒุฑุฑุฉ ุนู„ู‰ Lec 4 (Winter 2025 ุฌุฏูŠุฏ)
  1. Cyclomatic Complexity โ€” ุจูŠุฏูŠ class ูˆุนู„ูŠูƒ ุชุญุณุจ ุงู„ู€ average per function.
  2. Cohesion type โ€” ุจูŠุฏูŠ class ูˆุนู„ูŠูƒ ุชุญุฏุฏ type cohesion.
  3. Coupling type โ€” ุจูŠุฏูŠ code snippet (global var, parameter, etc.) ูˆุนู„ูŠูƒ ุชุญุฏุฏ type coupling.
  4. Test cases for coverage โ€” ู…ู‚ุงุฑู†ุฉ ุจูŠู† statement vs path vs branch coverage.
๐ŸŽฏ ู†ุตูŠุญุฉุงุญูุธ ุงู„ู€ 7 cohesion ูˆุงู„ู€ 6 coupling ุจุชุฑุชูŠุจ. ูƒุชูŠุฑ ุงู„ุฃุณุฆู„ุฉ ุจุชุณุฃู„ ุชุฎุชุงุฑ ู…ู† ู‚ุงุฆู…ุฉุŒ ูู„ูˆ ุญุงูุธ ุงู„ุชุฑุชูŠุจ ู‡ุชุนุฑู Functional > Sequential > ... ููˆุฑุงู‹.