Skip to content
Sahil edited this page Oct 8, 2019 · 6 revisions

Important

Please follow the following style guides for the 3 main languages:

Java - Google's Java Style Guide

C++ - Google's C++ Style Guide

Python - Google's Python Style Guide

Program Structure

  • Every program file should have a short description of the program in comments, what is does and how it works.
  • Optionally, the author's name and email-id can be added at the start of the program using comments.
  • The code should be properly indented, with proper spacing, and naming convention should be followed. Further details about all these are provided in below sections.
  • The code should have proper comments wherever necessary.
  • At the end of program, elaborate upon the time complexities involved and optionally a little about the space complexity of your code. It should be easily graspable.
// Name: Abc Xyz
// Email-id: [email protected]

// This program finds the maximum element of an array

int findMax(int array[], int size){
    // let the first element be the maximum
    int max = array[0];
    for(int i = 1; i < size; i ++){
        // if current element of array is larger than max, update max
        if(array[i] > max){
            max = array[i];
        }
    }
    return max;
}

...

// Time Complexity: O(n), Since we are iterating over each element of the array starting from index 1 to index n - 1, 
// it is just a linear traversal of the array. 
// Inside the for loop, there is just a condition check and updating the variable max if the condition returns true,
// thus these are just constant time operations. Overall, time taken is O(n) thus.

// Space Complexity: O(1), Since we have just used an additional variable max inside the function.

Indentation

Please don't mix different indentation style in a program and only use the following scheme:

4 Spaces/1 Tab indent

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 i = 0; i < 10; i ++){
    if(some_condition){
        // your code goes here
    }
}

Comments

  • 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;
    }
}

Naming Convention

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(...){
    // ...
}

Spaces

  • 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;

// taking multiple variables as input using single cin statement
cin >> x >> y >> z;

// printing these variable using single cout statement
cout << x << " " << y << " " << z;
  • For some lengthy mathematical expressions, always use proper spaces. e.g:
int discriminant;
discriminant = sqrt((b * b) - (4 * a * c)); 

Keep lines of a reasonable length

  • Don't try to write 2 - 3 lines of code in one line. This decreases readability.
  • Don't make lines of code too lengthy when using many conditions inside an if statement. e.g:
// Bad Idea
// hard to follow
if (x && y && myFunctionThatReturnsBool() && caseNumber3 && (15 > 12 || 2 < 3)) { 
}

// Good Idea
// Logical grouping, easier to read
if (x && y && myFunctionThatReturnsBool() 
    && caseNumber3 
    && (15 > 12 || 2 < 3)) { 
}

Don'ts

  • Don't use #include<bits/stdc++.h> in C++ codes. It increase compile time and does not support all compilers.
  • Don't commit any binary file. You can use .gitignore file that handles it automatically.
  • Don't send pull request from your master branch. Follow Github-flow Otherwise your pull request will be rejected.