about summary refs log tree commit diff stats
path: root/js/scripting-lang/tutorials/16_Best_Practices.md
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/tutorials/16_Best_Practices.md')
-rw-r--r--js/scripting-lang/tutorials/16_Best_Practices.md236
1 files changed, 236 insertions, 0 deletions
diff --git a/js/scripting-lang/tutorials/16_Best_Practices.md b/js/scripting-lang/tutorials/16_Best_Practices.md
new file mode 100644
index 0000000..8a6b246
--- /dev/null
+++ b/js/scripting-lang/tutorials/16_Best_Practices.md
@@ -0,0 +1,236 @@
+# Operator Spacing Best Practices
+
+## Why Spacing Matters
+
+The language uses spacing to distinguish between different types of operators and make expressions unambiguous. Proper spacing follows functional language conventions and makes your code more readable.
+
+## Minus Operator Spacing
+
+### Unary Minus (Negative Numbers)
+
+Unary minus works without parentheses and requires no leading space:
+
+```plaintext
+/* ✅ CORRECT - Unary minus without parentheses */
+-5;              /* negate(5) */
+-3.14;           /* negate(3.14) */
+-x;               /* negate(x) */
+f -5;            /* f(negate(5)) */
+map double -3;   /* map(double, negate(3)) */
+```
+
+### Binary Minus (Subtraction)
+
+Binary minus requires spaces on both sides:
+
+```plaintext
+/* ✅ CORRECT - Binary minus with spaces */
+5 - 3;           /* subtract(5, 3) */
+10 - 5;          /* subtract(10, 5) */
+x - y;           /* subtract(x, y) */
+3.14 - 1.5;      /* subtract(3.14, 1.5) */
+```
+
+### Legacy Syntax (Still Works)
+
+Legacy syntax continues to work for backward compatibility:
+
+```plaintext
+/* ✅ CORRECT - Legacy syntax still works */
+(-5);            /* negate(5) - explicit grouping */
+f (-5);          /* f(negate(5)) - explicit grouping */
+5-3;             /* subtract(5, 3) - legacy fallback */
+```
+
+### Complex Expressions
+
+Complex expressions with mixed operators work correctly:
+
+```plaintext
+/* ✅ CORRECT - Complex expressions */
+-5 + 3;          /* add(negate(5), 3) */
+-5 - 3;          /* subtract(negate(5), 3) */
+-5 * 3;          /* multiply(negate(5), 3) */
+-5 + 3 - 2;      /* subtract(add(negate(5), 3), 2) */
+```
+
+## General Operator Spacing
+
+### Binary Operators
+
+All binary operators should have spaces around them:
+
+```plaintext
+/* ✅ CORRECT - Binary operators with spaces */
+5 + 3;           /* add(5, 3) */
+5 * 3;           /* multiply(5, 3) */
+5 / 3;           /* divide(5, 3) */
+5 % 3;           /* modulo(5, 3) */
+5 ^ 3;           /* power(5, 3) */
+```
+
+### Comparison Operators
+
+Comparison operators require spaces:
+
+```plaintext
+/* ✅ CORRECT - Comparison operators with spaces */
+5 = 3;           /* equals(5, 3) */
+5 != 3;          /* notEquals(5, 3) */
+5 < 3;           /* lessThan(5, 3) */
+5 > 3;           /* greaterThan(5, 3) */
+5 <= 3;          /* lessEqual(5, 3) */
+5 >= 3;          /* greaterEqual(5, 3) */
+```
+
+### Logical Operators
+
+Logical operators require spaces:
+
+```plaintext
+/* ✅ CORRECT - Logical operators with spaces */
+true and false;  /* logicalAnd(true, false) */
+true or false;   /* logicalOr(true, false) */
+true xor false;  /* logicalXor(true, false) */
+```
+
+### Unary Operators
+
+Unary operators (except minus) don't require special spacing:
+
+```plaintext
+/* ✅ CORRECT - Unary operators */
+not true;        /* logicalNot(true) */
+not false;       /* logicalNot(false) */
+```
+
+## When to Use Parentheses
+
+### Explicit Grouping
+
+Use parentheses when you need explicit control over precedence:
+
+```plaintext
+/* ✅ CORRECT - Explicit grouping */
+(-5) + 3;        /* add(negate(5), 3) - explicit grouping */
+f (-5);          /* f(negate(5)) - explicit grouping */
+(5 + 3) * 2;     /* multiply(add(5, 3), 2) - explicit grouping */
+```
+
+### Complex Expressions
+
+Use parentheses to make complex expressions more readable:
+
+```plaintext
+/* ✅ CORRECT - Complex expressions with parentheses */
+(-5 + 3) * 2;    /* multiply(add(negate(5), 3), 2) */
+(-5) * (3 + 2);  /* multiply(negate(5), add(3, 2)) */
+```
+
+## Common Patterns
+
+### Function Calls with Negative Numbers
+
+```plaintext
+/* ✅ CORRECT - Function calls with negative numbers */
+double -5;       /* double(negate(5)) */
+map double -3;   /* map(double, negate(3)) */
+filter is_negative {-5, 0, 5};  /* filter(is_negative, {-5, 0, 5}) */
+```
+
+### Comparisons with Negative Numbers
+
+```plaintext
+/* ✅ CORRECT - Comparisons with negative numbers */
+-5 >= 0;         /* greaterEqual(negate(5), 0) */
+-5 < 0;          /* lessThan(negate(5), 0) */
+is_negative -5;  /* is_negative(negate(5)) */
+```
+
+### Arithmetic with Mixed Operators
+
+```plaintext
+/* ✅ CORRECT - Mixed arithmetic */
+-5 + 3 - 2;      /* subtract(add(negate(5), 3), 2) */
+5 * -3 + 2;      /* add(multiply(5, negate(3)), 2) */
+(-5) * 3 + 2;    /* add(multiply(negate(5), 3), 2) */
+```
+
+## Best Practices Summary
+
+### Do's
+
+- ✅ **Use spaces around binary operators**: `5 - 3`, `5 + 3`, `5 * 3`
+- ✅ **Unary minus works without parentheses**: `-5`, `f -5`
+- ✅ **Use parentheses for explicit grouping**: `(-5)`, `(5 + 3) * 2`
+- ✅ **Use spaces around comparison operators**: `5 = 3`, `5 < 3`
+- ✅ **Use spaces around logical operators**: `true and false`
+
+### Don'ts
+
+- ❌ **Don't omit spaces around binary operators**: `5-3`, `5+3` (legacy fallback)
+- ❌ **Don't add spaces after unary minus**: `- 5` (legacy fallback)
+- ❌ **Don't use inconsistent spacing**: `5- 3`, `5 -3` (legacy fallback)
+
+### When in Doubt
+
+- **Use spaces around binary operators** - it's always correct and more readable
+- **Unary minus works without parentheses** - `-5` is the preferred syntax
+- **Use parentheses for explicit grouping** - when you need to control precedence
+- **Follow functional language conventions** - spaces around operators are standard
+
+## Examples in Context
+
+### Data Processing
+
+```plaintext
+/* Process data with proper spacing */
+data : {-5, 0, 5, 10, 15};
+is_positive : x -> x > 0;
+double : x -> x * 2;
+sum : x -> reduce add 0 x;
+
+/* Pipeline with proper spacing */
+result : sum map double filter is_positive data;
+/* Reads: sum (map double (filter is_positive data)) */
+/* Result: 60 (positive: {5,10,15}, doubled: {10,20,30}, sum: 60) */
+```
+
+### Validation Logic
+
+```plaintext
+/* Validation with proper spacing */
+validate_age : age -> (age >= 0) and (age <= 120);
+validate_salary : salary -> (salary >= 0) and (salary <= 1000000);
+
+/* Test validation */
+test1 : validate_age -5;    /* false */
+test2 : validate_age 25;    /* true */
+test3 : validate_salary 50000;  /* true */
+```
+
+### Mathematical Expressions
+
+```plaintext
+/* Mathematical expressions with proper spacing */
+calculate_discount : price discount_rate -> 
+  price - (price * discount_rate);
+
+apply_tax : price tax_rate -> 
+  price + (price * tax_rate);
+
+/* Use the functions */
+final_price : apply_tax (calculate_discount 100 0.1) 0.08;
+/* Result: 97.2 (discount: 90, tax: 7.2) */
+```
+
+## Key Takeaways
+
+1. **Spacing distinguishes operators** - unary vs binary minus
+2. **Unary minus works without parentheses** - `-5` is preferred
+3. **Binary operators need spaces** - `5 - 3`, `5 + 3`, `5 * 3`
+4. **Legacy syntax still works** - but spaces are recommended
+5. **Parentheses for explicit grouping** - when you need control
+6. **Follow functional conventions** - spaces around operators are standard
+
+**Remember**: Proper spacing makes your code more readable and follows functional language conventions! 🚀
\ No newline at end of file
<vc@akkartik.com> 2015-02-17 16:57:37 -0800 committer Kartik K. Agaram <vc@akkartik.com> 2015-02-17 17:14:45 -0800 775 - starting to reorg C++ mu to use layers' href='/akkartik/mu/commit/cpp/literate/002trace?h=hlt&id=515309164793b2e03c15954bf8a89f0f288a7f2c'>51530916 ^
41f5188d ^
51530916 ^

