coding style

50
Overview . . . . . . . . . . . Organizing Straight Line Code . . . . . . . . . . . Using Conditionals . . . . . . Unusual Control Structures Taming Deep Nesting Code Complete Series Aecho Penpower, Inc [email protected] 2013, 06 1 / 50

Upload: hung-wei-liu

Post on 07-Jul-2015

116 views

Category:

Software


0 download

DESCRIPTION

關於Coding Style的部份。Team內部教學用。

TRANSCRIPT

Page 1: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Code Complete Series

Aecho

Penpower, Inc

[email protected]

2013, 06

1 / 50

Page 2: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Overview

Overview

Organizing Straight Line CodeWith Specific orderStatements whose order doesn’t matter

Using ConditionalsIf statementsCase statements

Unusual Control StructuresMultiple returns from a routinegotos

Taming Deep Nesting

2 / 50

Page 3: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

References

Code Complete 2ed

• Ch 14, Organizing Straight Line Code

• Ch 15, Using Conditionals

• Ch 17, Unusual Control Structures

• Ch 19.4, Taming Deep Nesting

3 / 50

Page 4: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Overview

Improve code qualities

• Readability

• Maintainability

4 / 50

Page 5: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Organizing Straight Line Code

Statement order can be one of following categories

• must be a specific order

• order doesn’t matter

.Sample of straight line codes..

......

Foo ( ) ;He l l oWor ld ( ) ;

f o r ( i n t i = 0 ; i < 100 ; i++) {He l l oK i t t y ( ) ;

}

5 / 50

Page 6: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

With specific order

.Run with specific order. The dependencies are obvious...

......

data = ReadData();

result = GetResultsFromData(data);

PrintResults(results);

6 / 50

Page 7: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

With specific order

.Run with specific order. The dependencies are not obvious...

......

revenue.ComputeMonthly();

revenue.ComputeQuarterly();

revenue.ComputeAnnual();

7 / 50

Page 8: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

With specific order

Make the dependences obvious.

• Organize code

• Name routine

• Use routine parameters

• Document dependencies with comments

• Check dependencies with assertions or error-handling

8 / 50

Page 9: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

With specific order

.With specific order, but not obviously...

......

ComputeMarket ingExpense ( )ComputeSalesExpense ( )ComputeTrave lExpense ( )ComputePersonne lExpense ( )DisplayExpenseSummary ( )

• The ComputeMarketingExpense() will initialize theexpense.

• The dependencies are not obvious in above codes.

9 / 50

Page 10: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

With specific order

• The parameter expense gives a hint the following routines aregrouped.

• The names of InitialExpenseData() andDisplayExpenseSummary() are obvious executed at firstand last.

.Grouped with routine parameter..

......

I n i t i a l E x p e n s eD a t a ( expense ) ;ComputeMarket ingExpense ( expense ) ;ComputeSalesExpense ( expense ) ;ComputeTrave lExpense ( expense ) ;ComputePersona lExpense ( expense ) ;DisplayExpenseSummary ( expense ) ;

10 / 50

Page 11: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

With specific order

.Document dependencies with comments..

......

/∗∗∗ Compute expense data . Each o f the r o u t i n e s a c c e s s e s∗ the member data expenseData . DisplayExpenseSummary∗ shou l d be c a l l e d l a s t because i t depends on data∗ c a l c u l a t e d by the o th e r r o u t i n e s .∗/

I n i t i a l E x p e n s eD a t a ( expense ) ;ComputeMarket ingExpense ( expense ) ;ComputeSalesExpense ( expense ) ;ComputeTrave lExpense ( expense ) ;ComputePersona lExpense ( expense ) ;DisplayExpenseSummary ( expense ) ;

11 / 50

Page 12: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Statements whose order doesn’t matter

• In the absence of routine dependencies, we can orderstatement or block of code ...

• Keep related actions together.• Grouping related statements.

• Ordering affects the readability, performance,maintainability.

12 / 50

Page 13: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Keep related actions together.The related actions are not together, but jump around..

......

