.TH "doc_CODING_md" 3elektra "Tue Aug 6 2019" "Version 0.9.0" "Elektra" \" -*- nroff -*-
.ad l
.nh
.SH NAME
doc_CODING_mdCODING
\- intensively (except for file headers)\&.
.PP
This document provides an introduction in how the source code of libelektra is organized and how and where to add functionality\&.
.PP
.SS "Folder structure"
.PP
After you downloaded and unpacked Elektra you should see some folders\&. The most important are:
.PP
.IP "\(bu" 2
\fBsrc:\fP This directory contains the source of the libraries, tools and plugins\&.
.IP "\(bu" 2
\fBdoc:\fP General documentation for the project and the core library\&.
.IP "\(bu" 2
\fBexamples:\fP Examples on how to use the core library\&.
.IP "\(bu" 2
\fBtests:\fP Contains the testing framework for the source (\fBsrc\fP)\&.
.PP
.PP
More about the structure you find in the folder's \fCREADME\&.md\fP\&.
.PP
.SS "General Information"
.PP
.IP "\(bu" 2
Keep to the style that is already present, except if this document says otherwise: then please open an issue for offending code\&.
.IP "\(bu" 2
We use Unix line endings
.IP "\(bu" 2
Large files should be in http://blobs.libelektra.org/ or https://rawdata.libelektra.org
.IP "\(bu" 2
`find \&. -not -regex '[/\&.a-zA-Z0-9_-]*'` should not find your file names
.PP
.PP
.SS "Source Code"
.PP
The same guidelines apply to all code in the repository including the plugins\&.
.PP
.SS "General Guidelines"
.PP
You are only allowed to break a guideline if there is a good reason to do so\&. When you do, document the fact as comment next to the code\&.
.PP
Of course, all rules of good software engineering apply: Use meaningful names and keep the software both testable and reusable\&.
.PP
The purpose of the guidelines is to have a consistent style, not to teach programming\&.
.PP
If you see code that breaks guidelines do not hesitate to fix them or at least open issues\&.
.PP
If you see inconsistency within the rules do not hesitate to open an issue about it\&.
.PP
.SS "Code Comments"
.PP
Code is not only for the computer, but it should be readable for humans, too\&. Up-to-date code comments are essential to make code understandable for others\&. Thus please use following techniques (in order of preference):
.PP
.IP "1." 4
Comment functions with \fC/**/\fP and Doxygen, see below\&.
.IP "2." 4
You should also add assertions to state what should be true at a specific position in the code\&. Their syntax is checked and they are automatically verified at run-time\&. So they are not only useful for people reading the code but also for tools\&. Assertions in Elektra are used by:
.PP
\fC#include <\fBkdbassert\&.h\fP>\fP
.PP
\fCELEKTRA_ASSERT (condition, 'formatted text to be printed when assert fails', \&.\&.\&.)\fP
.PP
Note: Do not use assert for user-APIs, always handle arguments of user-APIs like untrusted input\&.
.IP "3." 4
If the 'comment' might be useful to be printed during execution, use logging:
.PP
\fC#include <\fBkdblogger\&.h\fP>\fP
.PP
\fCELEKTRA_LOG ('formatted text to be printed according to log filters', \&.\&.\&.)\fP
.PP
Read \fBHERE\fP for how to enable the logger\&.
.IP "4." 4
Otherwise comment within source with \fC//\fP or with \fC/**/\fP for multi-line comments\&.
.PP
.PP
.SS "Coding Style"
.PP
.IP "\(bu" 2
Limits
.IP " \(bu" 4
Functions should not exceed 100 lines\&.
.IP " \(bu" 4
Files should not exceed 1000 lines\&.
.IP " \(bu" 4
A line should not be longer than 140 characters\&.
.PP
.PP
Split up when those limits are reached\&. Rationale: Readability with split windows\&.
.IP "\(bu" 2
Indentation
.IP " \(bu" 4
Use tabs for indentation\&.
.IP " \(bu" 4
One tab equals 8 spaces\&.
.PP
.IP "\(bu" 2
Blocks
.IP " \(bu" 4
Use blocks even for single line statements\&.
.IP " \(bu" 4
Curly braces go on a line on their own on the previous indentation level\&.
.IP " \(bu" 4
Avoid multiple variable declarations at one place\&.
.IP " \(bu" 4
Declare Variables as late as possible, preferable within blocks\&.
.PP
.IP "\(bu" 2
Naming
.IP " \(bu" 4
Use camelCase for functions and variables\&.
.IP " \(bu" 4
Start types with upper-case, everything else with lower-case\&.
.IP " \(bu" 4
Prefix names with \fCelektra\fP for internal usage\&. External API either starts with \fCks\fP, \fCkey\fP or \fCkdb\fP\&.
.PP
.IP "\(bu" 2
Whitespaces
.IP " \(bu" 4
Use space before and after equal when assigning a value\&.
.IP " \(bu" 4
Use space before round parenthesis ( \fC(\fP )\&.
.IP " \(bu" 4
Use space before and after \fC*\fP from Pointers\&.
.IP " \(bu" 4
Use space after \fC,\fP of every function argument\&.
.PP
.PP
.PP
The \fBreformat script\fP can ensure most code style rules, but it is obviously not capable of ensuring everything (e\&.g\&. naming conventions)\&. So do not give this responsibility out of hands entirely\&.
.PP
.SS "C Guidelines"
.PP
.IP "\(bu" 2
The compiler shall not emit any warning (or error)\&.
.IP "\(bu" 2
Use goto only for error situations\&.
.IP "\(bu" 2
Use \fCconst\fP as much as possible\&.
.IP "\(bu" 2
Use \fCstatic\fP methods if they should not be externally visible\&.
.IP "\(bu" 2
C-Files have extension \fC\&.c\fP, Header files \fC\&.h\fP\&.
.IP "\(bu" 2
Use internal functions: prefer to use \fCelektraMalloc\fP, \fCelektraFree\fP\&.
.PP
.PP
\fBExample:\fP \fCsrc/libs/elektra/kdb\&.c\fP
.PP
.SS "Clang Format"
.PP
To guarantee consistent formatting we use \fC\fCclang-format\fP\fP (version \fC6\&.0\fP or version \fC7\&.0\fP) to format all C and C++ code in the repository\&. Since our build servers also check the style for every pull request you might want to make sure you reformat your C/C++ code changes with this tool\&.
.PP
To find out which version of \fCclang-format\fP a certain build server uses please check:
.PP
.IP "\(bu" 2
the \fBDebian sid Docker image\fP,
.IP "\(bu" 2
the \fBTravis configuration file \fP, and
.IP "\(bu" 2
the \fBCirrus configuration file\fP
.PP
.PP
and search for the relevant packages (\fCclang-format\fP, \fCllvm\fP)\&. Currently we use
.PP
.IP "\(bu" 2
clang-format \fC6\&.0\fP in the \fBDebian sid image\fP on the Jenkins build server,
.IP "\(bu" 2
clang-format \fC7\&.0\fP in the \fBTravis configuration file \fP, and
.IP "\(bu" 2
clang-format \fC7\&.0\fP in the \fBCirrus macOS\fP build jobs
.PP
.PP
.SS "Installation"
.PP
.SS "macOS"
.PP
On macOS you can install \fCclang-format\fP using \fCHomebrew\fP either directly:
.PP
.PP
.nf
brew install clang-format
.fi
.PP
.PP
or by installing the whole \fCLLVM\fP infrastructure:
.PP
.PP
.nf
brew install llvm
.fi
.PP
.PP
\&. Please note, that both of these commands will install current versions of \fCclang-format\fP that might format code a little bit differently than Clang-Format \fC6\&.0\fP in certain edge cases\&. If you want you can also install Clang-Format \fC7\&.0\fP using LLVM \fC7\&.0\fP:
.PP
.PP
.nf
brew install llvm@7
.fi
.PP
.PP
.SS "Debian"
.PP
In Debian the package for Clang-Format \fC6\&.0\fP is called \fCclang-format-6\&.0\fP:
.PP
.PP
.nf
apt-get install clang-format-6\&.0
.fi
.PP
.PP
.SS "Usage"
.PP
For the basic use cases you can use \fCclang-format\fP directly\&. To do that, just call the tool using the option \fC-i\fP and specify the name of the files you want to reformat\&. For example, if you want to reformat the file \fC\fBsrc/bindings/cpp/include/kdb\&.hpp\fP\fP you can use the following command:
.PP
.PP
.nf
# On some systems such as Debian the `cmake-format` executable also contains
# the version number\&. For those systems, please replace `clang-format`,
# with `clang-format-6\&.0` or `clang-format-7` in the command below\&.
clang-format -i src/bindings/cpp/include/kdb\&.hpp
.fi
.PP
.PP
\&. While this works fine, if you want to format only a small number of file, formatting multiple files can be quite tedious\&. For that purpose you can use the script \fB`reformat-source`\fP that reformats all C and C++ code in Elektra’s code base
.PP
.PP
.nf
scripts/reformat-source # This script will probably take some seconds to execute
.fi
.PP
.PP
.SS "Tool Integration"
.PP
If you work on Elektra’s code base regularly you might want to integrate the formatting step directly in your development setup\&. \fCClangFormat’s homepage\fP includes a list of integrations for various tools that should help you to do that\&. Even if this webpage does not list any integrations for your favorite editor or IDE, you can usually add support for external tools such as \fCclang-format\fP to advanced editors or IDEs pretty easily\&.
.PP
.SS "TextMate"
.PP
While \fCTextMate\fP supports \fCclang-format\fP for the current file directly via the keyboard shortcut \fCctrl\fP+\fC⇧\fP+\fCH\fP, the editor does not automatically reformat the file on save\&. To do that
.PP
.IP "1." 4
open the bundle editor (“Bundles” → “Edit Bundles”),
.IP "2." 4
navigate to the “Reformat Code” item of the C bundle (“C” → “Menu Actions” → “Reformat Code”),
.IP "3." 4
insert \fCcallback\&.document\&.will-save\fP into the field “Semantic Class”, and
.IP "4." 4
change the menu option for the field “Save” to “Nothing”
.PP
.PP
\&. After that change TextMate will reformat C and C++ code with \fCclang-format\fP every time you save a file\&.
.PP
.SS "C++ Guidelines"
.PP
.IP "\(bu" 2
Everything as in C if not noted otherwise\&.
.IP "\(bu" 2
Do not use goto at all, use RAII instead\&.
.IP "\(bu" 2
Do not use raw pointers, use smart pointers instead\&.
.IP "\(bu" 2
C++-Files have extension \fC\&.cpp\fP, Header files \fC\&.hpp\fP\&.
.IP "\(bu" 2
Do not use \fCstatic\fP, but anonymous namespaces\&.
.IP "\(bu" 2
Write everything within namespaces and do not prefix names\&.
.IP "\(bu" 2
Oriented towards \fBhttps:\fP//github\&.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines\&.md 'more safe and modern usage'\&.
.PP
.PP
\fBExample:\fP \fCsrc/bindings/cpp/include/kdb\&.hpp\fP
.PP
.SS "CMake Guidelines"
.PP
We use a similar style for CMake as we do for other code:
.PP
.IP "\(bu" 2
The length of a functions should not exceed 100 lines\&.
.IP "\(bu" 2
The length of a file should not exceed 1000 lines\&.
.IP "\(bu" 2
A line should not be longer than 140 characters\&.
.IP "\(bu" 2
Use tabs for indentation\&.
.IP "\(bu" 2
One tab equals 8 spaces\&.
.IP "\(bu" 2
Declare variables as late as possible, preferable within blocks\&.
.IP "\(bu" 2
Add a space character before round parenthesis ( \fC(\fP )\&.
.IP "\(bu" 2
Use lower case for command names (e\&.g\&. \fCset\fP instead of \fCSET\fP)
.PP
.PP
.SS "cmake format"
.PP
We use \fC\fCcmake-format\fP\fP to reformat code according to the guidelines given above\&. Since \fCcmake-format\fP currently does not support tabs, we use the standard command \fCunexpand\fP to fix this issue\&. For example, to reformat the file \fCCMakeLists\&.txt\fP in the root folder of the repository we use the following command:
.PP
.PP
.nf
# This command uses `sponge`, which is part of the [moreutils](https://joeyh\&.name/code/moreutils/) package\&.
cmake-format CMakeLists\&.txt | unexpand | sponge CMakeLists\&.txt
.fi
.PP
.PP
.SS "Installation"
.PP
Since \fCcmake-format\fP is written in \fCPython\fP you usually install it via Python’s package manager \fCpip\fP:
.PP
.PP
.nf
# Install cmake format `0\&.5\&.4` with support for YAML config files
pip install cmake-format[yaml]==0\&.5\&.4
.fi
.PP
.PP
\&. Please make sure, that you install the correct version (\fC0\&.5\&.4\fP) of cmake format:
.PP
.PP
.nf
cmake-format --version
#> 0\&.5\&.4
.fi
.PP
.PP
, since otherwise the formatted code might look quite different\&.
.PP
We also use the \fCmoreutils\fP in our \fBCMake formatting script\fP, which you can install on macOS using \fCHomebrew\fP:
.PP
.PP
.nf
brew install moreutils
.fi
.PP
.PP
and on Debian using \fCapt-get\fP:
.PP
.PP
.nf
apt-get install moreutils
.fi
.PP
.PP
.SS "Usage"
.PP
If you want to reformat the whole codebase you can use the script \fB`reformat-cmake`\fP:
.PP
.PP
.nf
scripts/reformat-cmake # Running this script for the whole code base takes some time\&.
.fi
.PP
.PP
\&. To reformat specific files add a list of file paths after the command:
.PP
.PP
.nf
# The command below reformats the file `cmake/CMakeLists\&.txt`\&.
scripts/reformat-cmake cmake/CMakeLists\&.txt
.fi
.PP
.PP
.SS "Tool Integration"
.PP
If you work on CMake code quite often you probably want to integrate cmake format into your development workflow\&. The homepage of \fCcmake format\fP list some integration options\&.
.PP
.SS "TextMate"
.PP
While TextMate does not support cmake format directly, you can quickly create a command that applies \fCcmake-format\fP every time you save a CMake file yourself\&. The steps below show one option to do that\&.
.PP
.IP "1." 4
Open the “Bundle Editor”: Press \fC^\fP + \fC⌥\fP + \fC⌘\fP + \fCB\fP
.IP "2." 4
Create a new command:
.IP " 1." 6
Press \fC⌘\fP + \fCN\fP
.IP " 2." 6
Select “Command”
.IP " 3." 6
Press the button “Create”
.PP
.IP "3." 4
Configure your new command
.IP " 1." 6
Use “Reformat Document” or a similar text as “Name”
.IP " 2." 6
Enter \fCsource\&.cmake\fP in the field “Scope Selector”
.IP " 3." 6
Use \fC^\fP + \fC⇧\fP + \fCH\fP as “Key Equivalent”
.IP " 4." 6
Copy the text \fCcallback\&.document\&.will-save\fP into the field “Semantic Class”
.IP " 5." 6
Select “Document” as “Input”
.IP " 6." 6
Select “Replace Document” in the dropdown menu for the option “Output”
.IP " 7." 6
Select “Line Interpolation” in the menu “Caret Placement”
.IP " 8." 6
Copy the following code into the text field:
.PP
``` #!/bin/bash
.PP
set -o pipefail if ! '${TM_CMAKE_FORMAT:-cmake-format}' - | ${TM_CMAKE_FORMAT_FILTER:-tee}; then \&. '$TM_SUPPORT_PATH/lib/bash_init\&.sh' exit_show_tool_tip fi ```
.IP " 9." 6
Save your new command: \fC⌘\fP + \fCS\fP
.IP " 10." 6
Store the value \fCunexpand\fP in the variable \fCTM_CMAKE_FORMAT_FILTER\fP\&. To do that save the text
.PP
.PP
.PP
.PP
.nf
TM_CMAKE_FORMAT_FILTER = "unexpand"
.fi
.PP
.PP
in a file called \fC\fC\&.tm_properties\fP\fP in the root of Elektra’s repository\&.
.PP
.SS "Java / Groovy Guidelines"
.PP
Please follow \fCGoogle Java Style Guide\fP for Java and Groovy (used by Jenkins) files\&.
.PP
Most notably use:
.PP
.IP "\(bu" 2
2 spaces for indentation
.IP "\(bu" 2
Variable and function names in lowerCamelCase
.IP "\(bu" 2
K & R style brackets
.PP
.PP
.SS "Markdown Guidelines"
.PP
.IP "\(bu" 2
File Ending is \fC\&.md\fP or integrated within Doxygen comments
.IP "\(bu" 2
Only use \fC#\fP characters at the left side of headers/titles
.IP "\(bu" 2
Use \fCfences\fP for code/examples
.IP "\(bu" 2
Prefer fences which indicate the used language for better syntax highlighting
.IP "\(bu" 2
Fences with sh are for the \fBshell recorder syntax\fP
.IP "\(bu" 2
\fCREADME\&.md\fP and tutorials should be written exclusively with shell recorder syntax so that we know that the code in the tutorial produces output as expected
.IP "\(bu" 2
Please use \fC\fBtitle-case\fP\fP for headings in the general documentation\&.
.IP "\(bu" 2
For \fBman pages\fP please use \fBonly capital letters for subheadings\fP and only \fBsmall letters for the main header\fP\&. We use this header style to match the look and feel of man pages for Unix tools such as \fCls\fP or \fCmkdir\fP\&.
.PP
.PP
.SS "Prettier"
.PP
We use \fC\fCprettier\fP\fP to format the documentation according to the guidelines given above\&.
.PP
Under certain \fBexceptional\fP circumstances you might want to prevent \fCprettier\fP from formatting certain parts of a Markdown file\&. To do that you can
.PP
.IP "\(bu" 2
enclose the Markdown code in \fCand\fP tags, or
.IP "\(bu" 2
use `` to disable formatting till the end of a file
.PP
.PP
.SS "Installation"
.PP
.SS "macOS"
.PP
On macOS you can install \fC\fCprettier\fP\fP using \fCHomebrew\fP:
.PP
.PP
.nf
brew install prettier
.fi
.PP
.PP
.SS "General"
.PP
To install \fC\fCprettier\fP\fP using Node’s package manager \fCnpm\fP you can use the command below
.PP
.PP
.nf
npm install --global prettier@1\&.17\&.1
.fi
.PP
.PP
.SS "Usage"
.PP
You can format all Markdown files in the repository using the script \fB`reformat-markdown`\fP:
.PP
.PP
.nf
scripts/reformat-markdown
.fi
.PP
.PP
\&. To format only some files, please specify a list of filenames after the command:
.PP
.PP
.nf
scripts/reformat-markdown doc/CODING\&.md # Reformat this file
.fi
.PP
.PP
.SS "Tool Integration"
.PP
The \fChomepage of Prettier\fP lists various options to integrate the tool into your workflow\&.
.PP
.SS "TextMate"
.PP
To reformat a Markdown document in \fCTextMate\fP every time you save it, please follow the steps listed below\&.
.PP
.IP "1." 4
Open the “Bundle Editor”: Press \fC^\fP + \fC⌥\fP + \fC⌘\fP + \fCB\fP
.IP "2." 4
Create a new command:
.IP " 1." 6
Press \fC⌘\fP + \fCN\fP
.IP " 2." 6
Select “Command”
.IP " 3." 6
Press the button “Create”
.PP
.IP "3." 4
Configure your new command
.IP " 1." 6
Use “Reformat Document” or a similar text as “Name”
.IP " 2." 6
Enter \fCtext\&.html\&.markdown\fP in the field “Scope Selector”
.IP " 3." 6
Use \fC^\fP + \fC⇧\fP + \fCH\fP as “Key Equivalent”
.IP " 4." 6
Copy the text \fCcallback\&.document\&.will-save\fP into the field “Semantic Class”
.IP " 5." 6
Select “Document” as “Input”
.IP " 6." 6
Select “Replace Input” in the dropdown menu for the option “Output”
.IP " 7." 6
Select “Line Interpolation” in the menu “Caret Placement”
.IP " 8." 6
Copy the following code into the text field:
.PP
``` #!/bin/bash
.PP
if ! '${TM_PRETTIER:-prettier}' --stdin --stdin-filepath '${TM_FILEPATH}' then \&. '$TM_SUPPORT_PATH/lib/bash_init\&.sh' exit_show_tool_tip fi ```
.IP " 9." 6
Save your new command: \fC⌘\fP + \fCS\fP
.PP
.PP
.PP
.SS "Shell Guidelines"
.PP
.IP "\(bu" 2
Please only use \fCPOSIX\fP functionality\&.
.PP
.PP
.SS "shfmt"
.PP
We use \fC\fCshfmt\fP\fP to format Shell files in the repository\&.
.PP
.SS "Installation"
.PP
.SS "macOS"
.PP
You can install \fC\fCshfmt\fP\fP on macOS using \fCHomebrew\fP:
.PP
.PP
.nf
brew install shfmt
.fi
.PP
.PP
.SS "General"
.PP
\fCshfmt’s GitHub release page\fP offers binaries for various operating systems\&. For example, to install the binary for the current user on Linux you can use the following command:
.PP
.PP
.nf
mkdir -p "$HOME/bin" && cd "$HOME/bin" && \
curl -L "https://github\&.com/mvdan/sh/releases/download/v2\&.6\&.4/shfmt_v2\&.6\&.4_linux_amd64" -o shfmt && \
chmod u+x shfmt
.fi
.PP
.PP
\&. Please note that you have to make sure, that your \fCPATH\fP includes \fC$HOME/bin\fP, if you use the command above:
.PP
.PP
.nf
export PATH=$PATH:"$HOME/bin"
.fi
.PP
.PP
.SS "Usage"
.PP
We provide the script \fB`reformat-shfmt`\fP that formats the whole codebase with \fC\fCshfmt\fP\fP:
.PP
.PP
.nf
scripts/reformat-shfmt
.fi
.PP
.PP
\&. You can also reformat specific files by listing filenames after the script:
.PP
``\fC scripts/reformat-shfmt scripts/reformat-shfmt # Reformat the source of\fPreformat-shfmt`
.PP
.nf
\&.
##### Tool Integration
The GitHub project page of [`shfmt`][] offers some options to integrate the tool into your development workflow [here](https://github\&.com/mvdan/sh#related-projects)\&.
###### TextMate
The steps below show you how to create a [TextMate][] command that formats a documents with [`shfmt`][] every time you save it\&.
1\&. Open the “Bundle Editor”: Press ^ + ⌥ + ⌘ + B
2\&. Create a new command:
1\&. Press ⌘ + N
2\&. Select “Command”
3\&. Press the button “Create”
3\&. Configure your new command
1\&. Use “Reformat Document” or a similar text as “Name”
2\&. Enter `source\&.shell` in the field “Scope Selector”
3\&. Use ^ + ⇧ + H as “Key Equivalent”
4\&. Copy the text `callback\&.document\&.will-save` into the field “Semantic Class”
5\&. Select “Document” as “Input”
6\&. Select “Replace Input” in the dropdown menu for the option “Output”
7\&. Select “Line Interpolation” in the menu “Caret Placement”
8\&. Copy the following code into the text field:
.fi
.PP
#!/bin/bash
.PP
if ! '${TM_SHFMT_FORMAT:-shfmt}' -s -sr; then \&. '$TM_SUPPORT_PATH/lib/bash_init\&.sh' exit_show_tool_tip fi ```
.PP
.IP "9." 4
Save your new command: \fC⌘\fP + \fCS\fP
.PP
.PP
.SS "Doxygen Guidelines"
.PP
\fCdoxygen\fP is used to document the API and to build the html and pdf output\&. We also support the import of Markdown pages\&. Doxygen 1\&.8\&.8 or later is required for this feature (Anyways you can find the \fCAPI Doc\fP online)\&. Links between Markdown files will be converted with the \fBMarkdown Link Converter\fP\&. \fBMarkdown pages are used in the pdf, therefore watch which characters you use and provide a proper encoding!\fP
.PP
.IP "\(bu" 2
use \fC@\fP to start Doxygen tags
.IP "\(bu" 2
Do not duplicate information available in git in Doxygen comments\&.
.IP "\(bu" 2
Use \fC\\copydetails\fP, \fC@copybrief\fP and \fC@copydetails\fP intensively (except for file headers)\&.
.PP
.PP
.SS "File Headers"
.PP
Files should start with:
.PP
```c /**
.IP "\(bu" 2
.PP