The Elements of MATLAB Style is a guide for both new and experienced MATLAB programmers. It provides a comprehensive collection of standards and guidelines for creating solid MATLAB code that will be easy to understand, enhance, and maintain. It is written for both individuals and those working in teams in which consistency is critical. This is the only book devoted to MATLAB style and best programming practices, focusing on how MATLAB code can be written in order to maximize its effectiveness. Just as Strunk and White's The Elements of Style provides rules for writing in the English language, this book provides conventions for formatting, naming, documentation, programming and testing. It includes many concise examples of correct and incorrect usage, as well as coverage of the latest language features. The author also provides recommendations on use of the integrated development environment features that help produce better, more consistent software.
Author(s): Richard K. Johnson
Edition: 1
Publisher: Cambridge University Press
Year: 2010
Language: English
Pages: 181
Cover......Page 1
Half-title......Page 3
Title......Page 5
Copyright......Page 6
Contents......Page 7
Audience......Page 11
Introduction......Page 13
Disclaimer......Page 15
1. Avoid Causing Confusion......Page 16
4. Maintain the Style of the Original......Page 17
5. Document Style Deviations......Page 18
6. Keep Content Within the First Eighty Columns......Page 19
8. Indent Statement Groups Three or Four Spaces......Page 20
10. Do Not Use Hard Tabs......Page 21
12. Define Each Variable on a Separate Line......Page 22
13. Use Argument Alignment if it Enhances Readability......Page 23
16. Surround =, &, |, &&, and || by Spaces......Page 24
17. Use White Space Around Operators When it Enhances Readability......Page 25
19. Insert Spaces for Multiple Commands in One Line......Page 26
23. Break Code of Any Appreciable Length into Blocks......Page 27
27. Use Editor Cells......Page 28
Formatting Summary......Page 29
3. Naming......Page 30
28. Use Meaningful Names......Page 31
30. Use Consistent Names......Page 32
32. Avoid Cryptic Abbreviations......Page 33
34. Avoid Names that Differ Only by Capitalization......Page 34
36. Avoid Names with Hard-to-Read Character Sequences......Page 35
39. Avoid Ambiguous or Vague Names......Page 36
42. Use lowerCamelCase for Compound Variable Names......Page 37
44. Limit Use of Very Short Names to Variables with a Small Scope......Page 38
47. Follow a Consistent Convention on Pluralization......Page 39
49. Use the Suffix No or Prefix i for Variables Representing a Single Entity Number......Page 40
51. Embed is, has, etc., in Boolean Variable Names......Page 41
53. Use the Expected Logical Names and Values......Page 42
55. Avoid Hungarian Notation......Page 43
57. Avoid Reusing a Variable for Different Contents......Page 44
59. Use All Uppercase for Constant Names with Local Scope......Page 45
61. Use Meaningful Names for Constants......Page 46
65. Do Not Include the Name of the Structure in a Fieldname......Page 47
67. Name Cell Arrays Following the Style for Variables......Page 48
68. Give Functions Meaningful Names......Page 49
70. Follow a Case Convention for Function Names......Page 50
72. Use Expected Verbs in Expected Ways......Page 51
73. Use the Prefix is for Boolean Functions......Page 52
76. Use Numbers Inside Function Names Only for Common Conventions......Page 53
78. Use Nouns When Naming Classes......Page 54
81. Name Properties Like Structure Fields......Page 55
83. Name Accessor Methods after their Properties......Page 56
86. Use Sortable Numbering in Data Filenames......Page 57
87. Use ISO Date Format......Page 59
Naming Summary......Page 60
89. Document Each Module Before or During Its Implementation......Page 61
92. Consider Documenting Some Changes in Header Comments......Page 62
Comments......Page 63
97. Be Sure that Comments Agree with the Code......Page 64
98. Revise Comments to be Correct When the Code is Changed......Page 65
100. Clean up Commented Out Code before Release......Page 66
103. Minimize Use of End-Line Comments......Page 67
106. Consider Documenting Constant Assignments Where They are Defined......Page 68
109. Use Complete Sentences in Descriptive Blocks......Page 69
114. Write All Comments in English......Page 70
115. Format the Header Comments for Easy Publishing as Documentation......Page 71
118. Document the Function Interface in the Syntax Section......Page 73
119. Use the Actual Function Name Case in Comments......Page 74
120. Describe the Function Arguments in the Description Section......Page 75
124. Include See also in the Header Comments......Page 76
126. Format Header Comments of classdef Files for the Help Browser......Page 77
129. Place a Blank Line or Cell Break before a Block Comment......Page 78
130. Do Not Use Comment Blocks for Block Comments......Page 79
131. Use Comment Block Syntax to Temporarily Bypass a Block of Code......Page 80
Documentation Summary......Page 81
135. Avoid Cryptic Code......Page 83
139. Use Programming Patterns or Idioms......Page 84
141. Beware of Mistyping Variable Names......Page 85
142. Minimize the Use of Literal Numbers in Statements......Page 86
145. Avoid Mixing Units within a Program......Page 87
147. Limit Boolean Variable Values to True or False......Page 88
149. Use Appropriate Numerical Class Conversions......Page 89
150. Minimize the Use of Global Variables......Page 90
151. Minimize the Use of Global Constants......Page 91
153. Do Not Declare Unrelated Variables in a Single Line......Page 92
156. Use isempty......Page 93
159. Consider Using Character Arrays for Speed......Page 94
162. Use Structures for Metadata......Page 95
165. Be Careful with Fieldnames......Page 96
166. Consider Using Cell Arrays for Strings......Page 97
168. Consider Using Cell Arrays for Ragged Arrays......Page 98
169. Write Short Expressions......Page 99
172. Use Parentheses......Page 100
173. Use a Clear Form for Relational Expressions......Page 101
176. Avoid Use of eval When Possible......Page 102
177. Initialize Loop Variables Immediately Before the Loop......Page 103
180. Minimize the Use of break in Loops......Page 104
181. Minimize the Use of continue in Loops......Page 105
182. Avoid Unnecessary Computation within Loops......Page 106
184. Be Careful of Count Errors in while Loops......Page 107
186. In General, Include an else Statement with if......Page 108
188. Avoid Unnecessary if Constructs......Page 109
190. Avoid Unnecessary elseif Blocks......Page 110
191. Avoid Unnecessary Levels of Nesting in Control Structures......Page 111
193. Avoid the Conditional Expression if 0......Page 112
195. Consider Using a Cell Array to Simplify a switch Construct......Page 113
198. When Either if or switch Can Work, Use the More Readable One......Page 114
201. In General Use isequal Rather Than ==......Page 115
202. Be Thoughtful with Vectorization......Page 116
204. Modularize......Page 117
207. Design for Loose Coupling......Page 118
210. Write for High Cohesion......Page 119
214. Write a Function At One Level of Abstraction......Page 120
215. Write Convenience Functions......Page 121
219. Make Logical Output and Function Name Consistent......Page 122
221. Write Arguments in Useful Order......Page 123
224. Use a Structure to Replace a Long List of Function Arguments......Page 124
226. Consider varargin and varargout......Page 125
228. In General, Use Caller Variable Names Consistent with the Function Argument Names......Page 126
232. Avoid Including Hidden Side Effects......Page 127
234. Write Input Functions......Page 128
237. Use feof for Reading Files......Page 129
240. Provide for Automation......Page 130
241. Keep Classes Simple......Page 131
242. Avoid Classes with Ambiguous Overlap......Page 132
244. Follow Constructor Conventions......Page 133
246. Write Methods That You Can Unit Test......Page 134
248. Avoid Incomplete Public Methods......Page 135
249. Try to Make Properties Private......Page 136
252. Validate Method Argument Values......Page 137
256. Avoid Unconventional Usage of Overload Names......Page 138
260. Use Java Syntax for Java Methods......Page 139
262. Prepare for Errors......Page 140
265. Use Exceptions......Page 141
267. Use Appropriate Assertions......Page 142
Tests......Page 143
271. Write Small Tests......Page 144
275. Write Tests You Can Automate......Page 145
278. Develop Test Patterns......Page 146
281. Follow Database Conventions......Page 147
Programming Summary......Page 148
285. Put Test Files in a Separate Directory......Page 150
288. Provide for Integration with MATLAB......Page 151
292. Organize Your Project-Specific Files by Project Directory......Page 152
293. Organize Your Data Directory for Ease of Access......Page 153
Organization Summary......Page 154
7. Development......Page 156
295. Include Appropriate Flexibility......Page 157
300. Recompute When Data Changes......Page 158
Development Practices......Page 159
307. Run all Tests Before Release......Page 160
309. Use Smart Indent......Page 161
313. Use the Debugger Effectively......Page 162
315. Use the Profiler......Page 163
Development Summary......Page 164
Keywords......Page 167
is∗ function list......Page 169
Glossary......Page 171
Bibliography......Page 177
Index......Page 179