aboutsummaryrefslogtreecommitdiff
path: root/docs/CODING_STYLE.md
blob: a0817ac0de09671fc9d987cfc903565ba28b6c00 (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# fases - Ferass' Base System

*Simple coreutils for a fully functionnal UNIX-like system*

[« Go back](/README.md)

## Coding Style

The programming language used is ***POSIX C99*** and all code must be written 
in ***POSIX C99***, no extensions are used.

The coding style is as follows:

### General

- The code shall be as simple to understand as possible, add comments if 
necessary ;

- For indentation, the code shall use tab characters ;

- The code shall avoid using GNU-specific functions (as that would make 
the program less portable) ;

- Each line shall not exceed 79 characters ;

- Utilities shall only implement POSIX-compliant options, if the utility 
supports options, and the specified option is invalid, the utility shall 
print a help text (this applies only to coreutils) ;

- Utilities shall only use the **standard C library and POSIX libraries** ;

- For comments, instead of using `//` (as in `// Comment`), the code shall 
use `/*` and `*/` (as in `/* Comment */`) ;

- For commenting code, use `//` (as in `// printf("broken code")`) ;

- **Variable names** shall describe the variable's purpose ;

- The `main()` function shall be placed at the top, after function 
prototypes, to increase code readability ;

### Blocks

- In a block, do not use `{` and `}` if there's only one instruction inside ;

```
for (int i; i != 3; i++)
	printf("%d\n", i);
```

- When using `{` and `}`, `{` shall not be on it's own line ;

```
if (i) {
	printf("%d", i);
	i = 3;
}
```

- The block shall only use 1 line if there's only one instruction ran and 
that shall not exceed 79 characters ;

```
if (foo[1] == 'a') foo[1] = 'b';
```

### Functions

- The function type shall be on the same line as the function name and `{` ;

```
int main() {
	printf("Hello World.\n");
}
```

- Function names shall describe the function's intended purpose ;

- When exiting, the function shall use `return` instead of `exit()` ;
	
```
int calculate(int i) {
	int result;
	if (i != 0)
		result = 30 / 1;
		return 0;
	else
		return 1;
}
```

- Variable declarations shall be at the top of the function ;

```
int main() {
	int i, result;
	char foo;
	bool bar;
}
```

### Switch

- In `switch (foo)`, `case` shall be indented ;

```
switch (foo) {
	case 'b':
		break;
	case 'a':
		printf("bar\n");
	default:
		return 0;
}
```

### Preprocessor statements

- The `#define` preprocessor statement shall only be used for setting 
constant values ;

```
#define FOO 80
```

- Preprocessor statements shall not be indented.