Coding Standard - raisanusrat222/Metro-Rail-Project GitHub Wiki

A coding standard makes sure that all the developers working on the project are following certain specified guidelines. The code can be easily understood and proper consistency is maintained.: The purpose of having a coding standard is to improve readability, reusability, maintainability, reduce the complexity of the code, and so on.

Naming Conventions:

Variables Name:

  • Local variable name should be in lower case and for multiple words variable should be in (snake_case). Example: name, id, ticket_no
  • Global variable should use PascalCase. Example: GlobalScope
  • Boolean type variable can start with “is/has/have”. (if it’s not necessary then you can use a general naming convention). Example : is_employee=True
  • It is better to avoid the use of digits in variable names.

Constant Name:

  • Constants are usually defined on a module level and written in all capital letters with underscores separating words to enhance readability. Examples include MAX_OVERFLOW and TOTAL.
  • Function Name: We should use lowercase words or separate words by the underscore.Example : myfunction, my_function

Method Name:

Methods should be verbs or verb phrases and use Camel casing. Example: toString(), getName().

Class Name:

Class Names should be in Pascal Casing. Example: MyPage. Note that there is a separate convention for builtin names: most builtin names are single words (or two words run together)

Package and Module Names:

Package and module names should be in lower case and short. The use of underscores is discouraged.

Exception Names:

Exceptions should be classes, the class naming convention applies here. However, you should use the suffix "Error" on your exception names (if the exception actually is an error).


Layout Convention:

Indentation:

  • Tabs should be used rather than spaces for indentation.

  • A single tab should be used for per indentation level.

    Example:

     if x>2 
    print (“it works“)
  • For line continuations, ‘Hanging indent’ should be used. This means, the final non-whitespace character of a line is used as the initial parenthesis of a parenthesized statement, with successive lines indented till the closing parenthesis.

    Example:

      x = function( 
    first_arg, second_arg,
    third_arg, fourth_arg)
  • If hanging indent is to be used for function definition, an extra indentation (2 tabs) should be used to differentiate the continuation line of arguments from the function's code.

    Example:

       def function( 
    a1, a2,
    a3, x):
    return x
  • If the conditional portion of an if-statement is large enough to need several lines, an extra indentation (2 tabs) should be added on the conditional continuation line to distinguish the code for that if statement from the condition.

    Example:

     if (a<5 and 
    b>2)
    print(b)
  • Maximum length of a line should be 79 characters.

Blank Lines:

  • Top level functions and classes should be separated by 2 blank lines
  • Method definitions inside classes should be separated by single blank line.
  • A single blank line should be used between different steps inside a function.

White Space:

  • A white space should be added around assignment, arithmetic, comparison and Boolean operators.

    Example:

     x = a + b 
  • If there are multiple operators in a statement, then white space should be added around the operator with least priority.

    Example:

      a = b**2 + c 
    x = (a+1) * (b-2)
    if a<x and x/5==0:
  • A white space should be added after keywords such as- if, else, do, while, for and foreach.

⚠️ **GitHub.com Fallback** ⚠️