-
Notifications
You must be signed in to change notification settings - Fork 9
Coding Standards
Sandip Kumar edited this page Sep 2, 2018
·
6 revisions
- The logic of code always is written in method/function and in main method input and output will be shown.
- Public method will be called from the main method and in case other methods are needed then make them private.
- The code should be properly indented, with proper spacing, and naming convention should be followed.
- The code should have proper comments wherever necessary.
public static void main(String[] args) {
int[] arr = { 10, 15, 55, 22, 11, 33 };
int data = 22;
int index = linearSearch(arr, data);
System.out.println(index);
}
public static int linearSearch(int[] arr, int data) {
return getIndexLinearSearch(arr,data);
// Time Complexity: O(n),
// Space Complexity: O(1)
}
private static int getIndexLinearSearch(int[] arr, int data){
for (int i = 0; i < arr.length; i++) {
if (arr[i] == data) {
return i;
}
}
return -1;
}
...
- You will give a proper reason why your code is better than above code.
- You will create a public method and write logic in it.
- The method name is yourNameMethodName. you can refer below example
public static void main(String[] args) {
int[] arr = { 10, 15, 55, 22, 11, 33 };
int data = 22;
int index = linearSearch(arr, data);
System.out.println(index);
}
public static int linearSearch(int[] arr, int data) {
return getIndexLinearSearch(arr,data);
// Time Complexity: O(n),
// Space Complexity: O(1)
}
public static int sandipLinearSearch(int[] arr, int data) {
....
// Your code
.....
// Time Complexity: O(log(n)),
// Space Complexity: O(1)
}
...
- You will create a public method and write logic in it.
- The method name is reviewYourNameMethodName. you can refer below example.
- You will add problem or error if you faced. It must be added in the comment section and in the first line of the method.
public static void main(String[] args) {
int[] arr = { 10, 15, 55, 22, 11, 33 };
int data = 22;
int index = linearSearch(arr, data);
System.out.println(index);
}
public static int linearSearch(int[] arr, int data) {
return getIndexLinearSearch(arr,data);
// Time Complexity: O(n),
// Space Complexity: O(1)
}
public static int reviewSandipLinearSearch(int[] arr, int data) {
// In case any problem or error you are getting
....
// Your code
.....
// Time Complexity: O(n),
// Space Complexity: O(1)
}
...
Please don't mix different indentation style in a program and only use the following scheme:
Every next level of block of code should be indented with 4 spaces or 1 tab from the previous level. In the following example, if block has one level higher indentation than the for block.
Note: Pull Requests having codes with bad indentation might be rejected.
for(int k = 0; k < 10; k++){
if(true){
// your code goes here
}
}
- Every code should have well written comments and you should use
//. Comments with/* */are not allowed. - Comments should be added above the line of code not adjacent to it. e.g:
// calculate sum of even numbers from 0 to 10.
int sum = 0;
for(int i = 0; i <= 10; i ++){
// check for even numbers and add it to *sum*
if(i % 2 == 0){
sum += i;
}
}
Variables, constants, class names, function names, etc. should have names according to convention as mentioned below:
- Try to keep the name of variables, constants, etc. as clear as possible.
- Use UPPERCASE letters for constants.
- Use UpperCamelCase convention for class naming.
- Use snake_case convention for variable naming.
- Use lowerCamelCase convention for function names.
// UPPERCASE letters
const PI = 3.1415
// UpperCamelCase
class YourClass{
// ...
};
// snake_case
int name_of_variable;
// lowerCamelCase
void findMinimum(...){
// ...
}
- Use spaces whenever needed to make the code look more clear and easily readable.
- Whenever naming variables, use spaces after commas if declaring multiple variables in same line. e.g:
// declaring three integers
int x, y, z;
- For some lengthy mathematical expressions, always use proper spaces. e.g:
int discriminant;
discriminant = sqrt((b * b) - (4 * a * c));
Follow Github-flow Otherwise your pull request will be rejected.