Market ingData market ingData ;Sa l e sData s a l e sDa t a ;Trave lData t r a v e lDa t a ;

t r a v e lDa t a . ComputeQuarter ly ( ) ;s a l e sDa t a . ComputeQuarter ly ( ) ;market ingData . ComputeQuarter ly ( ) ;

s a l e sDa t a . ComputeAnnual ( ) ;market ingData . ComputeAnnual ( ) ;t r a v e lDa t a . ComputeAnnual ( ) ;

s a l e sDa t a . P r i n t ( ) ;t r a v e lDa t a . P r i n t ( ) ;market ingData . P r i n t ( ) ;

13 / 50

Page 14: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Keep related actions together

.The related actions are together..

......

Market ingData market ingData ;market ingData . ComputeQuarter ly ( ) ;market ingData . ComputeAnnual ( ) ;market ingData . P r i n t ( ) ;

Sa l e sData s a l e sDa t a ;s a l e sDa t a . ComputeQuarter ly ( ) ;s a l e sDa t a . ComputeAnnual ( ) ;s a l e sDa t a . P r i n t ( ) ;

Trave lData t r a v e lDa t a ;t r a v e lDa t a . ComputeQuarter ly ( ) ;t r a v e lDa t a . ComputeAnnual ( ) ;t r a v e lDa t a . P r i n t ( ) ;

14 / 50

Page 15: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Grouping related statements

Bad grouped statements Well grouped statements

15 / 50

Page 16: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Grouping related statements

Bad grouped statements

Well grouped statements

16 / 50

Page 17: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Grouping related statements

Bad grouped statements Well grouped statements

17 / 50

Page 18: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Key points

• The strongest principle for organizing straight-line code isordering dependencies.

• Dependencies should be made obvious through the use ofgood routine names, parameter lists, comments.

• If code doesn’t have order dependencies, keep relatedstatements as close together as possible.

18 / 50

Page 19: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Using Conditionals

Using Conditionals:

• if statements

• case statements

19 / 50

Page 20: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

If statements

Guidelines for If statements

• Write the nominal path through the code first; then write theunusual cases.

• Simplify complicated cases with boolean function calls.

• Put the common cases first.

• Make sure all cases are covered.

20 / 50

Page 21: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

If statements

Write the nominal path through the code first; then write theunusual cases..

......

OpenF i l e ( i n p u t F i l e , s t a t u s ) ;i f ( s t a t u s == S t a t u sE r r o r ) {

e r r o rType = F i l eOpenE r r o r ; // 1 , E r r o r Case} e l s e {

ReadF i l e ( i n p u t F i l e , f i l eD a t a , s t a t u s ) ; // 2 , Normal Casei f ( s t a t u s == S t a t u s Su c c e s s ) {

Summar izeF i l eData ( f i l eDa t a , summaryData , s t a t u s ) ; // 3 , Normal Casei f ( s t a t u s == S t a t u sE r r o r ) {

e r r o rType = ErrorTypeDataSummaryError ; // 4 , E r r o r Case} e l s e {

PrintSummary ( summaryData ) ; // 5 , Normal ca s eSaveSummaryData ( summaryData , s t a t u s ) ;i f ( s t a t u s == S t a t u sE r r o r ) {

e r r o rType = SummarySaveError ; // 6 , E r r o r ca se} e l s e {

UpdateA l lCounts ( ) ; // 7 , Normal ca s eE r a s eUndoF i l e s ( ) ;e r r o rType = ErrorTypeNone ;

}}

} e l s e {e r r o rType = F i l eR e a dE r r o r ; //

}}

21 / 50

Page 22: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

If statements

Write the nominal path through the code first; then write theunusual cases..

......

