高品質軟體的基本動作 101 for nthu

133
高品質軟體的基本動作 - 如何寫 "好的" 程式 Guidelines in How to Create High Quality Code Johnson J. Su 自由軟體鑄造場 蘇展 2014/11/20 Code Complete 101

Upload: su-jan

Post on 14-Jul-2015

162 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: 高品質軟體的基本動作 101 for NTHU

高品質軟體的基本動作- 如何寫 "好的" 程式

Guidelines in How to Create High Quality Code

Johnson J. Su

自由軟體鑄造場 蘇展

2014/11/20

Code Complete 101

Page 2: 高品質軟體的基本動作 101 for NTHU

Johnson J. Su

Linkedin:https://www.linkedin.com/in/jansuE-mail:[email protected]

TEL: (02) 2788-3799 ext. 1478

Page 3: 高品質軟體的基本動作 101 for NTHU

In This Talk

• We will discuss• How to use Code Complete• What is high code quality• How to create high quality code

• We will not discuss• Part V - Code Improvement• Part VI - Software Craftsmanship• Detail of any programing language• Detail of how to deal with code• Any useful tools

Page 4: 高品質軟體的基本動作 101 for NTHU

Agenda

1. What is High Quality Code?

2. Variables (Good and Bad Examples)

3. Statements (Good and Bad Examples)

4. How to create High Quality Code?

Page 5: 高品質軟體的基本動作 101 for NTHU

Before we start…

Page 6: 高品質軟體的基本動作 101 for NTHU

Have you ever thought…?

• What are others’ code doing?

• Why others’ code looks like so dirty?

• How to improve/create high quality and more

readable code?

Page 7: 高品質軟體的基本動作 101 for NTHU

Have you ever thought…?

• 看不懂別人的 Code 在做什麼?

• 為什麼別人寫的 Code 很凌亂?

• 如何寫出 “品質好”, “可讀性高” 的 Code?

• Or more…?

Page 8: 高品質軟體的基本動作 101 for NTHU

Here’s your answer.

Page 9: 高品質軟體的基本動作 101 for NTHU

Guidance Code Complete 2nd

If you are…• Junior Developer

• Chapter 11, 18

• Senior Developer• Chapter 4~9

• Project Manager• Chapter 2, 33

• Standard Maker• Chapter 32

Page 10: 高品質軟體的基本動作 101 for NTHU

Guidance Code Complete 2nd

If you are…• Junior Developer

• Chapter 11, 18

• Senior Developer• Chapter 4~9

• Project Manager• Chapter 2, 33

• Standard Maker• Chapter 32

Page 11: 高品質軟體的基本動作 101 for NTHU

Agenda

1. What is High Quality Code?

2. Variables (Good and Bad Examples)

3. Statements (Good and Bad Examples)

4. How to create High Quality Code?

Page 12: 高品質軟體的基本動作 101 for NTHU

What is High Quality Code?

Page 13: 高品質軟體的基本動作 101 for NTHU

Software Quality?

Page 14: 高品質軟體的基本動作 101 for NTHU

Software Quality

External• Correctness• Usability• Efficiency• Reliability• Integrity• Adaptability• Accuracy• Robustness

Internal• Maintainability• Flexibility

• Portability• Reusability• Readability

• Testability• Understandability

Presenter
Presentation Notes
正確性(Correctness) 可用性(Usability) 效率(Efficiency) 可靠性(Reliability) 完整性(integrity) 適應性(Adaptability) 精確性(Accuracy) 健壯性(Robustness) 可維護性(Maintainability) 靈活性(Flexibility) 可移植性(Portability) 可重用性(Reusability) 可讀性(Readability) 可測試性(Testability) 可理解性(Understandability)
Page 15: 高品質軟體的基本動作 101 for NTHU

This talk, we focus only on

Code Quality

Page 16: 高品質軟體的基本動作 101 for NTHU

Software Quality

External• Correctness• Usability• Efficiency• Reliability• Integrity• Adaptability• Accuracy• Robustness

Internal• Maintainability• Flexibility

• Portability• Reusability• Readability

• Testability• Understandability