d03028a3 ^
35064671 ^
41f5188d ^
5f98a10c ^


41f5188d ^
51530916 ^

41f5188d ^
e00d4854 ^
51530916 ^


41f5188d ^
51530916 ^
41f5188d ^
5feb36ff ^
41f5188d ^
77cdc6d0 ^
5feb36ff ^
51530916 ^

51530916 ^






7feea75b ^
5f98a10c ^
51530916 ^
e00d4854 ^

51530916 ^
51530916 ^
35064671 ^
51530916 ^
35064671 ^
51530916 ^
35064671 ^
51530916 ^



41f5188d ^
51530916 ^
029c04e2 ^

f4b6b2c2 ^
029c04e2 ^


51530916 ^

f4b6b2c2 ^
23eed027 ^
f4b6b2c2 ^
f4b6b2c2 ^





51530916 ^



53a569b8 ^
51530916 ^
baaf6d65 ^
5af83346 ^

51530916 ^
2e72dc12 ^
06584c52 ^
51530916 ^
ac0e9db5 ^

51530916 ^
ac0e9db5 ^
41f5188d ^

5feb36ff ^
41f5188d ^
51530916 ^

5feb36ff ^
51530916 ^


ac0e9db5 ^
51530916 ^
ac0e9db5 ^
41f5188d ^
51530916 ^


54e4548d ^
41f5188d ^
51530916 ^



41f5188d ^
d1bd0439 ^
ac0e9db5 ^

41f5188d ^
d7494165 ^
51530916 ^

41f5188d ^
d7494165 ^
51530916 ^


51530916 ^

41f5188d ^

51530916 ^

41f5188d ^
51530916 ^
5feb36ff ^
41f5188d ^
eba30983 ^
51530916 ^
eba30983 ^
51530916 ^



5f98a10c ^


51530916 ^
5f98a10c ^

51530916 ^



41f5188d ^

51530916 ^


7be15b8b ^
05d17773 ^
51530916 ^

51530916 ^



51530916 ^

ac0e9db5 ^
51530916 ^
ac0e9db5 ^

51530916 ^


7be15b8b ^
51530916 ^




7be15b8b ^








d7494165 ^












51530916 ^
51530916 ^



2fb94e3c ^



51530916 ^




51530916 ^


5feb36ff ^
51530916 ^

51530916 ^






467046ef ^

51530916 ^
7feea75b ^



77cdc6d0 ^


7feea75b ^


7feea75b ^





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372