OpenF i l e ( i n p u t F i l e , s t a t u s ) ;i f ( s t a t u s == Sta t u sSuc c e s s ) {

ReadF i l e ( i n p u t F i l e , f i l eD a t a , s t a t u s ) ; // 1 , Normal Casei f ( s t a t u s == S t a t u s Su c c e s s ) {

Summar izeF i l eData ( f i l eDa t a , summaryData , s t a t u s ) ; // 2 , Normal Casei f ( s t a t u s == S t a t u s Su c c e s s ) {

PrintSummary ( summaryData ) ; // 3 , Normal ca s eSaveSummaryData ( summaryData , s t a t u s ) ;i f ( s t a t u s == Sta tu sSuc c e s s ) {

UpdateA l lCounts ( ) ; // 4 , Normal ca s eE r a s eUndoF i l e s ( ) ;e r r o rType = ErrorTypeNone ;

} e l s e {e r r o rType = SummarySaveError ; // 5 , E r r o r ca se

}} e l s e {

e r r o rType = ErrorTypeDataSummaryError ; // 6 , E r r o r Case}

} e l s e {e r r o rType = F i l eR e a dE r r o r ; // 7 , E r r o r Case

}} e l s e {

e r r o rType = F i l eOpenE r r o r ; //}

22 / 50

Page 23: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

If statements

1. Error Case

2. Normal Case

3. Normal Case

4. Error Case

5. Normal Case

6. Error Case

7. Normal Case

1. Normal Case

2. Normal Case

3. Normal Case

4. Normal Case

5. Error Case

6. Error Case

7. Error Case

Easier to find the normalpath through the codes.

23 / 50

Page 24: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

If statements

Simplify complicated tests with boolean function calls.

......

i f ( i n p u tCha r a c t e r < SPACE ) {cha rac t e rType = Cha r a c t e rType Con t r o lCha r a c t e r ;

}e l s e i f (

i n p u tCha r a c t e r == ’ ’ | |i n p u tCha r a c t e r == ’ , ’ | |i n p u tCha r a c t e r == ’ . ’ | |i n p u tCha r a c t e r == ’ ! ’ | |i n p u tCha r a c t e r == ’ ( ’ | |i n p u tCha r a c t e r == ’ ) ’ | |i n p u tCha r a c t e r == ’ : ’ | |i n p u tCha r a c t e r == ’ ; ’ | |i n p u tCha r a c t e r == ’ ? ’ | |i n p u tCha r a c t e r == ’− ’

){cha rac t e rType = Cha rac t e rType Punc tua t i on ;

} e l s e i f ( ’ 0 ’ <= inpu tCha r a c t e r && i npu tCha r a c t e r <= ’ 9 ’ ) {cha rac t e rType = Cha r a c t e rType D i g i t ;

} e l s e i f (( ’ a ’ <= inpu tCha r a c t e r && i npu tCha r a c t e r <= ’ z ’ ) | |( ’A ’ <= inpu tCha r a c t e r && i npu tCha r a c t e r <= ’Z ’ )

) {cha rac t e rType = Cha r a c t e rType Le t t e r ;

}

24 / 50

Page 25: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

If statements

Simplify complicated tests with boolean function calls.With boolean function calls..

......

i f ( I s C o n t r o l ( i n pu tCha r a c t e r ) ) {cha rac t e rType = Cha r a c t e rType Con t r o lCha r a c t e r ;

}e l s e i f ( I sPun c t u a t i o n ( i n pu tCha r a c t e r ) ) {

cha rac t e rType = Cha rac t e rType Punc tua t i on ;}e l s e i f ( I s D i g i t ( i n pu tCha r a c t e r ) ) {

cha rac t e rType = Cha r a c t e rType D i g i t ;}e l s e i f ( I s L e t t e r ( i n pu tCha r a c t e r ) ) {

cha rac t e rType = Cha r a c t e rType Le t t e r ;}

25 / 50

Page 26: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

If statements

Put the most common cases first..Most common case first..

......

i f ( I s L e t t e r ( i n pu tCha r a c t e r ) ) {// The most common casecha rac t e rType = Cha r a c t e rType Le t t e r ;

}e l s e i f ( I sPun c t u a t i o n ( i n pu tCha r a c t e r ) ) {

cha rac t e rType = Cha rac t e rType Punc tua t i on ;}e l s e i f ( I s D i g i t ( i n pu tCha r a c t e r ) ) {

cha rac t e rType = Cha r a c t e rType D i g i t ;}e l s e i f ( I s C o n t r o l ( i n pu tCha r a c t e r ) ) {

// The l e a s t common casecha rac t e rType = Cha r a c t e rType Con t r o lCha r a c t e r ;

}

26 / 50

Page 27: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

If statements

Use default case to trap errors..Trap the errors with default case...

......

i f ( I s L e t t e r ( i n pu tCha r a c t e r ) ) {cha rac t e rType = Cha r a c t e rType Le t t e r ;

}e l s e i f ( I sPun c t u a t i o n ( i n pu tCha r a c t e r ) ) {

cha rac t e rType = Cha rac t e rType Punc tua t i on ;}e l s e i f ( I s D i g i t ( i n pu tCha r a c t e r ) ) {

cha rac t e rType = Cha r a c t e rType D i g i t ;}e l s e i f ( I s C o n t r o l ( i n pu tCha r a c t e r ) ) {

cha rac t e rType = Cha r a c t e rType Con t r o lCha r a c t e r ;} e l s e {

// Trap the e r r o r s .D i s p l a y I n t e r n a l E r r o r ( ”Unexpected type o f c h a r a c t e r d e t e c t e d . ” ) ;

}

27 / 50

Page 28: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Case Statements

Choosing the Most Effective Ordering of Cases

• Order cases alphabetically or numericallyIf cases are equally important, putting them in A-B-C order toimproves readability.

• Put the normal case first

• Order cases by frequencyPut the most frequently executed cases first and the leastfrequently executed last.

• Human readers can find the most common cases easily.• Putting the common ones at the top of the code makes the

search quicker.

28 / 50

Page 29: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Case statements

Tips of using case statements• Keep the actions of each case simple.Extract complicated codes to routine.

• Use the default clause to detect errors.

.

......

sw i t ch ( commandShortcutLetter ) {ca se ’ a ’ :

P r i n tAnnua lRepo r t ( ) ;b reak ;

ca s e ’ p ’ :// no a c t i o n r e qu i r e d , but ca se was c o n s i d e r e dbreak ;

ca s e ’ q ’ :P r i n tQua r t e r l yR e p o r t ( ) ;b reak ;

ca s e ’ s ’ :Pr intSummaryReport ( ) ;b reak ;

d e f a u l t :// Detect e r r o r s .D i s p l a y I n t e r n a l E r r o r ( ” I n t e r n a l E r r o r 905 : C a l l customer suppo r t . ” ) ;

}

29 / 50

Page 30: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Case statements

Tips of using case statements• Keep the actions of each case simple.Extract complicated codes to routine.

• Use the default clause to detect errors..

......

sw i t ch ( commandShortcutLetter ) {ca se ’ a ’ :

P r i n tAnnua lRepo r t ( ) ;b reak ;

ca s e ’ p ’ :// no a c t i o n r e qu i r e d , but ca se was c o n s i d e r e dbreak ;

ca s e ’ q ’ :P r i n tQua r t e r l yR e p o r t ( ) ;b reak ;

ca s e ’ s ’ :Pr intSummaryReport ( ) ;b reak ;

d e f a u l t :// Detect e r r o r s .D i s p l a y I n t e r n a l E r r o r ( ” I n t e r n a l E r r o r 905 : C a l l customer suppo r t . ” ) ;

}

30 / 50

Page 31: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Case statements

• Generally, code that falls through one case to another is aninvitation to make mistake as the code is modified. It shouldbe avoided.

• Clearly and unmistakably identify the flow-through at theend of case statement.

.

......

sw i t c h ( e r r o rDocumen ta t i onLev e l ) {ca se Documen t a t i onLev e l Fu l l :

D i s p l a y E r r o r D e t a i l s ( er rorNumber ) ;// FALLTHROUGH −− F u l l documentat ion a l s o p r i n t s summary comments

ca se Documentat ionLevel Summary :Disp layErrorSummary ( errorNumber ) ;// FALLTHROUGH −− Summary documentat ion a l s o p r i n t s e r r o r number

ca se Documentat ionLeve l NumberOnly :D i sp layEr ro rNumber ( er rorNumber ) ;b reak ;

d e f a u l t :D i s p l a y I n t e r n a l E r r o r ( ” I n t e r n a l E r r o r 905 : C a l l customer suppo r t . ” ) ;

}

31 / 50

Page 32: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Unusual Control Structure

• Multiple returns from a routine

• goto

32 / 50

Page 33: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Multiple returns from a routine

Multiple returns from a routine.

• Use a return when it enhances readability.

• Use guard clauses1 to simplify the complex error processing.

1Check invalid conditions and return error code directly.33 / 50

Page 34: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Multiple returns from a routine

.Multiple returns from a routine..

......

Comparison Compare ( i n t va lue1 , i n t v a l u e2 ) {i f ( v a l u e1 < va l u e2 ) {

r e t u r n Compar i son LessThan ;}e l s e i f ( v a l u e1 > va l u e2 ) {

r e t u r n Compar i son Greate rThan ;}r e t u r n Compar i son Equa l ;

}

34 / 50

Page 35: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Multiple returns from a routine

This codes can be re written with multiple returns....Deep nesting..

......

i f ( f i l e . va l idName ( ) ) {i f ( f i l e . Open ( ) ) {

i f ( enc r yp t i onKey . v a l i d ( ) ) {i f ( f i l e . Decrypt ( enc r yp t i onKey ) {

// l o t s o f s t a t emen t s// . . .

}}

}}

35 / 50

Page 36: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Multiple returns from a routine

.Use guard clauses to clarify the nominal cases..

......

i f ( ! f i l e . va l idName ( ) ) r e t u r n ;i f ( ! f i l e . open ( ) ) r e t u r n ;i f ( ! enc r yp t i onKey . v a l i d ( ) ) r e t u r n ;i f ( ! f i l e . Decrypt ( enc r yp t i onKey ) ) r e t u r n ;

// l o t s o f s t a t emen t s// . . .

36 / 50

Page 37: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Multiple returns from a routine

.More realistic codes. Use guard clauses to clarify the nominalcases..

......

i f ( ! f i l e . va l idName ( ) ) r e t u r n F i l e E r r o r I n v a l i dN ame ;i f ( ! f i l e . open ( ) ) r e t u r n F i l e E r r o r C a n tOp e nF i l e ;i f ( ! enc r yp t i onKey . v a l i d ( ) ) r e t u r n

F i l e E r r o r I n v a l i d E n c r y p t i o nK e y ;i f ( ! f i l e . Decrypt ( enc r yp t i onKey ) ) r e t u r n

F i l e E r r o r C a n tD e c r y p t F i l e ;

// l o t s o f s t a t emen t s// . . .

37 / 50

Page 38: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

gotos

About goto statements.

• Generally, Without goto → High quality codes

• Under some situations, goto → High readability andmaintainability.

38 / 50

Page 39: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

gotos.Error processing and gotos..

......

e r r o r S t a t e = F i l e S t a t u s S u c c e s s ;f i l e I n d e x = 0 ;

wh i l e ( f i l e I n d e x < numFi lesToPurge ) {f i l e I n d e x += 1 ;i f ( ! F i n d F i l e ( f i l e L i s t ( f i l e I n d e x ) , f i l eToPu r g e ) ) {

e r r o r S t a t e = F i l e S t a t u s F i l e F i n d E r r o r ;goto End Proc ;

}

i f ( ! OpenF i l e ( f i l eToPu r g e ) ) {e r r o r S t a t e = F i l e S t a t u s F i l eO p e nE r r o r ;goto End Proc ;

}

i f ( ! O v e rw r i t e F i l e ( f i l eToPu r g e ) ) {e r r o r S t a t e = F i l e S t a t u s F i l e O v e r w r i t e E r r o r ;goto End Proc ;

}

i f ( ! E ra se ( f i l eToPu r g e ) ){e r r o r S t a t e = F i l e S t a t u s F i l e E r a s e E r r o r ;goto End Proc ;

}

End Proc :D e l e t e P u r g e F i l e L i s t ( f i l e L i s t , numFi lesToPurge ) ; 39 / 50

Page 40: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting

• Few people can understand more than 3 level of nested if.1986, Noam Chomsky, and Gerald Weinberg.

• Many researchers recommend avoiding nesting more than 3 or4 levels.Myers 1976, Marca 1981, and Ledgard and Tauer 1987a.

40 / 50

Page 41: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting

.deep nesting if..

......

i f ( i n p u t S t a t u s == I npu t S t a t u s S u c c e s s ) {// l o t s o f code. . .i f ( p r i n t e r R o u t i n e != NULL ) {

// l o t s o f code. . .i f ( SetupPage ( ) ) {

// l o t s o f code . . .i f ( AllocMem ( &p r i n tDa t a ) ) {

// l o t s o f code. . .

}}

}}