Presenter
Presentation Notes
正確性(Correctness) 可用性(Usability) 效率(Efficiency) 可靠性(Reliability) 完整性(integrity) 適應性(Adaptability) 精確性(Accuracy) 健壯性(Robustness) 可維護性(Maintainability) 靈活性(Flexibility) 可移植性(Portability) 可重用性(Reusability) 可讀性(Readability) 可測試性(Testability) 可理解性(Understandability)
Page 17: 高品質軟體的基本動作 101 for NTHU

Code Quality

Understandability Maintainability

Flexibility

PortabilityReusability

Readability

Testability

Page 18: 高品質軟體的基本動作 101 for NTHU

Code Quality

Flexibility

PortabilityReusability

Testability

Self-documenting

Page 19: 高品質軟體的基本動作 101 for NTHU

Self-documenting

Code QualityConstruction

Page 20: 高品質軟體的基本動作 101 for NTHU

Self-documenting

Code QualityConstruction

Page 21: 高品質軟體的基本動作 101 for NTHU

Self-documenting

Code QualityConstruction

Page 22: 高品質軟體的基本動作 101 for NTHU

Agenda

1. What is High Quality Code?

2. Variables (Good and Bad Examples)

3. Statements (Good and Bad Examples)

4. How to create High Quality Code?

Page 23: 高品質軟體的基本動作 101 for NTHU

VariablesGood and Bad Examples

Code Complete 2nd Part III Variables

Page 24: 高品質軟體的基本動作 101 for NTHU

Initializing Variables

Chapter 10

Page 25: 高品質軟體的基本動作 101 for NTHU

Initializing Variables

Chapter 10

Page 26: 高品質軟體的基本動作 101 for NTHU

Keep Variables “Live” for as Short a Time as Possible

Chapter 10

Page 27: 高品質軟體的基本動作 101 for NTHU

Group related statements

Chapter 10

Page 28: 高品質軟體的基本動作 101 for NTHU

Group related statements

Chapter 10

Page 29: 高品質軟體的基本動作 101 for NTHU

Use each variable for one purpose only

Chapter 10

Page 30: 高品質軟體的基本動作 101 for NTHU

Use each variable for one purpose only

Chapter 10

Page 31: 高品質軟體的基本動作 101 for NTHU

Considerations in Choosing Good Names

Chapter 11

Page 32: 高品質軟體的基本動作 101 for NTHU

Chapter 11

Page 33: 高品質軟體的基本動作 101 for NTHU

Naming Loop Indexes

Chapter 11

Page 34: 高品質軟體的基本動作 101 for NTHU

Naming Loop Indexes

Chapter 11

Page 35: 高品質軟體的基本動作 101 for NTHU

Naming Loop Indexes

Chapter 11

Page 36: 高品質軟體的基本動作 101 for NTHU

Naming Status Variables

Chapter 11Chapter 11

Page 37: 高品質軟體的基本動作 101 for NTHU

Naming Status Variables

Chapter 11

Page 38: 高品質軟體的基本動作 101 for NTHU

Naming Temporary Variables

Chapter 11

Page 39: 高品質軟體的基本動作 101 for NTHU

Naming Temporary Variables

Chapter 11

Page 40: 高品質軟體的基本動作 101 for NTHU

Kinds of Names to Avoid

• Avoid misleading names or abbreviations• e.g. FALSE, TRUE

• Avoid names with similar meanings• e.g. Input and inputValue;

recordNum and numRecords;fileNumber and fileIndex

• Avoid variables with different meanings but similar names

• Bad: clientRecs and clientReps• Better: clientRecords and clientReports

• Avoid names that sound similar, such as wrap and rap

Chapter 11

Page 41: 高品質軟體的基本動作 101 for NTHU

Kinds of Names to Avoid

Chapter 11

• Avoid numerals in names• Avoid file1 and file2, or total1 and total2

• Avoid misspelled words in names• Avoid misspelling highlight as hilite• Was it highlite? hilite? hilight? hilit? jai-a-lai-t? Who knows?

• Avoid words that are commonly misspelled in English• e.g. Absense, acummulate, acsend, calender, concieve,

defferred, definate, independance, occassionally

Page 42: 高品質軟體的基本動作 101 for NTHU

Kinds of Names to Avoid

