Thursday, 16 March 2017

Principles for C programming

Common sense advice that can be applied to any language. Definitely recommend the read and good C programming perspective.

Avoid magic. Do not use macros. Do not use a typedef to hide a pointer or avoid writing “struct”. Avoid writing complex abstractions. Keep your build system simple and transparent. Don’t use stupid hacky crap just because it’s a cool way of solving the problem. The underlying behavior of your code should be apparent even without context.

Had this happen so many times where code I was reading was using an obscure solution and spent hours trying to understad what was going on.

It’s more important that a novice could understand your code than it is to use some interesting way of solving the problem. Ideally, a novice will understand your code and learn something from it. Write code as if the person maintaining it was you, circa last year.

What more is there to say?

Do strict testing and reviews. Identify the different possible code paths that your changes may take. Test each of them for the correct behavior. Give it incorrect input. Give it inputs that could “never happen”. Pay special attention to error-prone patterns. Look for places to simplify the code and make the processes clearer.

Principles for C programming →

And while you’re there, check out his other articles on C programming and the lessons to learn from it.