41 / 50

Page 42: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting.Simplify a nested if by retesting part of the condition..

......

i f ( i n p u t S t a t u s != I n p u t S t a t u s S u c c e s s )goto EXIT ;

// l o t s o f code. . .

i f ( p r i n t e r R o u t i n e == NULL )goto EXIT ;

// l o t s o f code. . .

i f ( ! SetupPage ( ) )goto EXIT ;

// l o t s o f code. . .

i f ( ! AllocMem ( &p r i n tDa t a ) )goto EXIT ;

// l o t s o f code. . .

EXIT :r e t u r n ;

42 / 50

Page 43: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting.Simplify a nested if by using a break block..

......

do {i f ( i n p u t S t a t u s != I n pu t S t a t u s S u c c e s s )

b reak ;

// l o t s o f code. . .

i f ( p r i n t e r R o u t i n e == NULL )break ;

// l o t s o f code. . .

i f ( ! SetupPage ( ) )b reak ;

// l o t s o f code. . .

i f ( ! AllocMem ( &p r i n tDa t a ) )b reak ;

// l o t s o f code. . .

} wh i l e ( f a l s e ) ;

43 / 50

Page 44: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting

.Overgrown decision tree..

......

i f ( 10 < qu an t i t y ) {i f ( 100 < qu an t i t y ) {

i f ( 1000 < qu an t i t y ) {d i s c o un t = 0 . 1 0 ;

}e l s e {

d i s c o un t = 0 . 0 5 ;}

}e l s e {

d i s c o un t = 0 . 0 2 5 ;}

}e l s e {

d i s c o un t = 0 . 0 ;}

