Wednesday, 4 May 2016

"Fixing C"

There's an interesting debates going on at embedded.com and eetimes.com about Fixing C

There's a lot of debate about removing brackets, semi-colons, using pythonesque tabbing etc but a lot of this is probably "syntactic sugar" that will lead to further unexpected problems.

On that note (syntactic) one of the few changes I would make would be the removal of the "single line statement"


if (something > something_else)
    a_single_line_statement(something_else);

WHY?!

Because it leads to hell! It is FAR too easy to see someone plug a line of code in and have everything go wrong. Especially when debugging and a printk/printf/debug() line is added.

It is not so hard to use {} to delimit that and the rule about "what is a code block" becomes crystal clear.

I'd apply the same rule to empty code blocks

for(unsigned x = 0; some_array[x]--; x += some_array[x]) ;

Why not just write {} instead of ;

Now, let's not get into why the structure notation uses the same braces as code blocks. But the answer is... there's only so many braces and C uses them all (and C++ augments them with <>!)

But - what else is there?

Proper bounds checking, user defined literals (notably for strings with length instead of null termination), rotate operators and more besides.

No comments: