C Coding

Thoughts on the C language.

Posted by dbunker on July 20, 2011

Systems engineers only decades ago were continually worried about time and space constraints due to hardware limitations. However, modern computer memory being so large and modern processors being so fast, the constant need to consider these constraints is waning. Though, it can still feel sluggish writing algorithms designed to save time and space in Python. All languages have their trade offs. C is fast, but can be painful to debug, and Python is powerful, but can be slow.

One of the greatest advantages of C code is that it is easy to see right down to the assembly. However it requires a balanced approach to use. It may be necessary to write temporary constructs while flushing out a system, but it’s important to then knock down security and robustness issues. C also forces optimization decisions to be made from the beginning, which can be a problem because so much of development is managing complexity. This is generally done by segmentation and abstraction into manageable parts.

Complexity in and of itself isn’t necessarily the problem. Building often involves making complex additions to add a feature and then refactoring to make the design better, simpler, faster, etc. It’s ignoring the refactoring step that problems arise.

C provides power, speed, and flexibility, but this can come at the cost of the ability to create layers of abstraction easily and safely. It is easy for C programs to end up being intertwined, and although encapsulation is possible, the language isn’t designed with it in mind in particular like object oriented or functional languages. Though C remains an excellent choice for writing performant programs, be they compilers with Lex and Yacc, for which C was their original target, DHT bittorent, proxy, webserver, or operating system. C is best suited for problems were efficiency and performance are key constraints, though likely less well suited for more general problems.

One could even draw a connection between the approaches of programming languages to landscapes. C like jagged mountains where it’s necessary to be careful of fault lines lest the land split apart. However, it’s possible to make the ground as smooth as glass, or rough as sand paper. Java like rolling hills, some options unattainable, but with a safer, if perhaps bigger area. Python a bit like a national park with pathways indicating the best ways through, following these pathways is powerful, but it can be difficult to forge a completely new path. Lisp is a bit like building a world on the moon, an enormous amount of flexibility, but will feel alien to many.

Despite its challenging nature, writing large programs in C can still be quite rewarding even if it’s not necessary for time and space constraints. As back when art was very difficult, hours of chiseling, or delicately painting resulted in art valued higher than cookie cutter art, even if the cookie cutter art looks similar.