Chapter 11

• Don’t differentiate variable names solely by capitalization• Names are unique• Avoid to use frd for fired,

FRD for final review duty, and Frd for full revenue disbursal.

• Avoid multiple natural languages• Avoid “color” or “colour” and “check” or “cheque”

• Avoid the names of standard types, variables, and routines

Page 43: 高品質軟體的基本動作 101 for NTHU

Kinds of Names to Avoid

Chapter 11

• Don’t use names that are totally unrelated to what the variables represent

• Bad: margaret and pookie. Who know?• Better: boyfriend, wife, and favoriteBeer are superior!

• Avoid names containing hard-to-read characters

Page 44: 高品質軟體的基本動作 101 for NTHU

Avoid “magic numbers”

Chapter 12

Page 45: 高品質軟體的基本動作 101 for NTHU

Avoid “magic numbers”

Chapter 12

Page 46: 高品質軟體的基本動作 101 for NTHU

Boolean Variables

Chapter 12

Page 47: 高品質軟體的基本動作 101 for NTHU

Boolean Variables

Chapter 12

Page 48: 高品質軟體的基本動作 101 for NTHU

Boolean Variables

Chapter 12

Page 49: 高品質軟體的基本動作 101 for NTHU

Boolean Variables

Chapter 12

Page 50: 高品質軟體的基本動作 101 for NTHU

Use enumerated types for readability

Chapter 12

Page 51: 高品質軟體的基本動作 101 for NTHU

Use enumerated types for readability

Chapter 12

Page 52: 高品質軟體的基本動作 101 for NTHU

Avoid literals, even “safe” ones

Chapter 12Chapter 12

Page 53: 高品質軟體的基本動作 101 for NTHU

Avoid literals, even “safe” ones

Chapter 12Chapter 12

Page 54: 高品質軟體的基本動作 101 for NTHU

Avoid literals, even “safe” ones

Chapter 12Chapter 12

Page 55: 高品質軟體的基本動作 101 for NTHU

Avoid literals, even “safe” ones

Chapter 12

Page 56: 高品質軟體的基本動作 101 for NTHU

Use structures to clarify data relationships

Chapter 12

Page 57: 高品質軟體的基本動作 101 for NTHU

Use structures to clarify data relationships

Chapter 12

Page 58: 高品質軟體的基本動作 101 for NTHU

Agenda

1. What is High Quality Code?

2. Variables (Good and Bad Examples)

3. Statements (Good and Bad Examples)

4. How to create High Quality Code?

Page 59: 高品質軟體的基本動作 101 for NTHU

StatementsGood and Bad Examples

Code Complete 2nd Part IV – Statements

Page 60: 高品質軟體的基本動作 101 for NTHU

Making Code Read from Top to Bottom

Chapter 12

Page 61: 高品質軟體的基本動作 101 for NTHU

Making Code Read from Top to Bottom

Chapter 12

Page 62: 高品質軟體的基本動作 101 for NTHU

Grouping Related Statements

Chapter 12

Page 63: 高品質軟體的基本動作 101 for NTHU

Chapter 15

Follow the if clause with a meaningful statement

Chapter 12

Page 64: 高品質軟體的基本動作 101 for NTHU

Chapter 15

Follow the if clause with a meaningful statement

Chapter 12

Page 65: 高品質軟體的基本動作 101 for NTHU

Chapter 15

Follow the if clause with a meaningful statement

Page 66: 高品質軟體的基本動作 101 for NTHU

Don’t make up phony variables to be able to use the case statement

Chapter 15

Page 67: 高品質軟體的基本動作 101 for NTHU

Don’t make up phony variables to be able to use the case statement

Chapter 15

Page 68: 高品質軟體的基本動作 101 for NTHU

In C++ and Java, avoid dropping through the end of a case statement

Chapter 15

Page 69: 高品質軟體的基本動作 101 for NTHU

Abnormal Loop-With-Exit Loops

Chapter 16

Page 70: 高品質軟體的基本動作 101 for NTHU

Don’t monkey with the loop index of a for loop to make the loop terminate

Chapter 16

Page 71: 高品質軟體的基本動作 101 for NTHU

Use meaningful variable names to make nested loops readable

