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.
ExampleWhat 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.