Purpose of this document

This document describes the recommended coding style for kdepim and akonadi. Nobody is
forced to use this style, but to have consistent formatting of the source code
files it is strongly recommended to make use of it.

Why is coding Style usefull?

Let us make a comparision with real life.
To make an addition, one can do:

123
+ 456
==========
= 579

But we have learned at the primary to write:

123
+456

==

=579

Which is much more readable, easy to control (or debug).

This is Coding Style: not necessary but very usefull, and pretty to read.

What do we need?

We need at least:

a specification (a set of rules) for the coding style of the sources

some tools to check the sources against the specification

some tools to change the sources

astyle is a pretty tool to make such changes. But astyle doesn't implement (yet) all the specification rules.

You can find below some awk-scripts which help us to check all the rules.

You can find below some awk-scripts which help us to make most of the changes.
The last part must be done manually.

The specification rules of coding style for kdepim and akonadi

Identation with four spaces, don't use any <TAB>s

Trim the lines

Only single empty lines

The first line, the last line(s) may not be empty

Only one statement per line

Variable declaration

Only one declaration per line

Use a space after each keyword, but not after a cast

Use a space after the name of the class

include directive

Place * and & near the variable

Use namespace foo { in the same line

Each member initialization of a method in a separate line

Surround all operators with spaces

switch rules

if, for, while and similar macros rules

typedef statement over more lines

Don't use &, without a variable

Don't use untyped enum

Don't use enum with empty member

No ; after some macros

No "one line" ifforwhile statement

No code after {

No code before '} (but else)

No header and body code in the same line, even empty body

No space between some keywords

No space around the index of an array

No space around an expression surrounded with braces

No space before : in a case statement

No space before ; at the end of statement

Migration

As discussed at the KDEPIM meeting, Berlin, 3 March 2013, all the files of KDEPIM will
be reviewed to follow the coding style. This will be done over a long time,
directory after directory, for each of the
rules defined above. For each rule, one can find one or two script(s).

Two scripts to check all the rules and to make the all the changes

Most of the rules can be check with the scripts below.
For some of the rules, we don't have a script to change the sources. It is better to make first a check for such a rule, make the modification(s) to suscript the rule(s) before using the change-script(s).

No ; after some macros

Looking over the git-history, one can find some "pedantic" changes.
These are changes to make a better code. The most of them are at the use of macro, where it is not necessary to have a ; at the end ofthe command.
The script make a check over all these:
AKTEST_MAIN;MAKE_CMD_ROW;Q_DECLARE_FLAGS;Q_PRIVATE_SLOT;Q_DECLARE_METATYPE;Q_DECLARE_OPERATORS_FOR_FLAGS;Q_DE
CLARE_PRIVATE;Q_DECLARE_PUBLIC;Q_DISABLE_COPY;K_GLOBAL_STATIC;Q_IMPORT_PLUGIN;Q_PROPERTY;Q_UNUSED;QTEST_KDEMAIN;QTEST_MAIN

No space before ; at the end of statement

Use all the scripts

put the comments away, change the strings, don't read the directive

The comments might contain some keyword. It is very difficult to avoid the confusion with the very simple awk-scripts. We prefer to change all the comments with the same number of empty lines.
It is very difficult to parse the strings correctly, so we prefer to change them to an empty string.
The same with the directive, so we prefer to change them to an empty line.

Check the objects and the libs

As a first approach, not any object may have binary change after applying one of the rules.
To check this, one uses the Md5sum-the-Objects.sh. Download the script: Media:Md5sum-the-Objects.sh.gz
Same for the libs. Use the Md5sum-the-Libs.sh. Download the script: Media:Md5sum-the-Libs.sh.gz

The script finds all the new objects, makes a comparision with the saved version:

test the object ./akonadi/CMakeFiles/akonadi-kde.dir/statisticsproxymodel.cpp.o
test the object ./akonadi/CMakeFiles/akonadi-kde.dir/entitytreeview.cpp.o
test the object ./akonadi/CMakeFiles/akonadi-kde.dir/itemfetchjob.cpp.o
test the object ./akonadi/kmime/CMakeFiles/akonadi-kmime.dir/standardmailactionmanager.cpp.o
all tests are OK

Check the assembler files

If we add or remove some lines, the debug informations included in the object file will be change also.

This is the case with the test/change of "Only single empty lines should be used", "First line, last line(s) may not be empty" and some more test/change below (adding some blocks with { and }).

For this reason it is no more possible to compare the objects.
We have to compare the assembler files.
This works pretty well for the version with CMAKE_BUILD_TYPE set to release.
For the version with CMAKE_BUILD_TYPE set to debug, we must remove all the debug informations before the comparision could take place.

Generate the assembler files

To generate the assembler files, we only need to modify the build.make in every folder.

The script Prepare-build_make_files.sh works on the all directory, finds the line with the compiler command,
duplicates the line, add a -S option and changes the name of the output to somename.s.
After a new make command, we can save all the assembler files with the script Check-the-assembler_code.sh.
Download the script: Media:Prepare-build_make_files.gz

Remove the debug informations

The debug informations change with the changes of line numbers.
We drop all these debug informations before making the test.

The script to check the assembler files can be used in the same way as the one above (Check-the-Objects.sh).
To check this, one uses the Check-the-assembler_code.sh. Download the script: Media:Check-the-assembler_code.sh.gz