Skip to content

Instantly share code, notes, and snippets.

@tarasowski
Last active November 21, 2024 14:41
Show Gist options
  • Save tarasowski/de0f209e6113a0bd49bdfd94cb419366 to your computer and use it in GitHub Desktop.
Save tarasowski/de0f209e6113a0bd49bdfd94cb419366 to your computer and use it in GitHub Desktop.
Style guides
# Principles of Clean Code for Techstarter Projects
0 Consistent Naming Conventions
All function names and variables should be written in English. In Python, use snake_case for naming, while in JavaScript, use camelCase.
1 Consistent Indentation (2 or 4 spaces)
Use 4 spaces for indentation in Python and either 2 or 4 spaces for JavaScript. Pick one and stick with it.
2 Use Meaningful Variable and Function Names
Choose descriptive names for variables, functions, and classes. For example, use "userAge" instead of "x" or "a".
3 Limit Line Length (80 or 100 characters)
Keep your lines to a maximum of 80-100 characters to maintain readability.
4 Spaces Around Operators
Add spaces around assignment ("="), comparison ("==", "!="), and arithmetic ("+", "-", "*") operators.
Example: x = 10 + 5 not x=10+5.
5 Consistent Use of Semicolons
Always use semicolons at the end of statements in JavaScript. In Python, semicolons are not needed.
6 Single vs Double Quotes
Choose either single quotes ('string') or double quotes ("string") and be consistent. In Python, single quotes are common, but both are acceptable. In JavaScript, single quotes are often used.
7 Avoid Magic Numbers (Use Constants)
Instead of hardcoding values like 3.14 or 1000, define them as constants with descriptive names.
8 Comment Code Where Necessary
Write comments to explain complex logic, but avoid over-commenting. Keep the code as self-explanatory as possible.
9 Avoid Lambda Functions and Functional Programming Style
Stick to using traditional function definitions with def in Python and standard function expressions or declarations in JavaScript. Avoid using lambda functions or features like map, filter, or reduce that are common in functional programming. Instead, prefer simple loops and explicit function calls to make your code easier to read and understand. This approach is more beginner-friendly and aligns with a more imperative programming style.
10 Limit Function Length
Keep functions short and focused on a single task. A good rule of thumb is to limit functions to around 20 lines of code. If a function starts to get too long, consider breaking it up into smaller, more manageable helper functions. This makes the code easier to maintain and debug, and helps students understand the importance of writing clear, modular code.
# Tools for Code Quality
- Pylint: definition of custom rules (.pylintrc) < github repo bootstrap project - pylint main.py
- ESLint: definition of custom rules (.eslintrc) < github repo bootstrap project - npx eslint main.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment