Article Writing Style
This page is no longer maintained. Please see Guidelines to Write an Article
Coding style
This is the source of many of the following coding standards.
1) Indentation should be done using 4 spaces.
2) Maximum 60 characters in a line so that the program is readable on mobile devices without much horizontal scrolling.
// Below style should be avoided int fun(int a, int sumSoFar, int currSum, char val, int *result) // The above should be written as int fun(int a, int sumSoFar, int currSum, char val, int *result)
// Below style should be avoided cout << "Sample code to understand coding style for more readability of millions of readers" << val; // The above should be written as cout << "Sample code to understand coding style for" << " more readability of millions of readers" << val;
3) In case code is written in multiple languages like Python, Java and C/C++, the output of all codes should be same.
4) Avoid use of scanf (or cin) statements.
5) Spaces in while, if, else, for
// There should be one space after while, no other // spaces while (i < 0) if (x < y) { }
6) There should not be any spaces for function call or function declaration
// No spaces after "reverse" or after "(" void reverse(char* str, int low, int high) { while (low < high) { swap(&str[low], &str[high]); ++low; --high; } } // Driver program to test above function int main() { char str[] = "geeksforgeeks"; reverse(str); return 0; }
7) Avoid the use of typdef.
8) Function names should be of the form “maxOfTwo()”, variable names should be of the form “max_of_two” or same as function name style. Class/Struct names should be of the form “ComplexNumber” or “SuffixTreeNode”. Macro names should be in capital letters like MAX_SIZE.
9) Avoid use of static and global variables.
10) When we use cout, we must use a space between cout and “<<” and space between two “<<“. For example:
cout << "Sample" << "Example"
11) There should be space after comma in declaration list and parameter passing.
int x, y, z;
fun(x, y, z);
12) There should be spaces in assignment operators
// Should be avoided int x, y=0; // Should be followed int x, y = 0; // Should be avoided x+=10; // Should be followed x += 10;
Linked List
1) For writing linked list posts, use the standard functions that have been used in all linked list posts on GfG. There may be exceptions when linked List is customized for a particular problem.
/* A utility function to insert a node at the beginning of linked list */ void push(struct node** head_ref, int new_data) { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the data */ new_node->data = new_data; /* link the old list off the new node */ new_node->next = (*head_ref); /* move the head to point to the new node */ (*head_ref) = new_node; } /* A utility function to print linked list */ void printList(struct node *node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } }
2) In main(), create a hard coded linked list instead of using scanf or cin. For example, following list.
// Function to create newNode in a linkedlist Node* newNode(int key) { Node *temp = new Node; temp->data = key; temp->next = NULL; return temp; } int main() { Node *head = newNode(1); head->next = newNode(2); head->next->next = newNode(3); head->next->next->next = newNode(4); head->next->next->next->next = newNode(5); }
Tree
1) For writing tree posts, use the standard structure and functions that have been used in most of the tree programs on GfG. There may be exceptions when tree is customized for a particular problem.
// A Tree node struct Node { int key; struct Node* left, *right; }; // Utility function to create a new node Node* newNode(int key) { Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return (temp); } // Driver program int main () { // Let us create Binary Tree shown in above example Node *root = newNode(1); root->left = newNode(12); root->right = newNode(13); root->right->left = newNode(14); root->right->right = newNode(15); root->right->left->left = newNode(21); root->right->left->right = newNode(22); root->right->right->left = newNode(23); root->right->right->right = newNode(24); }
2) In main(), create a hard coded tree instead of using scanf or cin. For example, following tree.
// Driver program int main () { /* 10 / \ 12 13 / \ 14 15 / \ / \ 21 22 23 24 Let us create Binary Tree shown in above example */ Node *root = newNode(1); root->left = newNode(12); root->right = newNode(13); root->right->left = newNode(14); root->right->right = newNode(15); root->right->left->left = newNode(21); root->right->left->right = newNode(22); root->right->right->left = newNode(23); root->right->right->right = newNode(24); }
Will be adding more to it.
See following for any other doubt.
http://users.ece.cmu.edu/~eno/coding/CCodingStandard.html
Other Style
1) Diagrams should use green color in text and black color in boundaries. More colors like Red, Blue can be used if needed. See this post for sample. If an image is taken from a source, the source should be specified with the image. See this article for example.
2) Formulas should be written in pre tags with characters till 60 columns so that they are readable on mobile devices also.
P = (x + y)(x - y)
3) All coding articles should have examples with sample inputs, sample outputs and explanations. See below for example on this post. The examples should also be written in pre tags and should not cross 60 characters.
Examples:
Input: n = 1 Output: 11 1 is first Fibonacci number in this representation and an extra 1 is appended at the end. Input: n = 11 Output: 001011 11 is sum of 8 and 3. The last 1 represents extra 1 that is always added. A 1 before it represents 8. The third 1 (from beginning) represents 3.