|
58 | 58 | "- use consistent verbs for function names, don't use `get_score()` and `grab_results()` (instead use `get` for both)\n", |
59 | 59 | "- variable names should be snake_case and all lowercase, eg `first_name`\n", |
60 | 60 | " - class names should be CamelCase, eg `MyClass`\n", |
61 | | - "function names should be snake_case and all lowercase, eg `quick_sort()`\n", |
| 61 | + " - function names should be snake_case and all lowercase, eg `quick_sort()`\n", |
62 | 62 | " - constants should be snake_case and all uppercase, eg `PI = 3.14159`\n", |
63 | 63 | " - modules should have short, snake_case names and all lowercase, eg `pandas`\n", |
64 | 64 | " - single quotes and double quotes are equivalent so pick one and be consistent—most automatic formatters prefer `\"`" |
|
142 | 142 | " return result\n", |
143 | 143 | "```\n", |
144 | 144 | "\n", |
| 145 | + "When using *method chaining* (something you can see in action in [](data-transform)) it's necessary to put the chain inside parentheses and it's good practice to use a new line for every method. The code snippet below gives an example of what good looks like:" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": null, |
| 151 | + "id": "f0f5bb37", |
| 152 | + "metadata": {}, |
| 153 | + "outputs": [], |
| 154 | + "source": [ |
| 155 | + "import pandas as pd\n", |
| 156 | + "\n", |
| 157 | + "df = pd.DataFrame(\n", |
| 158 | + " data={\n", |
| 159 | + " \"col0\": [0, 0, 0, 0],\n", |
| 160 | + " \"col1\": [0, 0, 0, 0],\n", |
| 161 | + " \"col2\": [0, 0, 0, 0],\n", |
| 162 | + " \"col3\": [\"a\", \"b\", \"b\", \"a\"],\n", |
| 163 | + " \"col4\": [\"alpha\", \"gamma\", \"gamma\", \"gamma\"],\n", |
| 164 | + " },\n", |
| 165 | + " index=[\"row\" + str(i) for i in range(4)],\n", |
| 166 | + ")\n", |
| 167 | + "\n", |
| 168 | + "\n", |
| 169 | + "# Chaining inside parentheses works\n", |
| 170 | + "\n", |
| 171 | + "results = df.groupby([\"col3\", \"col4\"]).agg({\"col1\": \"count\", \"col2\": \"mean\"})\n", |
| 172 | + "\n", |
| 173 | + "results" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "markdown", |
| 178 | + "id": "1d6f3bf8", |
| 179 | + "metadata": {}, |
| 180 | + "source": [ |
| 181 | + "And this is what *not* to do:\n", |
| 182 | + "\n", |
| 183 | + "```python\n", |
| 184 | + "results = df\n", |
| 185 | + " .groupby([\"col3\", \"col4\"]).agg({\"col1\": \"count\", \"col2\": \"mean\"})\n", |
| 186 | + "\n", |
| 187 | + "```" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "id": "d016d530", |
| 193 | + "metadata": {}, |
| 194 | + "source": [ |
145 | 195 | "## Principles of Clean Code\n", |
146 | 196 | "\n", |
147 | 197 | "While automation can help apply style, it can't help you write *clean code*. Clean code is a set of rules and principles that helps to keep your code readable, maintainable, and extendable. Writing code is easy; writing clean code is hard! However, if you follow these principles, you won't go far wong.\n", |
|
171 | 221 | "\n", |
172 | 222 | "Relatedly, do not have a single function that tries to do everything. Functions should have limits too; they should do approximately one thing. If you're naming a function and you have to use 'and' in the name then it's probably worth splitting it into two functions.\n", |
173 | 223 | "\n", |
174 | | - "Functions should have no 'side effects' either; that is, they shouldn't only take in value(s), and output value(s) via a return statement. They shouldn't modify global variables or make other changes.\n", |
| 224 | + "Functions should have no 'side effects' either; that is, they should only take in value(s), and output value(s) via a return statement. They shouldn't modify global variables or make other changes.\n", |
175 | 225 | "\n", |
176 | 226 | "Another good rule of thumb is that each function shouldn't have lots of separate arguments.\n", |
177 | 227 | "\n", |
|
220 | 270 | "name": "python", |
221 | 271 | "nbconvert_exporter": "python", |
222 | 272 | "pygments_lexer": "ipython3", |
223 | | - "version": "3.10.12" |
| 273 | + "version": "3.10.13" |
224 | 274 | }, |
225 | 275 | "toc-showtags": true |
226 | 276 | }, |
|
0 commit comments