[HD] EECS4302 F26 - 2026-09-17 (Thursday) - Lecture 3
Watch on YouTube →
Overview
Jackie Wang introduces cohesion, the Single Choice Principle, and regression testing as design checks for EECS4302 projects, then applies them to Java student classes and compiler architecture. The lecture contrasts 20 direct translations among five languages with a modular intermediate-representation approach, and previews how ANTLR 4 can generate scanners and parsers.
Key takeaways
- A class is cohesive when its fields and methods share a purpose; separate resident and nonresident rate fields in one Java Student class are a concrete sign that the class mixes responsibilities.
- The Single Choice Principle asks that a change affect the minimum necessary number of places: repeated kind checks in tuition and registration methods make adding a student category unnecessarily costly.
- Inheritance can improve both cohesion and change localization: shared tuition logic belongs in Student, while rate-specific behavior belongs in ResidentStudent and NonresidentStudent.
- Passing JUnit tests supports correctness only relative to the tests executed; as Edsger W. Dijkstra observed, testing can reveal bugs but cannot establish their absence.
- For five source and target languages, direct pairwise translation needs 20 compilers, whereas a shared-IR architecture can use five front ends and five back ends in Wang's schematic design.
- ANTLR 4 can generate a scanner and parser from token patterns and a context-free grammar, automating two key front-end stages.
Chapters
0:00
Using the Lecture Digest to Review EECS4302
- Jackie Wang directs students to the compass icon on the lecture site for the September 15 digest and says materials are usually posted shortly after class.
- The digest includes main ideas, key terms, examples, common pitfalls, learning outcomes, suggested practice, and links to notes or recording timestamps.
- It supplements rather than reproduces lecture notes; students should return to notes and recordings for full worked examples.
- Wang says the digest is AI-assisted, based on class content, and checked through external validation and proofreading.
7:26
Cohesion: Keep Each Class Focused on One Purpose
- Cohesion concerns whether attributes and methods within one class serve a shared purpose or unifying theme.
- A class has weak cohesion when its members divide into groups serving unrelated responsibilities.
- The Unix principle—each command should do one thing well—illustrates cohesion; commands such as ls, cd, and pwd can be combined through pipelines.
12:11
Single Choice Principle: Minimize the Places a Change Requires
- Wang defines the Single Choice Principle (SCP) as making a change affect the minimum necessary number of places in the software.
- Duplicate logic, whether within one class or across classes and packages, is a common sign that SCP is violated.
- Helper methods can factor out repeated logic; the minimum number of changed places need not be one, but the number must be justified.
16:04
A Java Student Class with Resident and Nonresident Variants
- The initial Java design represents both student types with one Student class and an integer kind field: 1 for resident and 2 for nonresident.
- The class stores courses, a course count, a resident premium rate, and a nonresident discount rate.
- Tuition calculation branches on kind to apply a premium or discount; course registration also branches to enforce different course caps.
23:05
Unused Rate Fields Reveal a Cohesion Problem
- Every resident object carries an irrelevant discount-rate field, while every nonresident object carries an irrelevant premium-rate field.
- Because the two rate attributes serve different student categories, they do not share one purpose within the combined Student class.
- Wang uses the unused fields as a practical clue that the class responsibilities can be separated.
25:35
Repeated Kind Checks Violate the Single Choice Principle
- The tuition and registration methods both repeat checks such as kind == 1 and kind == 2.
- Adding a third student type would require changes in multiple methods, so the implementation makes one conceptual change affect several places.
- Removing a student type would likewise require deleting repeated branches from multiple methods.
28:48
Java Inheritance Separates Student Types and Reuses Tuition Logic
- A parent Student class holds shared course data and base tuition calculation; ResidentStudent and NonresidentStudent subclasses hold only their relevant rate.
- Each subclass overrides tuition calculation and uses super to reuse the parent's base total before applying its premium or discount.
- A new student type can be added in a new subclass, while shared tuition logic remains centralized rather than duplicated.
- The subclass design improves cohesion and supports SCP by localizing type-specific behavior.
34:00
Regression Testing: Rerun Tests After Every Model Change
- Test cases should cover both normal and abnormal inputs, including cases that trigger errors or exceptions.
- Passing tests establishes correctness only with respect to the tests run; the test suite acts as a limited specification.
- Wang cites Edsger W. Dijkstra's point that testing can show the presence of bugs, but cannot prove their absence.
- When model classes change, rerun all existing JUnit tests; use breakpoints and a debugger to investigate failures.
42:50
Compiler Front Ends, Back Ends, and Intermediate Representation
- A compiler front end transforms a source program in concrete syntax into an intermediate representation (IR), while a back end transforms that IR into an output program.
- Source and target programs are concrete syntax, such as text files; the IR is an abstract, often tree-shaped runtime structure.
- The IR can be represented by a hierarchy of classes, and optional optimization stages can operate between the front end and back end.
48:45
Why Five Source and Target Languages Can Require 20 Direct Compilers
- For five languages—Java, C, C#, C++, and Python—direct translation requires 5 × 4 = 20 compilers when same-language translations are excluded.
- Each source language may need a separate compiler for every other target language.
- Wang uses this count to motivate sharing intermediate representations instead of building every pairwise translation independently.
54:29
Designing IRs Around Basic, Pointer, and Object-Oriented Features
- Wang proposes dividing language features into a basic procedural IR for constructs such as if statements, loops, and sequential composition.
- A second, advanced procedural IR captures pointers and pointer arithmetic; a third IR represents object-oriented features such as classes, attributes, and inheritance.
- The language-feature divisions are illustrative design choices: C lacks object-oriented features, while C++ combines procedural, pointer, and object-oriented capabilities.
59:49
Shared IRs Replace Pairwise Translation with Front Ends and Back Ends
- Each of the five source languages can have a front end that maps its constructs into the relevant shared IR or combination of IRs.
- Five back ends can map the shared representations into the five target languages, yielding a schematic total of 5 + 5 rather than 5 × 4 translation modules.
- Wang describes the direct approach as multiplicative and the shared-IR approach as additive, while noting that the architecture is schematic.
- Students suggest chaining compilers as another option; Wang notes that chaining may help but is not always possible.
1:07:01
ANTLR 4 Generates Scanners and Parsers for Compiler Front Ends
- The scanner performs lexical analysis, checking individual tokens—for example, whether a keyword is spelled correctly.
- The parser performs syntactic analysis against a context-free grammar.
- ANTLR 4 can generate both scanner and parser code from specified regular-language patterns and grammar rules.
- The conventional compilation example maps a high-level language such as C to low-level, machine-oriented code.
1:10:32
Assignment 1 Preview: Implementing DFA and NFA Concepts in Java
- Wang plans to release Assignment 1 the following week and give students two weeks to complete it.
- The assignment involves programmatically building something related to DFA and NFA using Java.
- Relevant lecture slides will be identified to support the assignment.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Jackie Wang.