Compilers are best friends

In that Reddit thread about the C programming language, after I wrote:

"Almost three decade here and still definitely having fun using C..."

another user asked me:

"How do you do it? I know a bit of C, C++ but a lot of python, powershell, bash and other scripting languages. With c you have to compile everything all the time. It’s such a pain to debug stuff. Like, literally, how do you do it? I get unit testing but even that is super time consuming compared to a repl. Having to compile just to test tiny tweaks keeps me away from so many languages, because ain’t nobody got time for that. You got any tricks?"

I replied with a long comment which is worth sharing here I think. It goes as follow.

My projects are divided into small units of few hundred lines of code, the compilation process is defined in a Makefile, and the dependencies are automatically generated by the compiler. Then, the compiler always knows the very small amount of code it has to recompile each time I ask it to do so. Checking now on a project I'm currently working on (25000+ lines), it takes under one second after editing the body of one unit.

The Makefile itself is no trouble, thanks to generic rules and a common architecture to all my projects I simply always reuse the same Makefile and never think about it. The act of compiling itself is a no-brainer operation: Makefile's rules are linked to shortcut commands in my editor, which can also saves and executes at the same time. So really from editing the code to seeing the result it's one click plus one second compiling plus the time to execute (which is also probably faster in C than in Python BTW).

During the 'wasted' one second of compilation, the compiler (with all warnings turned on of course) scrutinises my code with the zeal of a fanatic extremist, and lets me know immediately the where-what-why of a good share of the bugs. Very much appreciated, rather than having to spend hours tracking them down myself once their discovered a few days later. Even better if static analysis is run at the same time.

I'm lying about the one second delay: linking a very large app may take much longer. Except that I'm not, cause if I'm caught in an edit-compile-test loop I'm certainly not compiling the whole app but only the small unit test necessary to investigate the problem. If there was no unit test to investigate the problem, well there is one now: the code I've written to investigate it and which will be added to other unit tests to prove that the problem has been solved and check it doesn't occur again later.

The repeated edit-compile-test process should also be a rarity in itself. If that occurs during debugging, using a debugger is very likely to be the correct way to work and save a lot of time. If it occurs during development, it's called programming by coincidence. Typing at random on the keyboard expecting for a miracle should halt immediately, the computer should be turned off and replaced with a pen and paper which are the only appropriate tools to think and solve a problem before trying to implement it.

In conclusion, is the compilation a pain or loss of time ? Definitely not. It's an essential tool helping me to develop faster less buggy software. When it eventually is, that's a good sign I'm doing something wrong, need a break and take a step back.

FYI I also have four years of experience of daily programming in Python. Nothing but painful memories...

My comment on Reddit stops here. One more word to clarify, it's probable that the user does something relative to data science or AI. The Reddit thread, was about C programming and my comment explicitly relates to software development and debugging. To me, data analysis or AI modelling using Python isn't about software development. It's about some individuals using software, provided as Python modules developed by other individuals, interactively through a scripting language, the REPL. Both world have different tools, different skill sets, different goals, different constraints. I think lot of confusion and grief come from that distinction being misunderstood for many various reasons and people trying to do, for example, development using Python or data analysis using C.

2022-12-04
in All, C programming, Debunked,
172 views
Copyright 2021-2024 Baillehache Pascal