Chapter 16

Page 72: 高品質軟體的基本動作 101 for NTHU

Use meaningful variable names to make nested loops readable

Chapter 16

Page 73: 高品質軟體的基本動作 101 for NTHU

Don’t use recursion for factorials or Fibonacci numbers

Chapter 17

Presenter
Presentation Notes
不要用遞迴(Recursion)去執行 階乘 = Factorials 費氏數列 = Fibonacci
Page 74: 高品質軟體的基本動作 101 for NTHU

Don’t use recursion for factorials or Fibonacci numbers

Chapter 17

Presenter
Presentation Notes
不要用遞迴(Recursion)去執行 階乘 = Factorials 費氏數列 = Fibonacci
Page 75: 高品質軟體的基本動作 101 for NTHU

Using true and false for Boolean Tests

Chapter 19

Page 76: 高品質軟體的基本動作 101 for NTHU

Using true and false for Boolean Tests

Chapter 19

Presenter
Presentation Notes
預設 = True & False
Page 77: 高品質軟體的基本動作 101 for NTHU

Using true and false for Boolean Tests

Chapter 19

Page 78: 高品質軟體的基本動作 101 for NTHU

Agenda

1. What is High Quality Code?

2. Variables (Good and Bad Examples)

3. Statements (Good and Bad Examples)

4. How to create High Quality Code?

Page 79: 高品質軟體的基本動作 101 for NTHU

How to create High Quality Code?

Code Complete 2nd Part II – Creating High-Quality Code

Page 80: 高品質軟體的基本動作 101 for NTHU

Before we start…

Let’s talk about Software Construction first.

Page 81: 高品質軟體的基本動作 101 for NTHU

Construction Activities

Chapter 1

Page 82: 高品質軟體的基本動作 101 for NTHU

The book focus on these activities with highlight

Chapter 1

Page 83: 高品質軟體的基本動作 101 for NTHU

Construction is an…

Iterative process

Chapter 5

Page 84: 高品質軟體的基本動作 101 for NTHU

Design is …

• A Wicked Problem

• A Sloppy Process (Even If it Produces a Tidy Result)

• About Tradeoffs and Priorities

• A Heuristic Process

• And more…

Chapter 5

Page 85: 高品質軟體的基本動作 101 for NTHU

Form Consistent Abstractions

Chapter 5

Page 86: 高品質軟體的基本動作 101 for NTHU

Top-Down and Bottom-Up Design Approaches

Argument for Bottom Up

Argument for Top Down

Chapter 5

Page 87: 高品質軟體的基本動作 101 for NTHU

Top-Down and Bottom-Up Design Approaches

Argument for Bottom UpArgument for Top Down

No Argument, Really

Chapter 5

Page 88: 高品質軟體的基本動作 101 for NTHU

Software’s Primary Technical Imperative: Managing Complexity

Accidental and Essential Difficulties

Importance of Managing Complexity

Chapter 5

Presenter
Presentation Notes
偶然的難題和本質上的難題 本質上的難題,可車子來比喻,當你要設計一台車子,其中車門、引擎、輪子,就是你必須要面對的問題。 偶然上的難題,非必要性的,非這台子一定需要的零件,如車子的引擎要用V8或者渦輪增壓的選擇,選擇其中一種都不影響車子的是否可以正常上路。 管理複雜度的重要性 一個失敗軟體專案,通常會把失敗的原因歸類在,不合理的需求、沒有完整的規劃與管理所導致。 但若是失敗的原因確定是技術因素導致,那很可能就是失控的複雜度有關。開發軟體變得異常複雜。 所以管理控制複雜度是非常重要的議題,因為他也深深影響 code 品質。
Page 89: 高品質軟體的基本動作 101 for NTHU

Hide Secrets (Information Hiding)

A good class interface is

like the tip of an iceberg,

leaving most of the class

unexposed.

Chapter 5

Page 90: 高品質軟體的基本動作 101 for NTHU

Hide Secrets (Information Hiding)

Hiding ComplexityHiding Sources

Page 91: 高品質軟體的基本動作 101 for NTHU

Value of Information Hiding

Chapter 5

• Asking “What does this class need to hide?”

