I'm setting up these files for discussing a common clang-format
that will be used across our projects. As far as I understood, using WebKit
as base finds many of us happy. The first release has been generated by
clang-format --style=webkit -dump-config
A good starting point for the available options is Clang-Format Style Options.
Here below you can find some code that covers many cases, I'll keep this updated with the style agreed so far. I didn't find any demo code that already covers most of the C++ standard (any suggestion?). When discussing something, if it is not clear from the code below, please provide a code snippet to show your point. I'll include your example afterwards.
#include "include/header1.h"
#include "include/header2.h"
#include <iostream>
#include <vector>
namespace mynamespace {
class MyClass {
private:
int m_int;
vector m_vector;
public:
MyClass()
: m_int(42)
, m_vector(10)
{
}
~MyClass() {}
int getVar() const { return m_int; }
void MyFunction(int j)
{
if (j < 10) {
std::cerr << "Hello" << std::endl;
} else {
std::cerr << "World" << std::endl;
}
int list[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
do {
std::random_shuffle(list, list + 9);
if (list[0] == 1)
break;
} while (is_sorted(list));
}
void f()
{
f(aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
}
very::nested::namespaced::type function_long_line(very::nested::type1 t1, very::nested::type2 t2, very::nested::type3 t3) { return true; };
}
template <typename T, int size>
bool templated_function(T (&array)[size])
{
return std::adjacent_find(array, array + size, std::greater<T>()) == array + size;
}
The discussion can happen in the comments, let's use +1 and -1 and optionally write a new comment for concerns. When discussing a single option, please provide the same code formatted with the .clang-format
of this gist and the proposed one, pointing out the differences in the yaml file (provide a diff -u
if you like things well done).
If after 2 weeks no concerns are raised, I merge the change and update the demo code. Especially in the beginning of this process, when an option receives good feedback and it looks reasonable from all of us, I merge it right after.
For many options, you can use this online tool for a live demo.
Eventually there is the problem of how to enforce the application of a specific style (PR-based? Does it exist a codacy bot for style?). Though this is still premature, let's find a clang-format
with a wide agreement, and then decide on how to proceed.
### Status: Open / Merged / Closed
### Description
Put here a brief description
#### From
```cpp
```
#### To
```cpp
```
#### .clang-format changes
```diff
--- clang-format
+++ clang-format-proposed
```
I start with an example.
Status: Open
Limit line length
Let's avoid to have very long lines, the horizontal scrolling when reading the code is annoying as hell. In my personal setup I use 100 columns, and I think it is a good compromise between too short lines (that could create mess with clang-format) and horizontal scroll.
From
To
.clang-format changes