You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+92Lines changed: 92 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -431,3 +431,95 @@ message.send()
431
431
```
432
432
433
433
**[⬆ back to top](#table-of-contents)**
434
+
435
+
###Functions should only be one level of abstraction
436
+
437
+
When you have more than one level of abstraction, your function is usually doing too
438
+
much. Splitting up functions leads to reusability and easier testing.
439
+
440
+
**Bad:**
441
+
442
+
```python
443
+
defparse_better_js_alternative(code:str) ->None:
444
+
regexes= [
445
+
# ...
446
+
]
447
+
448
+
statements= regexes.split()
449
+
tokens= []
450
+
for regexin regexes:
451
+
for statementin statements:
452
+
# ...
453
+
454
+
ast= []
455
+
for tokenin tokens:
456
+
# Lex.
457
+
458
+
for nodein ast:
459
+
# Parse.
460
+
```
461
+
462
+
**Good:**
463
+
464
+
```python
465
+
defparse_better_js_alternative(code:str) ->None:
466
+
tokens= tokenize(code)
467
+
syntax_tree= parse(tokens)
468
+
469
+
for nodein syntax_tree:
470
+
# Parse.
471
+
472
+
473
+
deftokenize(code:str) ->list:
474
+
REGEXES= [
475
+
# ...
476
+
]
477
+
478
+
statements= code.split()
479
+
tokens= []
480
+
for regexinREGEXES:
481
+
for statementin statements:
482
+
# Append the statement to tokens.
483
+
484
+
return tokens
485
+
486
+
487
+
defparse(tokens:list) ->list:
488
+
syntax_tree= []
489
+
for tokenin tokens:
490
+
# Append the parsed token to the syntax tree.
491
+
492
+
return syntax_tree
493
+
```
494
+
495
+
**[⬆ back to top](#table-of-contents)**
496
+
497
+
###Don't use flags as function parameters
498
+
499
+
Flags tell your user that this function does more than one thing. Functions should do one thing. Split your functions if they are following different code paths based on a boolean.