If you can put a Function or Data into the class’s public interface without compromising its secrets, do. Otherwise, don’t.

Page 92: 高品質軟體的基本動作 101 for NTHU

Encapsulate Implementation Details

Chapter 5

Page 93: 高品質軟體的基本動作 101 for NTHU

Levels of Design

1.Software system

2.Subsystem/packages

3.Classes within packages

4.Data and routines within classes

5.Internal routine designChapter 5

Page 94: 高品質軟體的基本動作 101 for NTHU

Reasons to Create a Class

• Model real-world objects• Model abstract objects

• Reduce complexity• Isolate complexity• Hide implementation details

• Limit effects of changes• Hide global data

• Streamline parameter passing• Make central points of control

• Facilitate reusable code• Plan for a family of programs• Package related operations

• Accomplish a specific refactoring

Chapter 6

Presenter
Presentation Notes
為現實世界中的對象建模 為抽象的對象建模 降低複雜度 隔離複雜度 隱藏實現的細節 限制變動影響範圍 隱藏全局變數 讓參數傳遞更順暢 建立中心控制點 讓code易於重用 位程序做計劃
Page 95: 高品質軟體的基本動作 101 for NTHU

• Avoid creating god classes

• Eliminate irrelevant classes

• Avoid classes named after verbs

Classes to Avoid

Chapter 6

Presenter
Presentation Notes
避免像神一樣萬能的類別 消除無關緊要的類別 避免用動詞去命名類別,類別用來存放要動作的Rountine,所以類別用名詞,Routine用Verb。
Page 96: 高品質軟體的基本動作 101 for NTHU

Class Foundations: Abstract Data Types (ADTs)

Picture from: Auto Transport Company ScamsChapter 6

Page 97: 高品質軟體的基本動作 101 for NTHU

If you need to change to a 12-point font size

Chapter 6

Page 98: 高品質軟體的基本動作 101 for NTHU

If you need to change to a 12-point font size

Chapter 6

Page 99: 高品質軟體的基本動作 101 for NTHU

Poor Abstraction

Chapter 6

Page 100: 高品質軟體的基本動作 101 for NTHU

Good Abstraction

Chapter 6

Page 101: 高品質軟體的基本動作 101 for NTHU

Better Abstraction

Chapter 6

Page 102: 高品質軟體的基本動作 101 for NTHU

Mixed Levels of Abstraction

Chapter 6

Page 103: 高品質軟體的基本動作 101 for NTHU

Consistent Levels of Abstraction

Chapter 6

Page 104: 高品質軟體的基本動作 101 for NTHU

Exposing Class’s Implementation Details

Chapter 6

Page 105: 高品質軟體的基本動作 101 for NTHU

Hiding Class’s Implementation Details

Chapter 6

Page 106: 高品質軟體的基本動作 101 for NTHU

What is the routines?

Chapter 6

Page 107: 高品質軟體的基本動作 101 for NTHU

What is the routines?

Chapter 6

Routines = Functions

Page 108: 高品質軟體的基本動作 101 for NTHU

Valid Reasons to Create a Routine

• Reduce complexity• Introduce an intermediate,

understandable abstraction

• Avoid duplicate code• Support subclassing• Hide sequences

• Hide pointer operations• Improve portability

• Simplify complicated Boolean tests

• Improve performance• To ensure all routines are

small?

Chapter 6

Presenter
Presentation Notes
降低複雜度 引入中心、易懂的抽象 避免代碼重複 支持子類別化 隱藏順序 隱藏指針操作 提高可移植性 簡化複雜的 Boolean 判斷 改善效能 確保每個rontine都很小
Page 109: 高品質軟體的基本動作 101 for NTHU

Operations That Seem Too Simple to Put Into Routines

Chapter 7Chapter 7

Page 110: 高品質軟體的基本動作 101 for NTHU

Operations That Seem Too Simple to Put Into Routines

Chapter 7Chapter 7

Page 111: 高品質軟體的基本動作 101 for NTHU

Operations That Seem Too Simple to Put Into Routines

Chapter 7Chapter 7

Page 112: 高品質軟體的基本動作 101 for NTHU

Operations That Seem Too Simple to Put Into Routines

Chapter 7

