Style and Naming Conventions
- When writing code, it's not just about making it work—it's about making it readable, maintainable, and understandable for others (and your future self).
- Style refers to the way code is formatted and organised, including indentation, spacing, and comments.
Naming conventions
Rules for naming variables, functions, and other identifiers in a consistent and meaningful way.
Why Style and Naming Conventions Matter
- Readability: Code should be easy to read and understand, even for someone who didn't write it.
- Maintainability: Well-structured code is easier to modify and debug.
- Collaboration: Consistent style and naming conventions help teams work together efficiently.
- Debugging: Clear code reduces the time spent finding and fixing errors.
- Think of code style and naming conventions like grammar and punctuation in writing.
- They don't change the meaning, but they make the text easier to read and understand.
Key Elements of Good Style and Naming Conventions
Indentation
Proper indentation makes the code's structure clear.
What would you prefer:
public class Main{public static void main(String[] args){
int n = 5;int res = 1;
for (int i = 2; i <= n; i++){res = res * i;}
System.out.println(n + "! = " + res);}}
or
public class Main{
public static void main(String[] args){
int n = 5;
int res = 1;
for (int i = 2; i <= n; i++){
res = res * i;
}
System.out.println(n + "! = " + res);
}
}
- Notice how the code inside the function and loop is indented.
- This shows the hierarchy and flow of the program.
In programming languages like Java and C++, indentation is not a matter of concern, but in Python, clear indentation is mandatory, as it will result in errors otherwise.
Spacing
- Use spaces to separate operators and improve readability.
- Avoid unnecessary spaces that clutter the code.
- Good spacing:
total = a + b- Bad spacing:
total=a+bComments
- Comments explain the purpose of code segments.
- They should be concise and relevant.
# Calculate the total sum of a list
def calculate_sum(numbers):
total = 0
for num in numbers:
total += num
return total Meaningful identifiers
- Use descriptive names that reflect the purpose of the variable or function.
- Avoid vague names like x or temp.
- Good naming:
total_sum = 0
user_age = 25- Bad naming:
x = 0
a = 25 - Stick to a consistent naming convention throughout the code.
- The most common naming conventions include:
| Naming convention | Example | Usually used for |
|---|---|---|
| Camel Case | camelCase | Java variables and methods |
| Pascal Case | PascalCase | Java classes |
| Snake Case | snake_case | Python style standard Pep8 |
| Kebab Case | kebab-case | CSS classes and HTML attributes |
Avoid reserved words
- Don't use language keywords or reserved words as identifiers.
- This can cause errors and confusion.
Bad naming:
def print():
pass This overrides the built-in print function in Python.
- Why are style and naming conventions important in programming?
- Give an example of a meaningful identifier and explain why it's better than a vague one.
- How does proper indentation improve code readability?