Should I consider that declaring all C static functions is a good practice? [closed]

旧城冷巷雨未停 提交于 2019-12-12 10:49:26

问题


I recently wrote a piece of C code like that:

static void func1()
{

}

static void func2()
{

}


typedef void (*func_t)(void);

const func_t lookUpTable[FUNC_COUNT] =
{
    [FUNC1] = &func1,
    [FUNC2] = &func2
}

An other programmer worked on the same file and changed it to:

static void func1();
static void func2();

typedef void (*func_t)(void);

const func_t lookUpTable[FUNC_COUNT] =
{
    [FUNC1] = &func1,
    [FUNC2] = &func2
}

static void func1()
{

}

static void func2()
{

}

Since the funcN functions are only called thru the lookup table, I don't actually need the declarations of those functions.

Is it a matter of taste, or is there a coding style that is considered as a good/bad practice?


回答1:


It is indeed a matter of taste mostly (and coding style is always somehow a matter of opinion; the style of your partner is consistent with the habit of putting all the code after every other definitions).

In practice, you'll better ensure that your function names are unique (it makes grep-ing for them easier, and gdb will find them more easily) in the entire program, even if they are visible or used only inside one translation unit.

BTW, having your functions being non-static has also some advantages. For example, on Linux, the backtrace(3) & dladdr(3) functions are happier with globally named functions.

Also, sometimes computed gotos and threaded code (or even a plain large switch....) are faster than a table dispatch calling short functions indirectly thru a pointer. (The small overhead of calling functions, e.g. running their prologue and epilogue, might sometimes matter for tiny and quickly running code). See references here & there.



来源:https://stackoverflow.com/questions/35797254/should-i-consider-that-declaring-all-c-static-functions-is-a-good-practice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!