22

I just started at a company and one of the style comments at my first code review was that the return type and method name should be on different lines. For example, this

void foo() {
}

should be this

void
foo() {
}

I've always used the first style, and I was wondering if there is any reason other than personal preference why people use the second style? I don't think the first one hurts readability at all. Is one more common than the other with C programmers and large open source projects?

1

3 Answers 3

23

was wondering if there is any reason other than personal preference why people use the second style?

That's a style that was popular back in the early days of C, so the reason may just be that that's how they've done it for a very long time, they've got a lot of code that looks like that, and that's what everyone is used to. Not so much personal preference as corporate momentum.

Another reason is that function names always start in the first column. Return types vary in length and can be somewhat complex -- putting the type on its own line makes the function name easier to find.

If the company has a set style, they may also have a coding standards document. Ask for it. It may explain the reason(s) for this choice, and having a copy will help you avoid similar issues in future reviews.

6
  • 3
    I guess it's also connected to the 80 chars line limit still used and defended by many programmers. If you want to have a 80 chars limit and you want to use descriptive method/function names, you have to make some compromises. Splitting the method header is one of them.
    – Sulthan
    Jun 7, 2013 at 19:43
  • 1
    The declaration can also be longer - think about different (non-standard) modifiers, like calling convention identifiers (stdcall, etc.), information on symbol visibility (DLLEXPORT) or __attributes. And then when also looking at C++ you can have relatively complex return types so somewhere one might have to add a line break.
    – johannes
    Jun 8, 2013 at 14:01
  • 1
    @JAB I actually now that. I also know that reading a justified text and reading source code are two very different things and have almost nothing in common therefore that ideal width is irrelevant.
    – Sulthan
    Sep 22, 2016 at 16:17
  • 2
    "Another reason is that function names always start in the first column [and so are] easier to find." And this is much more than merely a visual benefit: Now we can search for the regex ^start_of_a_long_func and get taken immediately to the function we're searching for. Oct 3, 2017 at 19:45
  • 1
    the ability to search is the reason. When you search for where a function is defined, instead of getting back every instance of where that function name is invoked, which could be hundreds or more hits, you can just put '^' to search at the beginning of the line and get only places where it is defined. Mar 2, 2018 at 18:07
8

This is pretty much the only code formatting rule that I have found actually makes a noticeable impact on readability and it takes almost no effort (assuming your code editor doesn't start a fight with you over it).

It is good programming language design to have names appear in a consistent position in declarations/definitions. The rationale is straightforward: you have a nice visual anchor (a curly brace or just a hanging indentation) that you can use to immediately find the start of the name. You don't have to actually parse the language when scanning through a file the find the name.

It's the same as when you are formatting a document: when you start a new section you put the name up front in boldface--often on its own line--not buried somewhere, undifferentiated, in a long sentence.

Early C had very terse signatures: return types were optional and argument types were declared after the signature. Names also tended to be very short. This mitigated the impact of having an occasional return type offsetting the name.

double dot(x, y);

Is still pretty digestible.

C++ made this a bit worse. It moved argument type specifications into signatures making signatures longer. This syntax was later adopted during the standardization of C.

static struct origin *find_origin(struct scoreboard *sb,
                  struct commit *parent,
                  struct origin *origin)

is less digestible, but not too bad. (Excerpt from Git)

Now consider modern programming practices with long, descriptive names and parametrized types and see how this choice has become disastrous. An example from a Boost header:

template <class A1, class A2, class A3, class A4, class A5, class A6>
inline typename normalise<policy<>, A1, A2, A3, A4, A5, A6>::type make_policy(const A1&, const A2&, const A3&, const A4&, const A5&, const A6&)
{ 
   typedef typename normalise<policy<>, A1, A2, A3, A4, A5, A6>::type result_type;
   return result_type(); 
}

If you are writing generic code, signatures like that aren't even out of the ordinary. You can find examples of much worse cases than this without trying too hard.

C, C++, and their derivatives, Java and C#, seem to be the exceptions to having readable declarations/definitions. Their popular predecessors and peers (Fortran, ALGOL, Pascal) placed names before result types and, thankfully, many of their successors (Go, Scala, TypeScript, and Swift to name a few) have chosen more readable syntaxes as well.

3
  • 1
    Be thankful that you don't have to write Fortran programs. I tell you, separately declaring your function arguments will get on your nerves quite quickly once you are forced to do it. Especially when trying to read a function signature to understand what arguments it expects: C++ puts the types right where they belong, Fortran forces you to start a visual keyword search for the variable name to determine its type. Sep 22, 2016 at 21:19
  • 2
    Was putting the types in the function declaration really invented by C++? I'd never heard of that being the case, and am struggling to find citations. Sep 23, 2016 at 7:41
  • 1
    See The Development of the C Language. In the Standardization section Ritchie mentions the syntax was borrowed from C++. Sep 26, 2016 at 14:53
5

I first met this style on like 19th year working with C&C++. Was pretty much baffled on how could someone invent this evil thing.

The only (potentially) positive point I could find is that you can find the funciton definition using grep ^FuncName. Might be relevant factor decade+ ago in some real tool hating communities... At the place I saw it was applied to C++ and class member funcitons, that kill even this attribute.

Guess my opinion. :)

2
  • 1
    grep -P '^(\w+::)?FuncName' Sep 24, 2016 at 6:21
  • Yeah, class types in function names don't hinder the use of this at all. So, it's still a useful thing for quickly navigating source files. As for it looking evil or whatever, opinions are opinions: I've come to think it looks better. Oct 3, 2017 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.