44 / 50

Page 45: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting

.Convert to a set by if-then-elses..

......

i f ( 1000 < qu an t i t y ) {d i s c o un t = 0 . 1 0 ;

}e l s e i f ( 100 < qu an t i t y ) {

d i s c o un t = 0 . 0 5 ;}e l s e i f ( 10 < qu an t i t y ) {

d i s c o un t = 0 . 0 2 5 ;}e l s e {

d i s c o un t = 0 ;}

45 / 50

Page 46: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting

.Convert to a set by if-then-elses..

......

i f ( 1000 < qu an t i t y ) {d i s c o un t = 0 . 1 0 ;

}e l s e i f ( ( 100 < qu an t i t y ) && ( quan t i t y <= 1000 ) ) {

d i s c o un t = 0 . 0 5 ;}e l s e i f ( ( 10 < qu an t i t y ) && ( quan t i t y <= 100 ) ) {

d i s c o un t = 0 . 0 2 5 ;}e l s e i f ( q u a n t i t y <= 10 ) {

d i s c o un t = 0 ;}

46 / 50

Page 47: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting.

......

// p r o c e s s t r a n s a c t i o n depend ing on type o f t r a n s a c t i o ni f ( t r a n s a c t i o n . Type == Tran sac t i onType Depo s i t ) {

