I’m learning a little C over the holiday weekend, and I started to look at other programs written in C. I ended up looking at GNU Netcat, thinking it would be a good example.
I was a bit shocked to see a 600 line main() function. Is this normal? If it is normal is this considered good C coding practices?
There’s a quote of an American President (Lincoln?) who was asked how long a man’s legs should be. “Long enough to reach from his body to the ground,” he said.
Getting back on topic:
Authors of books like “Clean Code” advertise that every function do only one thing (that’s grossly simplified by me here), so in theory your
main()should maybe call an initialization function and then another function orchestrating the work of the application, and that’s all.In practice, many programmers find a lot of tiny functions irritating. A perhaps more useful metric is that a function should usually fit on one screen, if only to make it easier to see and think about.
If a program is complex and most of its functionality is in
main(), someone hasn’t done a decent job of breaking the problem down. Essentially you should strive for manageability, understandability and readability. There’s usually no good reason for a main() to be huge.