What this course is, and how to use it
This is the twelfth course in University Computer Science: software engineering 软件工程. It is about building systems that last longer than the people who wrote them: requirements that mean something, designs that hold up, tests that protect behaviour, and teams that work.
A limit, stated once: this is a study course, not a degree. One running course project, BookShare, a small app for lending library books, supplies every example. Its requirements, diagrams, tests and CI configuration are written here, and each one is checked automatically.
- Units 12.1–12.3 shape the product: requirements, architecture and the user interface.
- Units 12.4–12.5 protect it: version control, testing and continuous integration.
- Units 12.6–12.7 finish it: security, ethics and the team project itself.
The habit to build: every document or diagram is a decision made visible. Each unit leaves one behind, and it is checked like code.
Requirements
Why it matters. Most failed systems did not fail because of the code. They failed because people did not agree on what to build. Requirements engineering 需求工程 is the work of writing that agreement down.
The idea.
- A functional requirement 功能需求 says what the system does ("a member can borrow up to 5 books").
- A non-functional requirement 非功能需求 says how well it does it ("search answers within 2 s").
- A use case 用例 follows one goal from start to end, with the people or systems involved (the actors).

Why testable wording matters. "The app should be easy to use" can never fail a test: there is nothing to observe, so there is no verdict. Rewrite it as "a new member completes their first borrow in under 3 minutes without help (measured with 5 first-time users)", and now it can pass or fail. In the course list, every requirement has a clause that can be measured, and each of the three BookShare functional requirements is linked by name to an acceptance test.
Worked example 1: the course list.
| id | requirement | test |
|---|---|---|
| FR-1 | a member may hold at most 5 books | a sixth borrow is refused |
| FR-2 | a borrow records the date and a due date 21 days later | due date = borrow date + 21 |
| FR-3 | an overdue member gets a reminder on each overdue day | number of reminders = number of overdue days |
| NFR-1 | the 95th percentile of search time on 10 000 titles is at most 2 s | measured 95th percentile $\le 2$ s |
Worked example 2: tracing a requirement to code. Each functional requirement becomes an acceptance test. Each acceptance test is first written to fail (red), and each failing test is the reason for the next commit. So the chain requirement → test → code can be checked: a commit that completes FR-2 says so, and anyone can confirm that FR-2's test exists and passes on the borrowing code.
Common mistakes.
- Putting the solution into the requirement ("use Redis"). The requirement is the need, not the method.
- Adjectives that cannot be measured ("fast", "friendly").
- Hidden assumptions. The 21-day loan period should be a named constant that can be changed, not something people just "know".
Summary. Functional = what; non-functional = how well. Testable means observable. The chain from requirement to test to code is the audit trail. Assumptions become named constants.
Architecture and design
Why it matters. Architecture is the set of decisions that are expensive to change later. The job is to make them early, and where everyone can see them.
The idea. BookShare has three layers 分层:
- presentation: the web app;
- domain: loans, members and the catalogue;
- storage: the database.
The dependency rule 依赖规则: dependencies point inwards, towards the domain. The domain knows nothing about any framework, and that is what makes it possible to test it without a browser or a database.

Coupling and cohesion, in numbers. BookShare's domain module has 7 classes with 12 dependencies between them.
- Cohesion 内聚 keeps things that change together in one module (all the loan rules in one place).
- Coupling 耦合 means a change spreads across modules.
Count the files a change touches. Changing the loan-limit rule touches 1 module and 2 files: high cohesion, low coupling. If the same rule were spread through the presentation layer, it would touch 3 layers and 5 files. That count is the design measure.
Worked example 1: the borrow sequence.
- The member clicks "borrow".
- The presentation layer calls
domain.borrow(member, copy). - The domain checks the limit (FR-1) and whether the copy is available.
- Storage records the loan with its dates (FR-2).
- The presentation layer confirms.
Four participants, five messages, and every arrow is labelled with its data. The diagram is a code review that happens before the code exists.
Worked example 2: what a boundary buys. Sending reminders is a
strategy behind an interface (Notifier): email today, push messages
tomorrow. Swapping one for the other costs one configuration line and no
changes to the domain. In the tests, a test double replaces the real
notifier, and FR-3 still passes with the domain untouched.
Common mistakes.
- The domain importing the web framework. This turns the dependency rule upside down, and the domain can no longer be tested on its own.
- Diagrams as decoration. Arrows without labels give a reviewer nothing to check.
- Designing for a scale you only imagine. BookShare has 10 000 titles, not 10 million.
Summary. Layers with dependencies pointing inwards. High cohesion and low coupling: count the files a change touches. Sequence diagrams name their data. Boundaries behind interfaces make parts swappable.
HCI and accessibility
Why it matters. How fast a system is in a person's hands depends on its interface. An interface that some people cannot use is a defect. It is also a moral problem, and more and more often a legal one.
The idea. Design the screen as a set of states 状态 and the transitions 转移 between them. BookShare's borrow flow has five states: browsing, detail, confirm, done and error. Every state the user can reach needs a visible way out.

