user1242967
user1242967

Reputation: 1290

Long function declaration line. What are the conventions for splitting it?

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?

Upvotes: 15

Views: 18932

Answers (2)

yyny
yyny

Reputation: 1736

There are many conventions for writing large function declarations in C. Here are some of the most common ones:

      /////////////////////////////
     //  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 );

Chose one and stick to it. Consistency and readability are more important than religion and style.

[1]: With cindent, turned on for C/C++ by default.
[2]: Not sure if it was in Java, JavaScript, or somewhere else, but I've seen this one used extensively before.

Upvotes: 17

user1898811
user1898811

Reputation:

This is completely a matter of taste, but I'd be a fan of something along the lines of:

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

Additionally, you could typedef the function pointer type you reference and give it a handy name.

Upvotes: 20

Related Questions