ผลกระทบของการออกแบบโปรแกรม
แนะนำใหม่
7:20
การสร้างคลาส
นี่คือนักเรียน ซึ่งจัดเก็บเป็นตัวแปรอิสระสามตัว: ชื่อ, ปีการศึกษา, และเกรด. นั่นทำงานได้ดี.ตอนนี้เพิ่มนักเรียนอีกพันคน. คุณจำเป็นต้องใช้ตัวแปรสามพันตัว…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| modular/ˈmɒdjʊlə/ | แบบโมดูลาร์ |
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 ภายใต้นโยบาย available-score ค่าเฉลี่ยคืออะไร?
ความยาก 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 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวเลือกการออกแบบส่งผลต่อคนจริง — พิจารณาว่าใครได้รับผลกระทบ
สำหรับการตรวจสอบการคำนวณค่าเฉลี่ยโดยไม่ขึ้นอยู่กับอินพุตหรือผลลัพธ์ที่พิมพ์ออก Which design best isolates that responsibility?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การแยกการคำนวณออกจากอินพุตและเอาต์พุตช่วยให้สามารถตรวจสอบคะแนน Known-score ได้โดยไม่ต้องพึ่งพาการดำเนินการอื่นๆ การมีเมทόδขนาดเล็กเพียงอย่างเดียวไม่รับประกันความถูกต้อง
การออกแบบโปรแกรมเป็นเพียงความสวยงามภายนอกและไม่ส่งผลต่อความปลอดภัยในการแก้ไขโค้ด
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การออกแบบกำหนดว่าโปรแกรมจะถูกแก้ไขได้อย่างปลอดภัยเพียงใด
เมทόδ averageAvailable ที่แสดงจะคืนค่าศูนย์สำหรับอาร์เรย์ที่หายไปทั้งหมด
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ไม่ถูกต้อง: เมทόδนี้จะปฏิเสธกรณีดังกล่าวโดย throws 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.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้