Hurricos / realtek-poe

5 stars 10 forks source link

Remove bash-style function declarations from C code #11

Closed mrnuke closed 1 week ago

mrnuke commented 2 years ago

In shell scripts, functions do not have a return type

do_shell_stuff() {
}

Bad C code

On the other hand, in C, the return type of a function is crucial to the understanding of the function and surrounding code. The function is non severable from its return type. Thus lines like the following are non-sensical:

static int
do_c_stuff(void()
{
}

While I suspect there is a desire to write functions this way for consistency with bash code, I find this to be stupid. It detracts readability because:

  1. It increases the number of lines of code
  2. It requires more cognitive effort to understand the function

Preferred C code

It is always preferable do declare the function in a single line. If that can be done, it is the only way to do it.

static int do_c_stuff(void)
{
}

When this does not fit on a single line (80 characters according to the kernel coding guidelines), it is okay to mode the static and/or inline keyword on a separate line. In any case the return type must be on the same line as the function.

static inline
struct long_name_struct *do_long_function_name(int lots, size_t of, char arguments)
{
}

Issue

A lot of code in realtek-poe uses the "Bad C code" style. This must be refactored.