Accessibility you can measure. The core of the WCAG guidelines:
| principle | example check |
|---|---|
| perceivable | text contrast at least $4.5{:}1$ |
| operable | everything reachable by keyboard, with visible focus |
| understandable | real labels, not placeholder text |
| robust | correct structure, so assistive tools can read it |
On the course prototype, body text has a contrast of $7{:}1$. A grey button that looked disabled, though it could be clicked, had only $2.9{:}1$, which fails (truly disabled controls are exempt); it was replaced with $4.6{:}1$ plus an underline to show it can be clicked. Accessibility is something you measure, or it is just decoration.
Worked example 1: the five-state flow. Error is a full state of its own. A network failure leads there with the member's input kept, and with two ways out (try again, or go back). Walking every transition checks that confirm → error → detail keeps all the form fields, and that no state lacks a way out. The walkthrough is the test.
Worked example 2: the keyboard path. A member completes a borrow using only Tab and Enter. The focus order matches the visual order, the focus is visible on every control, and the confirm button can be reached and is read out by a screen reader. Anything that works only with a mouse (such as menus that open only on hover) fails this test, and the test finds it before users do.
Common mistakes.
- Using placeholder text as the label: it disappears when you type.
- Grey-on-grey "disabled" states that fail the contrast check.
- Error states that throw away what the user typed.
- Testing only the states on the "happy path".
Summary. Design in states and transitions, and give every state a way out. Accessibility has numbers (contrast, focus, keyboard path): measure them, like performance.
Version control and teamwork
Why it matters. Code whose history cannot be traced cannot be trusted. Version control is the memory that lets you answer "why?" months later.

The idea. The course repository's branching model 分支模型:
maincan always be released;- new work happens on short-lived feature branches;
- changes come in through pull requests, with review.
A merge conflict 合并冲突 is the tool reporting that two decisions collided.

How big should a commit be? A commit should be one change that can
be defended on its own. The course history has 12 commits for FR-1 to
FR-3; each is linked to a requirement, and the tests can run at each commit,
though a red commit contains a failing test. git bisect depends on this: if the tests can run at every
commit, a bug can be traced to one change in about $\log_2 n$ checkouts
(12 commits need at most 4).
Worked example 1: reading a conflict. Two branches both change
LOAN_LIMIT. One raises it to 6 for a trial; the other moves it into a
configuration file, at 5. Git reports the collision. Resolving it is a
decision (the trial value wins, and the configuration file notes why),
and the decision is recorded in the merge commit's message. A conflict
resolved without a record is a lost decision.
Worked example 2: the review contract. A pull request shows the changes, the tests and the linked requirement. The reviewer checks the change against FR-2's acceptance test, not against personal taste. Small pull requests (the course rule: under about 200 changed lines) can be reviewed in one pass, and every course pull request was reviewed within a day.
Common mistakes.
- One huge commit for a week of work.
git bisectbecomes useless. - Merge commits with no message. The decision is lost.
- Branches that live for a long time. Conflicts grow as the branches move apart.
- Reviewing style when the contract is about behaviour.
Summary. main can always be released. Each commit is one change,
linked to a requirement. Conflicts are decisions: record them. Review
against acceptance tests, not taste.
Testing and continuous integration
Why it matters. Tests are the only reason anyone dares to change code that works. Continuous integration (CI) is the reason nobody can ignore the tests.
The idea. The test pyramid 测试金字塔:
- many unit tests (fast, each testing one part on its own);
- fewer integration tests (with a real database);
- a few end-to-end tests (the whole app).
CI runs them on every push. If anything fails (red), the merge is blocked.

