| English | 释义 | 拼音 |
|---|---|---|
| modular/ˈmɒdjʊlə/ | 模块化 | mó kuài huà |
Two neat designs can report different averages
- A fictional record contains scores 80, absent and 100. Treating absence as zero gives
(80 + 0 + 100) / 3 = 60; omitting the absent result gives(80 + 100) / 2 = 90. - Separate methods can calculate either result correctly. Organization does not choose the reporting policy: establish what absence means, who decides, and which denominator the report must show before using the result.


分数为80、缺失和100。在可用分数政策下,平均值是多少?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
现有分数的总和是180,现有数量是2,因此180/2 = 90。
Turn a requirement into a contract
- Specify whether absence means an unattempted assessed task, a task not assigned, or data not yet entered. These cases need not share a rule; the fictional example demonstrates consequences and does not prescribe a school grading policy.
- State the accepted input, the treatment of missing values and the result. If the rule is “average available scores”, an all-missing record has no average; report that case explicitly instead of inventing zero or dividing by zero.
Separate responsibilities for checking
- A modular 模块化 design separates
readScores,averageAvailableandprintReport. The average method can be checked with known values without reading a file or capturing formatted output. - Separation helps when interfaces remain clear. A change to the meaning of absence can still require changes in calculation, reporting and checks; small methods and good names alone do not prevent those dependencies.
Check ordinary and boundary cases
- For the available-score policy, check [80, absent, 100] gives 90, a single 80 gives 80, and [absent, absent] produces the agreed “no result” outcome. Do not merely check that the program runs.
- The calculation below uses
nullfor missing values. It returns a double average and rejects the all-missing case. It assumes a non-null array of ordinary scores; input validation and reporting are separate responsibilities.
public class AverageAvailable {
public static double averageAvailable(Integer[] scores) {
double total = 0;
int present = 0;
for (Integer score : scores) {
if (score != null) {
total += score;
present++;
}
}
if (present == 0) {
throw new IllegalArgumentException("No available scores");
}
return total / present;
}
}
模块化设计(独立且命名良好的组件)主要使得...更容易:
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
模块化使更改更安全并使组件可复用。
当多个调用者复用同一方法时,修正该方法可使所有调用者受益,而无需编辑重复的代码副本。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
正确:调用者复用了该实现。但其契约及变更的影响仍需审查。
程序数据或逻辑中的偏见可能会在大范围内产生不公平的结果。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
设计选择影响现实世界中的人——请考虑谁会受到影响。
为了独立于输入和打印输出来验证平均值的计算,哪种设计最能隔离这一职责?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
将计算与输入输出分离,可以在不涉及其他操作的情况下进行已知分数的验证。仅有小方法并不能保证正确性。
程序设计纯粹是装饰性的,不影响代码更改的安全性。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
设计决定了程序被修改时的安全性。
所示的averageAvailable方法对于全缺失数组返回零。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
错误:该方法使用IllegalArgumentException拒绝此情况,因为没有可用分数可以定义平均值。
Report consequences to affected users
- A report of 90 should state that it averages two available scores out of three records, with one absent result omitted. Reporting only 90 hides information a reader needs to compare outcomes.
- Review who may be affected by the data and policy. A consistent calculation may still treat different kinds of absence inappropriately; test the agreed requirements and make the outcome understandable to users.
Modularity makes a policy easier to inspect and test; it does not make that policy fair or appropriate. The averages 60 and 90 use different denominators. Choose and disclose the rule rather than silently selecting whichever result looks better.
好设计还是差设计?
设计选择有实际后果。对每个结果进行排序。
Carry a policy change across the design
- If the authorized requirement changes to count an unattempted task as zero, update the denominator, relevant examples, checks and report wording. Reusing a method is useful only when its contract matches the new task.
- Review testability, correctness and user understanding separately. Clear boundaries reduce some change risks, but callers and shared assumptions must still be checked when a method's contract changes.
Program design affects maintainability and people. Separate responsibilities so behavior can be checked, define the missing-data policy, test its boundary cases and disclose the denominator. Correct arithmetic and neat structure do not establish an appropriate policy by themselves.
Match the change to the issue it addresses.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
A modular structure enables checking but cannot choose the right policy by itself.