File: coding-conventions.md

package info (click to toggle)
kwin 4%3A6.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,376 kB
  • sloc: cpp: 240,899; javascript: 2,225; xml: 2,096; ansic: 775; sh: 37; python: 15; makefile: 8
file content (86 lines) | stat: -rw-r--r-- 2,513 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Coding Conventions

This document describes some of the recommended coding conventions that should be followed in KWin.

For KWin, it is recommended to follow the KDE Frameworks Coding Style.


## `auto` Keyword

Optionally, you can use the `auto` keyword in the following cases. If in doubt, for example if using
`auto` could make the code less readable, do not use `auto`. Keep in mind that code is read much more
often than written.

* When it avoids repetition of a type in the same statement.

  ```
  auto something = new MyCustomType;
  auto keyEvent = static_cast<QKeyEvent *>(event);
  auto myList = QStringList({ "FooThing",  "BarThing" });
  ```

* When assigning iterator types.

  ```
  auto it = myList.const_iterator();
  ```


## `QRect::right()` and `QRect::bottom()`

For historical reasons, the `QRect::right()` and `QRect::bottom()` functions deviate from the true
bottom-right corner of the rectangle. Note that this is not the case for the `QRectF` class.

As a general rule, avoid using `QRect::right()` and `QRect::bottom()` as well methods that operate
on them. There are exceptions, though.

Exception 1: you can use `QRect::moveRight()` and `QRect::moveBottom()` to snap a `QRect` to
another `QRect` as long as the corresponding borders match, for example

```
// Ok
rect.moveRight(anotherRect.right());
rect.moveBottom(anotherRect.bottom());
rect.moveBottomRight(anotherRect.bottomRight());

// Bad
rect.moveRight(anotherRect.left() - 1); // must be rect.moveLeft(anotherRect.left() - rect.width());
rect.moveBottom(anotherRect.top() - 1); // must be rect.moveTop(anotherRect.top() - rect.height());
rect.moveBottomRight(anotherRect.topLeft() - QPoint(1, 1));
```

Exception 2: you can use `QRect::setRight()` and `QRect::setBottom()` to clip a `QRect` by another
`QRect` as long as the corresponding borders match, for example

```
// Ok
rect.setRight(anotherRect.right());
rect.setBottom(anotherRect.bottom());
rect.setBottomRight(anotherRect.bottomRight());

// Bad
rect.setRight(anotherRect.left());
rect.setBottom(anotherRect.top());
rect.setBottomRight(anotherRect.topLeft());
```

Exception 3: you can use `QRect::right()` and `QRect::bottom()` in conditional statements as long
as the compared borders are the same, for example

```
// Ok
if (rect.right() > anotherRect.right()) {
    return;
}
if (rect.bottom() > anotherRect.bottom()) {
    return;
}

// Bad
if (rect.right() > anotherRect.left()) {
    return;
}
if (rect.bottom() > anotherRect.top()) {
    return;
}
```