Coverage, honestly. Line coverage counts the lines that ran. Branch coverage measures the proportion of individual branches taken by tests. BookShare's tests reach 92% line coverage and 81% branch coverage. The branch number is the honest one, because each untested branch is a behaviour nobody has tested. The gate blocks merges below 85% line coverage, and the list of untested branches is the to-do list.
Worked example 1: the three levels for FR-2.
| level | what it checks |
|---|---|
| unit | due date = borrow date + 21, in 3 cases: a normal date, February in a leap year, and the end of a year |
| integration | the loan is saved in the database and can be read back |
| end-to-end | a scripted member borrows a book, and the screen shows the due date |
Three speeds, and three levels of confidence.
Worked example 2: the red-green loop in CI. Push a failing test for "the sixth borrow is refused" (FR-1): CI turns red. Add the check: CI turns green, coverage goes up, and the merge is allowed. The course history shows FR-1 arriving in exactly this shape: one red commit and one green commit, both linked to the requirement.
Common mistakes.
- Testing how the code works instead of what it does. Check the due date, not the function's internals.
- Chasing 100% line coverage with tests that assert nothing.
- Making everything an end-to-end test: slow and unreliable.
- Skipping an unreliable ("flaky") test instead of fixing the race condition behind it.
Summary. The pyramid: many unit tests, fewer integration tests, fewest end-to-end tests. Branch coverage matters more than line coverage. CI decides whether a merge is allowed. A test without an assertion only pretends to cover code.
Security and professional ethics
Why it matters. Systems hold other people's data, and the engineer is the person who knows what can go wrong. Both skill and conscience are part of the job.

The idea. A threat model 威胁模型 lists what must be protected (assets), the ways in (entry points) and who might attack, and then pairs each threat with a defence. For BookShare:
- assets: member records and loan history;
- entry points: the web form, the API and the reminder channel;
- attackers: opportunists, insiders and automated bots.

Least privilege, counted. In BookShare, the librarian role can register copies but cannot read members' addresses; the member role can read only its own loans. The permission table has 3 roles × 6 actions = 18 cells, and only 4 are allowed. So least privilege is something you can count: 14 actions are refused by default.
Worked example 1: the injection that did not happen. Search takes a
free-text field. It uses parameterised queries (the lesson of unit 8)
and limits the length of the input. The threat-model row "search form →
SQL injection → parameterised queries" is closed by a test that sends
' OR '1'='1 and checks that no rows leak.
Worked example 2: an ethical decision. The product owner asks to sell members' reading histories to advertisers. The professional codes of ethics (ACM/IEEE) put avoiding harm to privacy and the public interest above what an employer prefers. The engineer's right move is a written refusal that offers alternatives (for example, anonymous totals, collected with consent). Ethics is a decision with a written record, not just a feeling.
Common mistakes.
- Building the threat model after launch.
- "Protecting" a page by hiding the link to it. The endpoint still answers.
- Treating "it's only an internal tool" as a reason to skip security.
- Treating ethics as box-ticking. The written record is the point.
Summary. Model threats as assets × entry points × attackers. Least privilege can be counted. Parameterise inputs. Ethics means documented decisions with alternatives, linked to a professional code.
Team project
Why it matters. Everything above reaches users through a team. The project is where the habits either work together or fall apart.
The idea. On the course board, user stories 用户故事 ("as a member, I can renew a loan") move from backlog → doing → review → done, and each story carries its acceptance test. The standup 每日站会 (a short daily meeting) keeps everyone in step, and the retro 回顾 (a review at the end of each cycle) improves the process.

Estimates you can check. Stories are sized in points (1 point is about half a day). By week 3 the course team's velocity settles at 8 points per week. With 52 points left in the backlog, the forecast is $52 / 8 = 6.5$ weeks, with an error bar of about $\pm 1$ week. The forecast is updated at every retro, and the board history shows whether the velocity claim was true.
Worked example 1: the definition of done. A story is done when its
acceptance test passes, it has been reviewed, it is merged into main,
and the documentation is updated. The "done" column follows CI's
verdict: "it works on my machine" is not a column.
Worked example 2: a blocked story. The reminder story is blocked, waiting for the notifier's login details. The block is visible on the board, raised at standup, and solved by two people working together. Blocks older than 2 days are escalated; there was one in the course history, and it was solved in a day.
Common mistakes.
- Stories without acceptance tests.
- Estimating in days and then defending the estimate instead of updating it.
- Standups that turn into long status reports.
- Retros that change nothing.
Summary. Stories carry tests. Done means CI passes and the work is reviewed. Velocity makes the forecast more accurate. Blocks are escalated after two days.