Reason for placing function type and method name on different lines in C

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

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

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

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật