Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 12 down vote accepted

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.

share|improve this answer
    
+1 for corporate momentum, lots of that going around. –  JeffO Jun 7 '13 at 16:54
    
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 '13 at 19:43
    
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 '13 at 14:01

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. :)

share|improve this answer
    
Thanks, the grep trick will be useful. –  gsingh2011 Jun 7 '13 at 17:57

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)

Flash forward to modern programming style with long, descriptive names and parametrized types and it becomes a disaster. 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 cargo cult offspring, Java and C#, seem to be the exceptions to having readable declarations/definitions. Even their popular predecessors and peers (Fortran, ALGOL, Pascal) had names before result types.

Putting names at the start of the line makes the code easier to read regardless of the editor it is being viewed in. I'm puzzled as to why people think it's a legacy practice as the benefit was never that great until long names became more common and types got more complex. You could put nicely formatted comments with the names in lights but that takes a lot more effort than just hitting Enter instead of Space.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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