|
| 1 | +==================================== |
| 2 | +PEP 1 - Think Language Style Guide |
| 3 | +==================================== |
| 4 | + |
| 5 | +Introduction |
| 6 | +------------ |
| 7 | + |
| 8 | +This document provides style guidelines for the Think programming language. Think emphasizes readability and clear expression of problem decomposition. This guide aims to help programmers write code that is consistent and maintainable. |
| 9 | + |
| 10 | +Program Structure |
| 11 | +----------------- |
| 12 | + |
| 13 | +Objectives |
| 14 | +^^^^^^^^^^ |
| 15 | + |
| 16 | +Each program must begin with a single, clear objective that defines its purpose. The objective should be concise but descriptive: |
| 17 | + |
| 18 | +.. code-block:: python |
| 19 | +
|
| 20 | + # Good |
| 21 | + objective "Calculate student grades and generate report" |
| 22 | +
|
| 23 | + # Bad - Too vague |
| 24 | + objective "Process data" |
| 25 | +
|
| 26 | + # Bad - Multiple purposes |
| 27 | + objective "Calculate grades and manage attendance and track assignments" |
| 28 | +
|
| 29 | +Tasks |
| 30 | +^^^^^ |
| 31 | + |
| 32 | +Tasks represent major components of your solution. Each task should have a clear, specific purpose. |
| 33 | + |
| 34 | +**Naming Conventions:** |
| 35 | + |
| 36 | +- Use clear, descriptive names |
| 37 | +- Names should be action-oriented |
| 38 | +- Be specific about purpose |
| 39 | +- Use natural language phrasing |
| 40 | + |
| 41 | +.. code-block:: python |
| 42 | +
|
| 43 | + # Good |
| 44 | + task "Process Grades": |
| 45 | + step "Initialize": |
| 46 | + grades = [85, 92, 78] |
| 47 | +
|
| 48 | + # Bad - too vague |
| 49 | + task "Do Stuff": |
| 50 | + step "Setup": |
| 51 | + x = [85, 92, 78] |
| 52 | +
|
| 53 | +**Organization:** |
| 54 | + |
| 55 | +- Group related tasks together |
| 56 | +- Each task should have a single responsibility |
| 57 | +- Order tasks logically based on data flow |
| 58 | + |
| 59 | +.. code-block:: python |
| 60 | +
|
| 61 | + # Good - Clear progression |
| 62 | + task "Load Data": |
| 63 | + step "Read File": |
| 64 | + grades = load_csv("grades.csv") |
| 65 | +
|
| 66 | + task "Process Data": |
| 67 | + step "Calculate Average": |
| 68 | + total = sum(grades) |
| 69 | + average = total / len(grades) |
| 70 | +
|
| 71 | + # Bad - Mixed responsibilities |
| 72 | + task "Do Everything": |
| 73 | + step "Process": |
| 74 | + grades = load_csv("grades.csv") |
| 75 | + total = sum(grades) |
| 76 | + average = total / len(grades) |
| 77 | +
|
| 78 | +Steps |
| 79 | +^^^^^ |
| 80 | + |
| 81 | +Steps represent specific actions within a task. Each step should do one thing clearly. |
| 82 | + |
| 83 | +**Naming Conventions:** |
| 84 | + |
| 85 | +- Use descriptive action verbs |
| 86 | +- Be specific about what the step does |
| 87 | +- Use natural, readable phrases |
| 88 | + |
| 89 | +.. code-block:: python |
| 90 | +
|
| 91 | + # Good |
| 92 | + step "Initialize Variables": |
| 93 | + count = 0 |
| 94 | + total = 0 |
| 95 | +
|
| 96 | + # Bad - too vague |
| 97 | + step "Setup": |
| 98 | + count = 0 |
| 99 | + total = 0 |
| 100 | +
|
| 101 | +**Size and Scope:** |
| 102 | + |
| 103 | +- Each step should do one thing |
| 104 | +- Keep steps focused and small |
| 105 | +- Avoid deep nesting |
| 106 | + |
| 107 | +.. code-block:: python |
| 108 | +
|
| 109 | + # Good |
| 110 | + step "Calculate Total": |
| 111 | + total = score1 + score2 + score3 |
| 112 | +
|
| 113 | + step "Calculate Average": |
| 114 | + average = total / 3 |
| 115 | +
|
| 116 | + # Bad - too many responsibilities |
| 117 | + step "Do Calculations": |
| 118 | + total = score1 + score2 + score3 |
| 119 | + average = total / 3 |
| 120 | + decide: |
| 121 | + if average >= 90 then: |
| 122 | + grade = "A" |
| 123 | + else: |
| 124 | + grade = "B" |
| 125 | +
|
| 126 | +Subtasks |
| 127 | +^^^^^^^^ |
| 128 | + |
| 129 | +Subtasks are reusable computations that return values. They act like functions within your tasks. |
| 130 | + |
| 131 | +**When to Use Subtasks:** |
| 132 | + |
| 133 | +- For reusable computations |
| 134 | +- When code needs to return a value |
| 135 | +- For complex operations that can be broken down |
| 136 | + |
| 137 | +**Naming and Usage:** |
| 138 | + |
| 139 | +.. code-block:: python |
| 140 | +
|
| 141 | + # Good - Clear purpose and return value |
| 142 | + subtask "Calculate Average": |
| 143 | + total = sum(scores) |
| 144 | + return total / len(scores) |
| 145 | +
|
| 146 | + step "Process Results": |
| 147 | + avg = Calculate_Average() |
| 148 | + print("Average:", avg) |
| 149 | +
|
| 150 | + # Bad - Unclear purpose |
| 151 | + subtask "Process": |
| 152 | + return total / count |
| 153 | +
|
| 154 | +Code Layout |
| 155 | +----------- |
| 156 | + |
| 157 | +Indentation |
| 158 | +^^^^^^^^^^^ |
| 159 | + |
| 160 | +- Use consistent indentation |
| 161 | +- Each level should be clearly visible |
| 162 | +- Align related code blocks |
| 163 | + |
| 164 | +.. code-block:: python |
| 165 | +
|
| 166 | + # Good |
| 167 | + task "Process Data": |
| 168 | + step "Initialize": |
| 169 | + count = 0 |
| 170 | + total = 0 |
| 171 | +
|
| 172 | + # Bad - inconsistent indentation |
| 173 | + task "Process Data": |
| 174 | + step "Initialize": |
| 175 | + count = 0 |
| 176 | + total = 0 |
| 177 | +
|
| 178 | +Whitespace |
| 179 | +^^^^^^^^^^ |
| 180 | + |
| 181 | +**Around Operators:** |
| 182 | + |
| 183 | +.. code-block:: python |
| 184 | +
|
| 185 | + # Good |
| 186 | + average = total / count |
| 187 | + name = first + " " + last |
| 188 | +
|
| 189 | + # Bad |
| 190 | + average=total/count |
| 191 | + name=first+" "+last |
| 192 | +
|
| 193 | +**Block Structure:** |
| 194 | + |
| 195 | +.. code-block:: python |
| 196 | +
|
| 197 | + # Good - Clear separation between blocks |
| 198 | + task "First Task": |
| 199 | + step "Do Something": |
| 200 | + x = 1 |
| 201 | +
|
| 202 | +
|
| 203 | + task "Second Task": |
| 204 | + step "Do Another": |
| 205 | + y = 2 |
| 206 | +
|
| 207 | +Control Flow |
| 208 | +------------ |
| 209 | + |
| 210 | +Decide Statements |
| 211 | +^^^^^^^^^^^^^^^^^ |
| 212 | + |
| 213 | +.. code-block:: python |
| 214 | +
|
| 215 | + # Good |
| 216 | + decide: |
| 217 | + if score >= 90 then: |
| 218 | + grade = "A" |
| 219 | + elif score >= 80 then: |
| 220 | + grade = "B" |
| 221 | + else: |
| 222 | + grade = "C" |
| 223 | +
|
| 224 | +Loops |
| 225 | +^^^^^ |
| 226 | + |
| 227 | +.. code-block:: python |
| 228 | +
|
| 229 | + # Good |
| 230 | + for item in items: |
| 231 | + total = total + item |
| 232 | + end |
| 233 | +
|
| 234 | + # For enumeration |
| 235 | + for index, value in enumerate(items): |
| 236 | + print(index, value) |
| 237 | + end |
| 238 | +
|
| 239 | + # Range-based loop |
| 240 | + for i in range(5): |
| 241 | + print(i) |
| 242 | + end |
| 243 | +
|
| 244 | +Program Organization |
| 245 | +-------------------- |
| 246 | + |
| 247 | +Run Statements |
| 248 | +^^^^^^^^^^^^^^ |
| 249 | + |
| 250 | +Run statements should be organized logically and grouped by related functionality: |
| 251 | + |
| 252 | +.. code-block:: python |
| 253 | +
|
| 254 | + # Good - Clear execution flow |
| 255 | + run "Load Data" |
| 256 | + run "Validate Input" |
| 257 | + run "Calculate Results" |
| 258 | + run "Generate Report" |
| 259 | +
|
| 260 | +Best Practices |
| 261 | +-------------- |
| 262 | + |
| 263 | +1. **Code Reuse** |
| 264 | + - Extract common logic into subtasks |
| 265 | + - Avoid duplicating code |
| 266 | + - Keep functions focused |
| 267 | + |
| 268 | +2. **Maintainability** |
| 269 | + - Write self-documenting code through clear naming |
| 270 | + - Use consistent naming conventions |
| 271 | + - Follow logical organization |
| 272 | + |
| 273 | +3. **Readability** |
| 274 | + - Prioritize clarity over cleverness |
| 275 | + - Maintain consistent style |
| 276 | + - Use meaningful names |
| 277 | + |
| 278 | +Remember |
| 279 | +-------- |
| 280 | + |
| 281 | +The primary goal of these guidelines is to make Think code more readable, maintainable, and understandable. When in doubt, choose clarity over convenience. |
0 commit comments