crossbario / autobahn-c

Apache License 2.0
14 stars 5 forks source link

Autobahn C Coding Standards #4

Closed ericchapman closed 8 years ago

ericchapman commented 8 years ago

@oberstat So after googling it, there is no real globally used standard for C code. Every company, college, etc. seem to have invented their own. That being said, here are the two that come to mind

The GNU one is short and sweet while the Linux one seems to be more concise AND give reasons for why things are a bad idea. I am leaning towards Linux but open for suggestions.

oberstet commented 8 years ago

I dislike the GNU style. The Linux one is nicer. This is mostly K&R - which I do like.

How about this: https://www.nginx.com/resources/wiki/start/topics/examples/coding_style/

It's also K&R, but uses 4 spaces per indent.

Nginx is a modern C code base written by cracks .. scanning through some sources https://github.com/nginx/nginx/tree/master/src .. I like it. I immediately feel comfortable with this.

They seem to have the habit of placing return type and function on different lines though https://github.com/nginx/nginx/blob/master/src/event/ngx_event_accept.c#L22

Looking at RIOT OS, they do place everything on one line https://github.com/RIOT-OS/RIOT/blob/master/core/msg.c#L281

RIOT is also K&R, 4 spaces. Their style guide is here: https://github.com/RIOT-OS/RIOT/wiki/Coding-conventions.

In fact, scanning over RIOT: very clean code. Immediately readable.

void bloom_del(bloom_t *bloom)
{
...
}

I have the habit of writing this

void bloom_del (bloom_t* bloom)
{
...
}

2 spaces places differently. And the have no space only in invocation:

bloom_del(p);

I've also had a quick look at Apache and Mozilla guides .. less nice to my eyes.

Following the RIOT guide would have an additional benefit, as RIOT is the primary incubator for AutobahnC - so we are immediately "on track".

ericchapman commented 8 years ago

@oberstet Yes!! I like the RIOT one. Definitely how I naturally code as well in C. I am in agreement with that if you want to make it official. I also like the "type_t" notation which the Linux one didn't seem to like. It feels more natural in a C world.

Do we want Autobahn C types to have a prefix to avoid possible collisions or just leave it? For example

typedef struct session_s {
    ...
} session_t;

vs.

typedef struct ab_session_s {
    ...
} ab_session_t;

I think the non-prefixed type looks cleaner.

oberstet commented 8 years ago

Agreed to all! Lets follow the RIOT coding guide. And no prefixes. So I guess we can close this. I will write a CONTRIBUTING.md that persists our decision.