tags:

views:

144

answers:

3

It has been a while since I've written C, and I never really did any formatting when I did it since it was all toy work and inconsequential. For open source C projects, what would be the best (being the most agreed upon and utilized) style guide (Formatting and convention wise) to follow?

+6  A: 

C and C++ Style Guides lists many style guides. I think you should use choose one that is used in the area of your OS project. I.g. GNU people like other formatting than linux kernel developers.

Just examples:

stacker
+1  A: 

That's a vast and difficult area, there are as many opinions as there are programmers. In my opinion though, the most important is to be consistent, nothing is worse than to try to find what block belongs to what condition because the lines are sometime indented with spaces, sometimes with tabs, sometimes 4 spaces per indentations then elswhere with 2 ...

Then, it is important to make the thing readable. Readability is enhanced if the code is grouped by logical units, spacing these group a little bit (comparable to paragraphs in text). When several lines do very similar things, align them so that they can be edited in columns.

if((pInfo->pSelects.Include.DT = CmdBuildMap(Tbl+TAG_SELINC_DT))) Counter++;
if((pInfo->pSelects.Include.DO = CmdBuildMap(Tbl+TAG_SELINC_DO))) Counter++;
if((pInfo->pSelects.Include.RQ = CmdBuildMap(Tbl+TAG_SELINC_RQ))) Counter++;
if((pInfo->pSelects.Include.YR = CmdBuildMap(Tbl+TAG_SELINC_YR))) Counter++;
if((pInfo->pSelects.Exclude.DT = CmdBuildMap(Tbl+TAG_SELEXC_DT))) Counter++;
if((pInfo->pSelects.Exclude.DO = CmdBuildMap(Tbl+TAG_SELEXC_DO))) Counter++;
if((pInfo->pSelects.Exclude.RQ = CmdBuildMap(Tbl+TAG_SELEXC_RQ))) Counter++;
if((pInfo->pSelects.Exclude.YR = CmdBuildMap(Tbl+TAG_SELEXC_YR))) Counter++;

is more readable than

pInfo->pSelects.Include.DT = CmdBuildMap(Tbl+TAG_SELINC_DT);
if(pInfo->pSelects.Include.DT) 
  Counter++;
pInfo->pSelects.Include.DO = CmdBuildMap(Tbl+TAG_SELINC_DO);
if(pInfo->pSelects.Include.DO) 
  Counter++;
pInfo->pSelects.Include.RQ = CmdBuildMap(Tbl+TAG_SELINC_RQ);
if(pInfo->pSelects.Include.RQ) 
  Counter++;
pInfo->pSelects.Include.YR = CmdBuildMap(Tbl+TAG_SELINC_YR);
if(pInfo->pSelects.Include.YR) 
  Counter++;
pInfo->pSelects.Exclude.DT = CmdBuildMap(Tbl+TAG_SELEXC_DT);
if(pInfo->pSelects.Exclude.DT) 
  Counter++;
pInfo->pSelects.Exclude.DO = CmdBuildMap(Tbl+TAG_SELEXC_DO);
if(pInfo->pSelects.Exclude.DO) 
  Counter++;
pInfo->pSelects.Exclude.RQ = CmdBuildMap(Tbl+TAG_SELEXC_RQ);
if(pInfo->pSelects.Exclude.RQ) 
  Counter++;
pInfo->pSelects.Exclude.YR = CmdBuildMap(Tbl+TAG_SELEXC_YR);
if(pInfo->pSelects.Exclude.YR) 
  Counter++;

(and just by editing here for SO the second form was really difficult to get right)

As you can see, I even used a side-effect form of instruction to make the code more readable in column form, even if I wouldn't do it for only one statement.

This is highly subjective of course.

tristopia
+3  A: 

Many major (open-source) projects have their own conventions and style guides, so you will want to check to see if the problem you wish to contribute to already has a style guide for you to read. Otherwise, being familiar with one or more of the common styles.

Just avoid the other religious war of which editor to use.

Addition examples (continued from @stacker):

mctylr