10

Here is the declaration

dll_DoublyLinkedNode *dll_search(const dll_DoublyLinkedList list, void *key, int (*compare)(void *data, void *key)){

Should I split it? Should I just leave it as it is? Perhaps I should do something different?

4

2 回答 2

14

这完全是一个品味问题,但我会喜欢以下方面的东西:

dll_DoublyLinkedNode *dll_search(
  const dll_DoublyLinkedList list, 
  void *key, 
  int (*compare)(void *data, void *key)
){

此外,您可以 typedef 您引用的函数指针类型并给它一个方便的名称。

于 2013-05-10T21:45:27.050 回答
12

在 C 中编写大型函数声明有许多约定。以下是一些最常见的约定:

      /////////////////////////////
     //  Type //    Meaning    ///
    /////////////////////////////
   //  `.`  // Single space   //
  //  `-|` // Half an indent //
 //  `->` // Full indent    //
/////////////////////////////

// One line
static inline int namespace_class_method(class_t *self, a_t a, b_t b, c_t c);

// Traditional C
static inline int namespace_class_method(self, a, b, c)
..class_t *self;
..a_t a;
..b_t b;
..c_t c;
{
../* Traditional K&R C had no declarations */
}

// Line wrapping
static inline int namespace_class_method(class_t *self, a_t a,
b_t b, c_t c);

// Naive
static inline int namespace_class_method(class_t *self, a_t a,
--->b_t b, c_t c);

// Linux, VIM[1]
static inline int namespace_class_method(class_t *self, a_t a,
........b_t b, c_t c);

// GNU
static inline int
namespace_class_method(class_t *self, a_t a,
.......................b_t b, c_t c);

// Java[2]

static inline int
namespace_class_method
-|(
--->class_t *self,
--->a_t a,
--->b_t b,
--->c_t c
-|);

// Haskell style
static inline int
namespace_class_method
-|( class_t *self
-|, a_t      a
-|, b_t      b
-|, c_t      c );

选择一个并坚持下去。一致性和可读性比宗教和风格更重要。

[1]:带cindent, 默认为 C/C++ 开启。
[2]:不确定它是在 Java、JavaScript 还是其他地方,但我之前已经看到过这个被广泛使用。

于 2016-11-08T18:23:44.780 回答