Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Code style guidelines

Ingvord edited this page Feb 27, 2018 · 7 revisions

General stuff

  • prefer 4 space indentation
  • prefer snake_case
  • use standard header for new files, see below

Using code styles provided in the repository

If you are a happy user of CLion you can just open the existing project cloned from this repo. Settings must be applied automatically. To double check this go to Settings (File->Settings OR ctrl+alt+s) and navigate to Editor/Code Style:

code_style_template_0.png

and make sure that highlighted values are correct i.e. Scheme = Project and Line separator = Unix

Next navigate to Editor/File and code templates:

code_style_template.png

and make sure its similar to what you see on the picture above.

To contribute a code style just change these settings and make a Pull Request. Here is a reference on how to use templates in CLion products: configuring code style

Setting up Live template in CLion

One can define live templates to speed up coding in CLion (as well as any IntelliJ based product). Here is a small guide on how to add Copyright live template:

Bring up Settings dialog (ctrl+alt+s) and navigate to Editor/Live templates:

clion_live_templates

In the right panel do the following:

  1. Expand C/C++ and a new template (alt+insert)
  2. Define Abbreviation and Description, in our example above it is copy and copyright
  3. Define desired template following this guide
  4. Define variables' values, existing variables are described in this guide
  5. Define scope (in our case any place in C/C++)

Now open any C++ header/source file type copy and press Tab, copy will be substituted with the template:

clion_live_templates_1

CLion will ask to redefine variables' values if default are good just proceed pressing Enter.

Clean code guidelines:

  • standard naming patterns (for-loop): for(size_t i = 0, size = ...; i < size; ++i)
  • Avoid:
    • nested ifs,
    • huge switches,
    • incomplete variable names;
  • each method does exactly one thing;
  • extract general purpose classes;
  • execution pattern: create than execute;
  • each class in its own header+source (export as few as needed by the user)
  • Eliminate dead code
  • alway# Clean code guidelines:
  • standard naming patterns (for-loop): for(size_t i = 0, size = ...; i < size; ++i)
  • Avoid:
    • nested ifs,
    • huge switches,
    • incomplete variable names;
  • each method does exactly one thing;
  • extract general purpose classes;
  • execution pattern: create than execute;
  • each class in its own header+source (export as few as needed by the user)
  • Eliminate dead code
  • always true/false;
  • fixed value params
  • Etc
  • Use integral types to calculate time

5 minutes inspiring video

5 minutes inspiring video