Table of Contents

Open all
Close all
Preface
17
Who This Book Is For
17
How This Book Is Organized
17
Acknowledgments
19
Conclusion
21
1 Introduction
23
1.1 What Is Clean ABAP?
23
1.1.1 What Is Readability?
23
1.1.2 What’s the Story behind Clean ABAP?
25
1.2 How to Get Started with Clean ABAP
26
1.3 How to Handle Legacy Code
26
1.4 How to Check Code Automatically
28
1.5 How Does Clean ABAP Relate to Other Guides?
29
1.6 How to Engage with the Clean ABAP Community
30
1.7 Summary
31
2 The ABAP Language
33
2.1 Mind Legacy Code
33
2.2 Mind Performance
35
2.2.1 Mostly Harmless
35
2.2.2 Start Clean, Degrade Only Where Needed
36
2.2.3 Measure, Don’t Assume
37
2.3 Prefer Object Orientation to Procedural Programming
37
2.3.1 ABAP’s Programming Paradigm
37
2.3.2 The Difference between Function Groups and Classes
38
2.3.3 Differences in ABAP’s Object Orientation
41
2.3.4 If You Have No Choice
43
2.4 Favor Functional Language Constructs over Procedural Language Constructs
43
2.5 Avoid Obsolete Language Elements
46
2.6 Use Design Patterns Wisely
48
2.6.1 Too Many Singletons
48
2.6.2 Too Many Patterns Mixed
49
2.7 Summary
50
3 Classes and Interfaces
51
3.1 Object Orientation
51
3.1.1 Interfaces
52
3.1.2 Classes and Objects
57
3.1.3 State
75
3.2 Scope and Visibility
76
3.2.1 Global and Local Scope
76
3.2.2 Visibility
81
3.3 Constructors
83
3.3.1 Scope and Visibility
83
3.3.2 Dependency Injection
85
3.3.3 Static Creation Methods
86
3.3.4 Creational Patterns
89
3.3.5 Instantiation
91
3.4 Summary
93
4 Methods
95
4.1 Object-Oriented Programming
95
4.1.1 Static and Instance Methods
95
4.1.2 Public Instance Methods
98
4.1.3 Method Redefinition
100
4.2 Parameters
102
4.2.1 How Many Input Parameters Are Too Many?
103
4.2.2 Optional Input Parameters
103
4.2.3 Preferred Input Parameters
105
4.2.4 Boolean Input Parameters
106
4.2.5 EXPORTING Parameters
108
4.2.6 RETURNING Parameters
109
4.2.7 CHANGING Parameters
111
4.2.8 Pass-by-Value and Pass-by-Reference
112
4.3 Method Body
115
4.3.1 Do One Thing
115
4.3.2 Descend One Level of Abstraction
117
4.3.3 Keep Methods Small
120
4.3.4 Fail Fast
122
4.3.5 CHECK versus RETURN
124
4.4 Calling Methods
125
4.4.1 Passing Input Parameters
125
4.4.2 Capturing Output Parameters
126
4.4.3 The CALL METHOD Construct
127
4.4.4 Optional Parameter Name
128
4.4.5 Self-Reference
129
4.5 Summary
130
5 Names
133
5.1 Good Naming
133
5.1.1 Descriptive Names
133
5.1.2 Domain Terms
134
5.1.3 Plural or Singular?
135
5.1.4 Abbreviations
136
5.1.5 Naming Classes and Methods
136
5.1.6 Noise Words
137
5.1.7 Term Consistency
137
5.1.8 Design Patterns in Code
138
5.1.9 Hungarian Notation and Prefixes
138
5.2 ABAP Peculiarities
138
5.2.1 Group of Objects with Possible Name Collisions
139
5.2.2 snake_case versus camelCase
139
5.3 Affixes: Prefixes, Suffixes, and Infixes
140
5.3.1 The Musts
140
5.3.2 Helpful Affixes
141
5.3.3 Affixes That Don’t Bring Value
142
5.4 Dealing with Legacy Code
142
5.5 Summary
143
6 Variables and Literals
145
6.1 Variables
145
6.1.1 Declaring Variables
146
6.1.2 Branches and Scope
148
6.1.3 Declaration Chaining
149
6.1.4 Looping Variables
151
6.2 Constants
152
6.2.1 Proper Use of Constants
152
6.2.2 Enumeration Classes
154
6.2.3 Constant Grouping
159
6.3 Strings
160
6.3.1 String Literals
160
6.3.2 String Building
161
6.4 Booleans
161
6.4.1 When to Use Booleans
162
6.4.2 XSDBOOL for Inline Decisions
163
6.5 Regular Expressions
164
6.5.1 Simple Regular Expressions
164
6.5.2 Basic Checks
165
6.5.3 Complex Regular Expressions
166
6.6 REDUCE
166
6.7 Summary
168
7 Internal Tables
171
7.1 Using the Right Table Category
171
7.1.1 Standard Tables
172
7.1.2 Sorted Tables
172
7.1.3 Hashed Tables
173
7.2 Avoiding DEFAULT KEY
173
7.3 INSERT INTO TABLE and APPEND TO
174
7.3.1 APPEND Statement
174
7.3.2 INSERT Statement
175
7.4 Verifying the Existence of a Row
176
7.4.1 READ TABLE
176
7.4.2 LOOP AT
176
7.4.3 LINE_EXISTS
177
7.5 Retrieving Table Contents
177
7.5.1 LOOP AT
178
7.5.2 READ TABLE
178
7.6 LOOP AT WHERE and Nested IF
179
7.6.1 Nested IF
179
7.6.2 LOOP AT … WHERE
180
7.7 Identifying Unnecessary Table Reads
180
7.8 Block Processing of Table Rows and Single Row Operations
181
7.9 DESCRIBE TABLE and Table Function LINES
182
7.9.1 DESCRIBE TABLE
182
7.9.2 LINES
183
7.10 Summary
183
8 Control Flow
185
8.1 IFs
185
8.1.1 IF Branches
186
8.1.2 Keep It Understandable
188
8.2 Nesting Depth
190
8.3 Conditions
190
8.3.1 Try to Make Conditions Positive
190
8.3.2 IS NOT or NOT IS
193
8.3.3 Complex Conditions
195
8.4 CASE
196
8.4.1 CASE or IF
197
8.4.2 CASE or SWITCH
198
8.4.3 Multiple Uses of the Same CASE
199
8.5 Do 1 Times
200
8.5.1 Pseudo Loops for Control Flow
200
8.5.2 Refactoring
201
8.6 Summary
203
9 Comments
205
9.1 Express Yourself in Code
205
9.2 Comment Placement and Usage
207
9.3 Comments to Avoid
208
9.4 FIXME, TODO, and XXX Comments
211
9.5 Special Comments: ABAP Doc, Pragmas, and Pseudo Comments
212
9.6 Summary
214
10 Formatting
215
10.1 Consistency in Coding Style
216
10.2 Optimizing for Reading
216
10.3 The Pretty Printer
217
10.4 Number of Statements Per Line
218
10.5 Line Length
219
10.6 Condensing the Code
220
10.7 Blank Lines
220
10.8 Alignment of Assignment Statements
221
10.9 Alignment of Variable Declarations
222
10.10 Placement of Closing Brackets
223
10.11 Formatting Method Parameters
223
10.11.1 Single Parameter Calls
224
10.11.2 Line Break Multiple Parameters
224
10.11.3 Placement of Parameters
224
10.11.4 Indenting Parameters
225
10.11.5 Vertical Alignment of Parameter Values
226
10.11.6 Breaking Calls to a New Line
226
10.11.7 Indents and Using the Tab Key
227
10.11.8 Indentation of Inline Declarations
227
10.12 Summary
227
11 Error Handling
229
11.1 Messages
229
11.2 Return Codes
232
11.2.1 Exceptions versus Return Codes
233
11.2.2 Handling Failures
234
11.3 Exceptions
236
11.3.1 Exceptions Are for Errors
236
11.3.2 Class-Based Exceptions
237
11.3.3 CX_STATIC_CHECK
242
11.3.4 CX_NO_CHECK
243
11.3.5 CX_DYNAMIC_CHECK
244
11.4 Raising and Catching
245
11.4.1 Exception Superclass
245
11.4.2 Raising Exceptions
247
11.4.3 Catching Exceptions
248
11.4.4 When to Dump
251
11.5 Summary
252
12 Unit Testing
253
12.1 Test Classes
254
12.1.1 Test Class Properties
254
12.1.2 Test Class Scope
257
12.1.3 Test Helper Classes
259
12.1.4 Executing Tests
260
12.2 Test Methods
262
12.2.1 Test Fixture Methods
262
12.2.2 Given-When-Then Style
265
12.3 Class under Test
266
12.4 Naming
267
12.5 Assertions
269
12.5.1 Writing Effective Assertions
270
12.5.2 Checking Expected Exceptions
272
12.5.3 Unexpected Static Exceptions
274
12.5.4 Custom Assertions
275
12.5.5 Constraints
277
12.6 Test Doubles
279
12.6.1 Dependency Inversion and Test Doubles
279
12.6.2 ABAP Object-Oriented Test Double Framework
284
12.6.3 More ABAP Test Tools
288
12.7 Test Seams
289
12.8 Principles
291
12.8.1 Test-Driven Development
291
12.8.2 Clean Test Properties
292
12.8.3 Test Coverage
294
12.9 Summary
294
13 Packages
297
13.1 General Package Concepts
297
13.1.1 Use Cases
298
13.1.2 Reuse Levels
298
13.1.3 Cohesion
299
13.2 Package Concepts in ABAP
299
13.2.1 Package Types
299
13.2.2 Encapsulated Packages
300
13.2.3 Package Interfaces
301
13.2.4 Best Practices
303
13.3 Package Design Options
304
13.3.1 Side-by-Side Application Hierarchies
305
13.3.2 Layer-Based Hierarchies
306
13.3.3 Translation Relevance Split Hierarchies
306
13.4 Package Checks
308
13.4.1 What Are Package Checks?
310
13.4.2 Manual Execution of Package Checks
310
13.4.3 Automated Execution of Package Checks
312
13.4.4 How to Fix Package Check Errors
313
13.4.5 Best Practices
315
13.5 Consequences of Poor or No Package Strategy
316
13.6 Summary
316
14 How to Implement Clean ABAP
319
14.1 Common Understanding among Team Members
319
14.2 Collective Code Ownership
320
14.3 Clean Code Developer Initiative
322
14.4 Tackling the Broken Window Effect
323
14.4.1 Static Code Check
325
14.4.2 Metrics
325
14.4.3 Code Coverage
326
14.5 Code Review and Learning
326
14.5.1 Code Review Prefix
326
14.5.2 Style Guide
327
14.5.3 Make It Visible
327
14.5.4 Feedback Culture
327
14.6 Clean Code Advisor
330
14.7 Learning Techniques
330
14.7.1 Kata
330
14.7.2 Dojo
331
14.7.3 Code Retreat
331
14.7.4 Fellowship
332
14.7.5 Pair Programming
332
14.7.6 Mob Programming
333
14.7.7 Habits
333
14.8 Continuous Learning in Cross-Functional Teams
334
14.8.1 Profile of a Team Member
334
14.8.2 Cross-Functional Teams
335
14.8.3 Multipliers: Need for Topic Experts in the Team
336
14.8.4 Need for Community of Practice
336
14.9 Summary
337
The Authors
339
Index
341