// p r o c e s s a d e p o s i ti f ( t r a n s a c t i o n . AccountType == AccountType Check ing ) {

i f ( t r a n s a c t i o n . AccountSubType == AccountSubType Bus iness )MakeBusinessCheckDep ( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;

e l s e i f ( t r a n s a c t i o n . AccountSubType == AccountSubType Persona l )MakePersonalCheckDep ( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;

e l s e i f ( t r a n s a c t i o n . AccountSubType == AccountSubType School )MakeSchoolCheckDep ( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;

}e l s e i f ( t r a n s a c t i o n . AccountType == AccountType Sav ings )

MakeSavingsDep ( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;e l s e i f ( t r a n s a c t i o n . AccountType == AccountType DebitCard )

MakeDebitCardDep ( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;e l s e i f ( t r a n s a c t i o n . AccountType == AccountType MoneyMarket )

MakeMoneyMarketDep ( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;e l s e i f ( t r a n s a c t i o n . AccountType == AccountType Cd )

MakeCDDep( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;}e l s e i f ( t r a n s a c t i o n . Type == Transac t i onType Withd rawa l ) {

// p r o c e s s a w i thd rawa li f ( t r a n s a c t i o n . AccountType == AccountType Check ing )

MakeCheckingWithdrawal ( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;e l s e i f ( t r a n s a c t i o n . AccountType == AccountType Sav ings )

MakeSav ingsWithdrawal ( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;e l s e i f ( t r a n s a c t i o n . AccountType == AccountType DebitCard )

MakeDebitCardWithdrawal ( t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;}e l s e i f ( t r a n s a c t i o n . Type == Tran s a c t i o nType Tran s f e r ) {

MakeFundsTransfer ( t r a n s a c t i o n . SourceAccountType , t r a n s a c t i o n .TargetAccountType , t r a n s a c t i o n . AccountNum , t r a n s a c t i o n . Amount ) ;

}e l s e {

// p r o c e s s unknown k ind o f t r a n s a c t i o nLogTran s a c t i o nE r r o r ( ”Unknown Tran sa c t i on Type” , t r a n s a c t i o n ) ;

}

47 / 50

Page 48: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting.Factor deeply nested code into its routine..

......

// p r o c e s s t r a n s a c t i o n depend ing on type o f t r a n s a c t i o ni f ( t r a n s a c t i o n . Type == Tran sac t i onType Depo s i t ) {

Proc e s sDepo s i t (t r a n s a c t i o n . AccountType ,t r a n s a c t i o n . AccountSubType ,t r a n s a c t i o n . AccountNum ,t r a n s a c t i o n . Amount

) ;}e l s e i f ( t r a n s a c t i o n . Type == Transac t i onType Withd rawa l ) {

Proce s sWi thd rawa l (t r a n s a c t i o n . AccountType ,t r a n s a c t i o n . AccountNum ,t r a n s a c t i o n . Amount

) ;}e l s e i f ( t r a n s a c t i o n . Type == Tran s a c t i o nType Tran s f e r ) {

MakeFundsTransfer (t r a n s a c t i o n . SourceAccountType ,t r a n s a c t i o n . TargetAccountType ,t r a n s a c t i o n . AccountNum ,t r a n s a c t i o n . Amount ) ;

}e l s e {

// p r o c e s s unknown t r a n s a c t i o n typeLogTran s a c t i o nE r r o r ( ”Unknown Tran sa c t i on Type” , t r a n s a c t i o n ) ;

} 48 / 50

Page 49: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Taming Deep Nesting

Summary of Techniques for Reducing Deep Nesting

• Retest the part of condition.

• Convert to if-then-elses.

• Factor deeply nested code into its own routine.

• Use objects and polymorphic dispatch.Design pattern. Ex, Strategy pattern, State pattern, ... etc.

• ... etc.

49 / 50

Page 50: Coding Style

Overview. . . . . .. . . . .

Organizing Straight Line Code. . . . . . . .. . .

Using Conditionals. . . .. .

Unusual Control Structures Taming Deep Nesting

Ending

Any questions ?

50 / 50