|
| 1 | +# Sack Style Guide |
| 2 | + |
| 3 | +## Naming convention |
| 4 | + |
| 5 | +Functions and variables should use `snake_case`, unless they are supposed to be used as constants in which case they should use `SCREAMING_SNAKE_CASE`. |
| 6 | + |
| 7 | +Never use `l` (lower case L), `I` (uppercase i), or `O` (uppercase o), for variable names as they look too much like numbers. |
| 8 | + |
| 9 | +If you don't want people to use your function/variable unless they are sure they know exactly what they are doing, prefix it with a single underscore. |
| 10 | +Variables and functions specific to a certain implementation should have two underscores, the implementations name, an underscore, and then name of the variable/function. |
| 11 | + |
| 12 | +## Comments |
| 13 | + |
| 14 | +Comments should be directly before the line they describe, one space should be put after the `#`. |
| 15 | + |
| 16 | +Comments should start with an uppercase letter but should not end with a period, however an exclamation or question mark is allowed. For example: |
| 17 | + |
| 18 | +``` |
| 19 | +# Calculates foo |
| 20 | +func calc_foo () { |
| 21 | + # Returns 4 |
| 22 | + return 4; |
| 23 | +} |
| 24 | +
|
| 25 | +# Stores foo |
| 26 | +let foo = calc_foo(); |
| 27 | +``` |
| 28 | + |
| 29 | +## Spacing |
| 30 | + |
| 31 | +Every binary operator, parenthesis, and brace should have 1 space around it, with the exception of function calls, list indexes, and semicolons. Unary operators should not have space. For example: |
| 32 | + |
| 33 | +``` |
| 34 | +if a[0] == -b { |
| 35 | + print( "Hello!" ); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Indentation |
| 40 | + |
| 41 | +After every left brace should be an indent of 4 spaces, which should end before the closing right brace. For example: |
| 42 | +``` |
| 43 | +loop (while foo > 2) { |
| 44 | + print( "Foo is greater than 2" ); |
| 45 | + foo -= 1; |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Trailing expressions follow the same rules but with with `[]` and `()`. For example: |
| 50 | +``` |
| 51 | +takes_long_args( |
| 52 | + this_is_a_really_really_long_argument_name, |
| 53 | + these_long_names_should_change |
| 54 | +); |
| 55 | +``` |
| 56 | + |
| 57 | +Trailing expressions may be on the same line as other trailing expressions. For example: |
| 58 | +``` |
| 59 | +let my_list = [ |
| 60 | + 1, 2, 3, |
| 61 | + 4, 5, 6 |
| 62 | + 7, 8, 9, |
| 63 | + 0 |
| 64 | +]; |
| 65 | +``` |
| 66 | + |
| 67 | +Trailing binary operators should have the operator on the second line, unless the binop changes the a value. for example: |
| 68 | +``` |
| 69 | +let sum = |
| 70 | + a_really_long_var_name |
| 71 | + + some_other_long_var; |
| 72 | +``` |
| 73 | + |
| 74 | +## Brace style |
| 75 | + |
| 76 | +All left braces should be inline with the rest of the line, all right braces should be on there own line (with the exception of `else` and `else if`), for example: |
| 77 | + |
| 78 | +``` |
| 79 | +func foo ( bar ) { |
| 80 | + if bar { |
| 81 | + # Some code here... |
| 82 | + } else { |
| 83 | + # More code here... |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Code location |
| 89 | + |
| 90 | +Imports should always be at the top of files, and sorted alphabetically, with an extra newline after the final import. |
| 91 | + |
| 92 | +All globals variables should be after imports, with an extra newline after the final variable. |
| 93 | + |
| 94 | +Following after global variables should be functions. All functions should have a newline after the final `}`. |
| 95 | + |
| 96 | +All other code should be at the bottom of the file. |
| 97 | + |
| 98 | +## Returning |
| 99 | + |
| 100 | +When returning from all branches of an `if-(else-if)*-else`, it is better to remove the else. For example: |
| 101 | +``` |
| 102 | +# Wrong |
| 103 | +func is_even ( a ) { |
| 104 | + if ( type(a) != "Number" ) { |
| 105 | + return false; |
| 106 | + } else { |
| 107 | + return a % 2; |
| 108 | + } |
| 109 | +} |
| 110 | +
|
| 111 | +# Correct |
| 112 | +func is_even ( a ) { |
| 113 | + if ( type(a) != "Number" ) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + return a % 2; |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +## Unary vs binary increment/deincrement operators |
| 121 | + |
| 122 | +Unary increment and deincrement operators (`++` and `--`) should only be used when the value is not needed, they should not be used when `+=` or `-=` would work. |
0 commit comments