Update 'Coding Style'

Patrick Vogler 2023-06-22 13:37:57 +00:00
parent 116fb27c49
commit 476b54c903

@ -43,7 +43,7 @@ Keep the length of source code lines to 100 characters or less to ensure maximum
Each new level is indented by 2 spaces, braces go on a line by themselves, and they are indented as well: Each new level is indented by 2 spaces, braces go on a line by themselves, and they are indented as well:
```c ```c
while ((c = *str++)) while (c = *str++)
{ {
if ((c >= 97) && (c <= 122)) if ((c >= 97) && (c <= 122))
{ {
@ -135,25 +135,168 @@ else
``` ```
unless one of the following 4 exceptions applies: unless one of the following 4 exceptions applies:
1. If either side of an if…else statement has braces **1.** If either side of an if…else statement has braces
```c ```c
if (stream->T == 0xFF)
{
stream->L = stream->L + stream->t
return stream->L + 1;
}
else
{
return stream->L;
}
```
**2.** If a single statement covers multiple lines
```c
if (stream->T == 0xFF)
{
stream->L = stream->L
+ stream->Lmax
+ stream->t;
}
else
{
return stream->L;
}
```
**3.** If the condition is composed of many lines
```c
if (stream->L <= stream->Lmax
stream->T == 0xFF &&
stream->t == 8)
{
return stream->L + 1;
}
else
{
return stream->L;
}
```
**4.** Nested if, in which case the block should be placed on the outermost if
```c
if (stream->L <= stream->Lmax)
{
if (stream->T == 0xFF) if (stream->T == 0xFF)
return stream->L + 1; return stream->L + 1;
else else
return stream->L; return stream->L;
}
else
{
stream->L++;
}
``` ```
2. If a single statement covers multiple lines The closing parenthesis in multiline constructs must be placed at the end of the last line of the construct, as in:
```c
```
3. If the condition is composed of many lines
```c
```
4. Nested if, in which case the block should be placed on the outermost if
```c
```
```c
# Correct
DWT_5X3_FILTER[2][5] = {{DWT_5X3_H1, DWT_5X3_H0, 0, 0, 0},
{DWT_5X3_G2, DWT_5X3_G1, DWT_5X3_G0, 0, 0}};
```
### Whitespace ### Whitespace
Always put a space before an opening parenthesis but never after. For multiple opening parenthesis only a single space in front of the first parenthesis should be used:
```c
while (c = *str++)
{
if ((c >= 97) && (c <= 122))
{
c = c - 32;
}
hash = (hash * 33) ^ c;
}
# Wrong
while(c = *str++)
{
if((c >= 97) && (c <= 122))
{
c = c - 32;
}
hash =(hash * 33) ^ c;
}
# Wrong
while (c = *str++)
{
if ( (c >= 97) && (c <= 122))
{
c = c - 32;
}
hash = (hash * 33) ^ c;
}
```
When declaring a structure type use newlines to separate logical sections of the structure:
```c
typedef struct
{
uint16 CSsgc; // Flag signaling user control variable.
uchar resilience; // Flag signalling error resilience.
uint64 tileSizeX, tileSizeY; // Spatial tile size.
uint64 tileSizeZ, tileSizeTS; // Temporal tile size.
uint64 numTilesX, numTilesY; // Spatial number of tiles.
uint64 numTilesZ, numTilesTS; // Temporal number of tiles.
uint64 numTiles; // Global number of tiles.
} bwc_gl_ctrl;
```
Do not eliminate whitespace and newlines just because something would fit on a single line:
```c
# Wrong
if (stream->T == 0xFF) return stream->L + 1; else return stream->L;
```
Do eliminate trailing whitespace on any line, preferably as a separate patch or commit. Never use empty lines at the beginning or at the end of a file.
### Functions
The following general rules should be followed when defining a function in BigWhoop:
* Function name must be lowercase, optionally separated with underscore _ character
* Functions private to a translation unit need to be declared before they are called
* All private functions of a translation unit must appear before the public functions in their respective block, which is identified by the block header 'private functions':
* All public functions must appear in their respective block, identified by the `public functions` block header, following the `private functions` block:
Functions should be declared by placing the returned value on a separate line from the function name:
```c
static void
free_tile (bwc_field *const field)
{
}
```
The argument list must be broken into a new line for each argument, with the argument names right aligned, taking into account pointers and const qualifiers:
```c
static uchar
initialize_subband (bwc_field *const field,
bwc_parameter *const parameter,
bwc_resolution *const resolution,
bwc_subband *const subband,
int32 const level,
int16 const highband)
{
}
```
This alignment holds when calling the function:
```c
initialize_subband (field,
parameter,
resolution,
&resolution->subband[m],
r,
l);
```
```c ```c
prec_control->numCodeblocks_a = (uint64) (prec_control->numCbX prec_control->numCodeblocks_a = (uint64) (prec_control->numCbX
@ -161,7 +304,6 @@ prec_control->numCodeblocks_a = (uint64)(prec_control->numCbX
* prec_control->numCbZ * prec_control->numCbZ
* prec_control->numCbTS); * prec_control->numCbTS);
``` ```
### Functions
### Derived Types ### Derived Types
## Comments ## Comments