Löwenware

The Right Coding Style for C

Being a C developer means to be facing various style of coding (or lack of it) quite often. Programmers, communities, corporations and software foundations may define and follow own rules without (or almost without) any affect on a resulting application work process and efficiency. Such freedom and flexibility that C language grants to developers is one of its greatest adventages and a proof of genius of Dennis Ritchie and Kenneth Thompson.

But what is the right coding style for C?

Sometime ago during discussion of coding style with one of my customers I've asked myself this question. Need to say I am quite flexible with code formatting and ready to change my habbits, especially if there is a reason behind it. I did some research and found that many people has no idea why to format code in one or another way. And with all my respect "because Dennis and Kenneth was doing so" - is not a reason. First of all because it was long time ago and many things had changed. Another reason "because it is better for readability" is too subjective. A real reason could be something that affects some of the important things: product quality, required resources, earnings.

It is actual even for non-commercial single-maintainer projects, because program should do its job, your time is a resource and you never know who and when will be interested in your work in future. Code style and cleanliness in this case is a somthing that could really affect the price. So it is worth to follow at least some rules than nothing. Needless to say that for commercial projects, importance of those things goes higher.

File Layout

This is one of the most natural language aspects. Almost all developers are following such or similar order:

To get work done you need things to be declared before first use. This language requirement in the end helps programmer to find necessary definition or declaration in backward search through file.

Important note here could be done regarding static functions, which declarations is better to avoid when possible. It helps to keep their definitions in logical order, preserve file size and simplify developer's life when function return value or parameters list changes.