Java Code Style Guide for DSA Examples
Java Code Style Guide
This guide explains how to write clean and beginner-friendly Java code for DSA examples.
The main goal is to keep Java solutions simple, readable, and easy to understand for students and new contributors.
Why This Guide Is Needed
DSA examples are easier to learn when the code follows one clean style.
This guide helps contributors with:
- Clear class and method names
- Meaningful variable names
- Proper formatting
- Useful comments
- Time and space complexity
- Input and output examples
- Edge case handling
1. Class Naming
Use PascalCase for class names.
Class names should clearly describe the algorithm or data structure.
Good
public class BinarySearch {
}
public class MergeSort {
}
Avoid
public class binarysearch {
}
public class mergesortexample {
}
2. Method Naming
Use camelCase for method names.
Method names should describe what the method does.
Good
public static int binarySearch(int[] arr, int target) {
return -1;
}
public static void mergeSort(int[] arr, int left, int right) {
}
Avoid
public static int Binary_Search(int[] arr, int target) {
return -1;
}
public static void sort1(int[] arr) {
}
3. Variable Naming
Use meaningful variable names.
For DSA examples, short names like i, j, and n are fine in loops, but important values should have clear names.
Good
int left = 0;
int right = arr.length - 1;
int mid = left + (right - left) / 2;
Avoid
int a = 0;
int b = arr.length - 1;
int c = (a + b) / 2;
Common DSA Variable Names
| Variable | Meaning |
|---|---|
left | Left pointer or left boundary |
right | Right pointer or right boundary |
mid | Middle index |
start | Starting index |
end | Ending index |
target | Value to search |
result | Final answer |
count | Number of occurrences |
sum | Sum of values |
4. Formatting and Indentation
Use consistent indentation.
Recommended indentation:
4 spaces
Good
if (arr[mid] == target) {
return mid;
}
Avoid
if(arr[mid]==target){
return mid;
}
Clean formatting makes the code easier to read and review.
5. Keep Code Simple
For beginner DSA examples, avoid unnecessary advanced syntax.
The code should be easy for students to follow.
Good
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Avoid for beginner examples
Arrays.stream(arr).forEach(System.out::println);
Advanced syntax is useful, but beginner examples should focus on clarity first.
6. Comments
Use comments only when they help the reader understand the logic.
Do not add comments for every single line.
Good
// Move the left boundary because the target is greater than the middle value
left = mid + 1;
Avoid
// Increase left
left = mid + 1;
A comment should explain why something is happening, not just repeat the code.
7. Input and Output Examples
Whenever possible, add input and output examples.
This helps learners understand how the algorithm works.
Example
Input:
Array = [1, 3, 5, 7, 9]
Target = 7
Output:
3
Explanation:
The target value 7 is present at index 3.
8. Time and Space Complexity
Every DSA solution should mention time and space complexity.
Use this format:
Time Complexity: O(log n)
Space Complexity: O(1)
Example
For binary search:
Time Complexity: O(log n)
Space Complexity: O(1)
Reason:
- Binary search divides the search space by 2 each time.
- It uses only a few extra variables.
9. Edge Cases
Always mention important edge cases.
Common edge cases in Java DSA examples:
- Empty array
- Single element array
- Duplicate values
- Negative numbers
- Large input size
- Target not found
- Null input, if applicable
Example
if (arr == null || arr.length == 0) {
return -1;
}
This prevents errors when the input array is empty or not available.