|
| 1 | +\input source_header.tex |
| 2 | + |
| 3 | +\begin{document} |
| 4 | + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 5 | + \docheader{beta release}{Source}{Style Guide} |
| 6 | + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7 | + |
| 8 | +This is the style guide for the language Source, |
| 9 | +the official language of the book \emph{Structure and Interpretation |
| 10 | +of Computer Programs}, JavaScript Adaptation. |
| 11 | +You have never heard of Source? No worries! It was invented |
| 12 | +it just for the purpose of the book. Source is a sublanguage of |
| 13 | +\href{http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf}{ |
| 14 | +ECMAScript 2016 ($7^{\textrm{th}}$ Edition)}. This style guide is a compilation |
| 15 | +of commonly accepted rules, with the possible exception of conditional expressions |
| 16 | +on page~\pageref{condex}. |
| 17 | + |
| 18 | +\section*{Indentation} |
| 19 | + Use a four space indent. This means that if a line $L$ ends with \verb#{#, then |
| 20 | +the following line starts with four more spaces than required to reach the keyword |
| 21 | +\lstinline{function}, \lstinline{if} or \lstinline{else} in $L$. |
| 22 | +Example: |
| 23 | +\begin{lstlisting} |
| 24 | +function make_abs_adder(x) { |
| 25 | + function the_adder(y) { |
| 26 | + if (x >= 0) { |
| 27 | + return x + y; |
| 28 | + } else { |
| 29 | + return -x + y; |
| 30 | + } |
| 31 | + } |
| 32 | + return the_adder; |
| 33 | +} |
| 34 | +\end{lstlisting} |
| 35 | +Note the four spaces before \lstinline{return}, and then indentation of the \lstinline{if} |
| 36 | +statement four spaces to the right of the preceding \lstinline{function}. |
| 37 | + |
| 38 | +\section*{Line Length} |
| 39 | + Lines should be truncated such that they do not require excessive horizontal scrolling. |
| 40 | + As a guide, it should be \emph{no more than 80 characters}. |
| 41 | + |
| 42 | +\section*{Curly Braces} |
| 43 | + Curly braces should \emph{open on the same line and close on a new line}. |
| 44 | + Statements within the curly braces should be indented one more level. |
| 45 | + |
| 46 | +\begin{lstlisting} |
| 47 | +// function declaration |
| 48 | +function my_function(<parameters>) { |
| 49 | + <statements here should be indented> |
| 50 | +} |
| 51 | + |
| 52 | +// if-else |
| 53 | +if (<predicate>) { |
| 54 | + ... |
| 55 | +} else if (<predicate>) { |
| 56 | + ... |
| 57 | +} else { |
| 58 | + ... |
| 59 | +} |
| 60 | + |
| 61 | +// nested-if |
| 62 | +if (<predicate>) { |
| 63 | + ... |
| 64 | + if (<some other predicate>) { |
| 65 | + ... |
| 66 | + } |
| 67 | + ... |
| 68 | +} |
| 69 | +\end{lstlisting} |
| 70 | + |
| 71 | +\paragraph{Always use curly braces.} Even if the block consists of only |
| 72 | + one statement. |
| 73 | + This is required in Source, and recommended in JavaScript. |
| 74 | + |
| 75 | +\begin{lstlisting} |
| 76 | +// correct Source |
| 77 | +if (<predicate>) { |
| 78 | + return x + y; |
| 79 | +} else { |
| 80 | + return x * y; |
| 81 | +} |
| 82 | + |
| 83 | +// incorrect Source |
| 84 | +if (<predicate>) |
| 85 | + return x + y; |
| 86 | +else |
| 87 | + return x * y; |
| 88 | + |
| 89 | +// worse |
| 90 | +if (<predicate>) return x + y; |
| 91 | +else return x * y; |
| 92 | +\end{lstlisting} |
| 93 | + |
| 94 | +\section*{Whitespace} |
| 95 | + |
| 96 | + \subsection*{Operators} |
| 97 | + Leave a single space between binary and ternary operators. |
| 98 | + |
| 99 | +\begin{lstlisting} |
| 100 | +// good style |
| 101 | +const x = 1 + 1; |
| 102 | + |
| 103 | +// bad style |
| 104 | +const x=1+1; |
| 105 | + |
| 106 | +// good style |
| 107 | +return (x === 0) ? "zero" : "not zero"; |
| 108 | + |
| 109 | +// bad style |
| 110 | +return (x === 0)?"zero":"not zero"; |
| 111 | +\end{lstlisting} |
| 112 | + |
| 113 | + Do not leave a space between unary operators and the variable involved. |
| 114 | + |
| 115 | +\begin{lstlisting} |
| 116 | +// good style |
| 117 | +const negative_x = -x; |
| 118 | + |
| 119 | +// bad style |
| 120 | +const negative_x = - x; |
| 121 | +\end{lstlisting} |
| 122 | + |
| 123 | +\subsection*{Function Definitions} |
| 124 | + |
| 125 | +Keep the parameters and the body expression of a function definition expression |
| 126 | +in one line, if they are short enough. |
| 127 | + |
| 128 | +If they are lengthy, use |
| 129 | +indentation. The indentation starts just below the first character of the \textit{parameter}, |
| 130 | +or the open parenthesis, if there are multiple parameters. If the body expression does not |
| 131 | +fit in one line, use indentation so that the subsequent lines start after the blank character |
| 132 | +after the arrow. |
| 133 | + |
| 134 | +\begin{lstlisting} |
| 135 | +// good style |
| 136 | +function count_buttons(garment) { |
| 137 | + return accumulate((sleaves, total) => sleaves + total, |
| 138 | + 0, |
| 139 | + map(jacket |
| 140 | + => is_checkered(jacket) |
| 141 | + ? count_buttons(jacket) |
| 142 | + : 1, |
| 143 | + garment)); |
| 144 | +} |
| 145 | + |
| 146 | +// bad style |
| 147 | +function count_buttons(garment) { |
| 148 | + return accumulate((sleaves, total) |
| 149 | + => |
| 150 | + sleaves + total, |
| 151 | + 0, |
| 152 | + map(jacket |
| 153 | + => is_checkered(jacket) |
| 154 | + ? count_buttons(jacket) |
| 155 | + : 1, |
| 156 | + garment)); |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +\end{lstlisting} |
| 161 | + |
| 162 | + |
| 163 | + \subsection*{Conditional Expressions} |
| 164 | +\label{condex} |
| 165 | +Keep the three components of a conditional expression in one line, if they are short enough. |
| 166 | + |
| 167 | +\begin{lstlisting} |
| 168 | +// good style |
| 169 | +const aspect_ratio = landscape ? 4 / 3 : 3 / 4; |
| 170 | + |
| 171 | +// bad style |
| 172 | +const aspect_ratio = landscape |
| 173 | + ? 4 / 3 |
| 174 | + : 3 / 4; |
| 175 | +\end{lstlisting} |
| 176 | + |
| 177 | +If the \textit{consequent-expression} or \textit{alternative-expression} are lengthy, use |
| 178 | +indentation. The indentation starts just below the \textit{predicate}. |
| 179 | + |
| 180 | +\begin{lstlisting} |
| 181 | +// good style |
| 182 | +function A(x,y) { |
| 183 | + return y === 0 |
| 184 | + ? 0 |
| 185 | + : x === 0 |
| 186 | + ? 2 * y |
| 187 | + : y === 1 |
| 188 | + ? 2 |
| 189 | + : A(x - 1, A(x, y - 1)); |
| 190 | + |
| 191 | +// bad style |
| 192 | +function A(x,y) { |
| 193 | + return y === 0 ? 0 : x === 0 ? 2 * y : y === 1 ? 2 : A(x - 1, A(x, y - 1)); |
| 194 | +} |
| 195 | + |
| 196 | +// bad style |
| 197 | +function A(x,y) { |
| 198 | + return y === 0 |
| 199 | + ? 0 |
| 200 | + : x === 0 |
| 201 | + ? 2 * y |
| 202 | + : y === 1 |
| 203 | + ? 2 |
| 204 | + : A(x - 1, A(x, y - 1)); |
| 205 | +} |
| 206 | +\end{lstlisting} |
| 207 | + |
| 208 | + \subsection*{Conditional Statements and Functions} |
| 209 | + Leave a single space between the \lstinline{if} statement and the first parenthesis and before every opening curly brace. |
| 210 | + Start your \lstinline{else} statement on the same line as the closing curly brace, with a single space between them. |
| 211 | + |
| 212 | +\begin{lstlisting} |
| 213 | +if (<predicate>) { |
| 214 | + ... |
| 215 | +} else if (<predicate>) { |
| 216 | + ... |
| 217 | +} else { |
| 218 | + ... |
| 219 | +} |
| 220 | +\end{lstlisting} |
| 221 | + |
| 222 | + When calling or declaring a function with multiple parameters, leave a space after each comma. |
| 223 | + There should also be no spaces before your parameter list. |
| 224 | + |
| 225 | +\begin{lstlisting} |
| 226 | +// good style |
| 227 | +function my_function(arg1, arg2, arg3) { |
| 228 | + ... |
| 229 | +} |
| 230 | + |
| 231 | +// bad style |
| 232 | +function my_function (arg1, arg2, arg3) { |
| 233 | + ... |
| 234 | +} |
| 235 | + |
| 236 | +// good style |
| 237 | +my_function(1, 2, 3); |
| 238 | + |
| 239 | +// bad style |
| 240 | +my_function(1,2,3); |
| 241 | + |
| 242 | +// bad style |
| 243 | +my_function (1, 2, 3); |
| 244 | +\end{lstlisting} |
| 245 | + |
| 246 | + There should be no spaces after your opening parenthesis and before your closing parenthesis. |
| 247 | + |
| 248 | +\begin{lstlisting} |
| 249 | +// bad style |
| 250 | +function my_function( arg1, arg2, arg3 ) { |
| 251 | + ... |
| 252 | +} |
| 253 | + |
| 254 | +// bad style |
| 255 | +my_function( 1, 2, 3 ); |
| 256 | + |
| 257 | +// bad style |
| 258 | +if ( x === 1 ) { |
| 259 | + ... |
| 260 | +} |
| 261 | + |
| 262 | +// good style |
| 263 | +function my_function(arg1, arg2, arg3) { |
| 264 | + ... |
| 265 | +} |
| 266 | + |
| 267 | +// good style |
| 268 | +my_function(1, 2, 3); |
| 269 | + |
| 270 | +// good style |
| 271 | +if (x === 1) { |
| 272 | + ... |
| 273 | +} |
| 274 | +\end{lstlisting} |
| 275 | + |
| 276 | + Clean up \emph{all trailing whitespace} before submitting your program. |
| 277 | + |
| 278 | +\section*{Variables} |
| 279 | +\subsection*{Naming} |
| 280 | +When naming variables, use \emph{underscores} to separate words. Examples: \lstinline{my_variable}, \lstinline{x}, \lstinline{rcross_bb}. |
| 281 | + |
| 282 | +\subsection*{Nesting} |
| 283 | +Do not use the same variable name for nested scope. Examples: |
| 284 | +\begin{lstlisting} |
| 285 | +// bad program |
| 286 | +const x = 1; |
| 287 | +function f(x) { |
| 288 | + // here, the name x declared using const |
| 289 | + // is ``shadowed'' by the formal parameter x |
| 290 | + ... |
| 291 | +} |
| 292 | +\end{lstlisting} |
| 293 | + |
| 294 | +\begin{lstlisting} |
| 295 | +// another bad program |
| 296 | +function f(x) { |
| 297 | + return x => ...; |
| 298 | + // here, the formal parameter x of f is ``shadowed'' |
| 299 | + // by the formal parameter of the returned function |
| 300 | +} |
| 301 | +\end{lstlisting} |
| 302 | + |
| 303 | +\begin{lstlisting} |
| 304 | +// a third bad program |
| 305 | +function f(x) { |
| 306 | + const x = 1; |
| 307 | + // in the following, the formal parameter x of f |
| 308 | + // is ``shadowed'' by the const declaration of x. |
| 309 | + ... |
| 310 | +} |
| 311 | +\end{lstlisting} |
| 312 | +Finally, the worst case would be a (surely accidental) |
| 313 | +use of the same variable name for two parameters of a function. |
| 314 | +In this case, the second variable is not visible; it is ``shadowed'' |
| 315 | +by the first. |
| 316 | +\begin{lstlisting} |
| 317 | +// worse than the above |
| 318 | +function f(x, x) { |
| 319 | + ... |
| 320 | +} |
| 321 | +\end{lstlisting} |
| 322 | + |
| 323 | +\section*{Comments} |
| 324 | +Comments should be used to describe and explain statements |
| 325 | +that might not be obvious to a reader. |
| 326 | +Redundant comments should be avoided. The comment in the following program is useful |
| 327 | +because it explains what \lstinline{x} and \lstinline{y} stands for and what type of |
| 328 | +object is meant. |
| 329 | +\begin{lstlisting} |
| 330 | +// area of rectangle with sides x and y |
| 331 | +function area(x, y) { |
| 332 | + return x * y; |
| 333 | +} |
| 334 | +\end{lstlisting} |
| 335 | +The programmer has decided to use the short word \lstinline{area} as the name of the function, |
| 336 | +which is ok, as long as it is clear that the geometric objects that the program deals with |
| 337 | +are always rectangles. |
| 338 | + |
| 339 | +An example for bad style as a result of a redundant comment follows here: |
| 340 | +\begin{lstlisting} |
| 341 | +// square computes the square of the argument x |
| 342 | +function square(x) { |
| 343 | + return x * x; |
| 344 | +} |
| 345 | +\end{lstlisting} |
| 346 | +% |
| 347 | +For multi-line comments, use \lstinline{/* ... */} and for single line comments, use \lstinline{//}. |
| 348 | + |
| 349 | +\end{document} |
| 350 | + |
| 351 | + |
0 commit comments