Code Standards - Falicer/Project_Tech_2021 GitHub Wiki

Code practices

What are the coding rules?

Coding rules ensure that your software is:

  • Safe: It can be used without causing harm.
  • Secure: It canโ€™t be hacked.
  • Reliable: It functions as it should, every time.
  • Testable: It can be tested at the code level.
  • Maintainable: It can be maintained, even as your codebase grows.
  • Portable: It works the same in every environment.

And also:

  • A coding standard gives a uniform appearance to all the code written by different engineers.
  • It improves readability and reduces complexity.
  • It helps in code reuse and helps to detect errors easily.
  • It promotes sound programming practices and increases the efficiency of programmers.

Effective coding standards

Limited use of global variables

When a variable is declared global, there is a very high chance that someone else overwrites its value unknowingly.

Follow naming conventions

  • Meaningful and understandable variable names help everyone to understand the reason for using it.
  • Local variables should be named using camel case lettering starting with lowercase letters (e.g. localData) whereas global variables names should start with a capital letter (e.g. GlobalData). Constant names should be formed using all capital letters (e.g. CONSDATA).
  • It is best to avoid the use of digits in variable names.
  • The names of the function should be written in camel case starting with small letters.
  • The name of the function must describe the reason for using the function clearly and briefly.

Indentation

Proper indentation is necessary to maintain code readability. Here are some spacing conventions:

  • There must be a space after a comma between two function arguments.
  • Each nested block should be properly indented and spaced.
  • Proper indention should be at the beginning and at the end of each block in the program.
  • All braces should start on a new line, and the code following the end of braces should also start on a new line.

Do not write paragraph as functions

Lengthy functions are quite difficult to understand so break the longer functions up in smaller functions so it's easier to understand and looks less complex. Also a benefit is it helps in managing bugs ;)

Code should be well documented

Write code that basically speaks for itself. Write comments where it's needed to understand a certain part.

Follow language specific conventions

Each language has their own specific conventions so be wary of those, these are some examples for Java:

  • Whenever your override equals() method, you must also override the hashCode() method.
  • Do not compare strings using == or !=.
  • Do not ignore exceptions that you caught.
  • Do not catch broad exception classes like Exception or RuntimeException.

Sources

maansoftwares. (2018, August 6). Coding Standards The Way to Maintainable Code. Retrieved May 23, 2021, from https://www.maansoftwares.com/coding-standards-the-way-to-maintainable-code

Rezvi, M. (2020, January 7). Write Better Code with Coding Standards. Retrieved May 23, 2021, from https://levelup.gitconnected.com/write-better-code-with-coding-standards-546faf3fd4d1

Perforce. (2018, September 21). Intro to Coding Standards โ€” Coding Rules and Guidelines | Perforce. Retrieved May 23, 2021, from https://www.perforce.com/resources/qac/coding-standards