C Coding Style – Reason for Placing Function Type and Method Name on Different Lines

ccoding-style

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?

Best Answer

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.

Related Topic