Page 113: 高品質軟體的基本動作 101 for NTHU

Good Routine Names

• Describe everything the routine does• Bad: ComputeReportTotals()

• Silly Names: ComputeReportTotalsAndOpenOutputFile()

• Avoid meaningless, vague, or wishy-washy verbs• Bad: HandleCalculation(), PerformServices(),OutputUser(), ProcessInput(),

and DealWithOutput()…

• HandleOutput() → FormatAndPrintOutput()

• Make names of routines as long as necessary

Chapter 7

Presenter
Presentation Notes
描述routines所的所有事情 避免使用無意的、模糊的或表達不清的動詞 不要透過數字去命名rountine
Page 114: 高品質軟體的基本動作 101 for NTHU

Good Routine Names

• Don’t differentiate routine names solely by number

• Bad: Part1, Part2,

OutputUser0, OutputUser1, and OutputUser2

• To name a function, use a description of the return value

• e.g. cos(), customerId.Next(), printer.IsReady(), and pen.CurrentColor()

• To name a procedure, use a strong verb followed by an object

• e.g. PrintDocument(), CalcMonthlyRevenues(), CheckOrderlnfo(), and

RepaginateDocument()

Chapter 7

Presenter
Presentation Notes
描述routines所的所有事情 避免使用無意的、模糊的或表達不清的動詞 不要透過數字去命名rountine
Page 115: 高品質軟體的基本動作 101 for NTHU

Good Routine Names

• Establish conventions for common operations

• e.g. employee.id.Get(), dependent.GetId(), supervisor(), candidate.id()

• Use opposites precisely

Chapter 7

Page 116: 高品質軟體的基本動作 101 for NTHU

How Long Can a Routine Be?

100

200Less then 200 lines is better.

Chapter 7

Page 117: 高品質軟體的基本動作 101 for NTHU

Don’t use routine parameters as working variables

Chapter 7

Page 118: 高品質軟體的基本動作 101 for NTHU

Don’t use routine parameters as working variables

Chapter 7

Page 119: 高品質軟體的基本動作 101 for NTHU

Limit the number of a routine’s parameters to about

Seven

7Chapter 7

Page 120: 高品質軟體的基本動作 101 for NTHU

What is Pseudocode?

Chapter 5

Pseudocode is an informal high-level descriptionof the operating principle of a computer programor other algorithm.

Page 121: 高品質軟體的基本動作 101 for NTHU

Pseudocode Principles

Chapter 5

Page 122: 高品質軟體的基本動作 101 for NTHU

Pseudocode Programming Process: Classes

Chapter 5

Page 123: 高品質軟體的基本動作 101 for NTHU

Pseudocode Programming Process: Routines

Chapter 5

Page 124: 高品質軟體的基本動作 101 for NTHU

Constructing Routines by Using the PPP

Chapter 5

Design the routine.

Code the routine.

Check the code.

Clean up loose ends.

Repeat as needed.

1

23

4

5

Page 125: 高品質軟體的基本動作 101 for NTHU

Code the Routine

Chapter 5

1

2

3

4

5

Page 126: 高品質軟體的基本動作 101 for NTHU

Write the routine declaration

Chapter 5

1

Page 127: 高品質軟體的基本動作 101 for NTHU

Writing the First and Last Statements Around Pseudocode

Chapter 5

2

Chapter 5

Page 128: 高品質軟體的基本動作 101 for NTHU

Turn the Pseudocode into High-level comments

Chapter 5Chapter 5

2

Page 129: 高品質軟體的基本動作 101 for NTHU

Chapter 5

The code for each comment has been filled in from here down.3

Page 130: 高品質軟體的基本動作 101 for NTHU

Chapter 15

Example of a Complete Routines Overview

Routine Header

Routine Interface

The code for each comment has been filled in from here down.

The last paragraph of Routine

Page 131: 高品質軟體的基本動作 101 for NTHU

Self-documenting

Code QualityConstruction

Page 132: 高品質軟體的基本動作 101 for NTHU

Thank You!

Presenter
Presentation Notes
Picture from http://hcsin.wikispaces.com/glossary+by+c-o+'14
Page 133: 高品質軟體的基本動作 101 for NTHU

Q & A