Hacker News new | ask | show | jobs
by finishingmove 3620 days ago
> a function shouldn’t have more than 60 lines of code.

I started having headache spikes after reading this sentence that refers to keeping functions "short", in the article about safety critical programs.

3 comments

Keep in mind that we're talking about C code here. Error handling etc. blows that up quite significantly.
I agree that making functions shorter does not automatically make them safer, but I do see how having a well designed "main" function in the ballpark of ~80LOC which calls another 10 auxiliary (and inlined) minor functions of ~15-25LOC would be easier to reason about than an equivalent function of ~220LOC.

The fact that you could safisfy the business requirement with a single function of ~140LOC does not mean the other approach is more verbose, but that you are only thinking on the happy path, and a bunch of edge cases are left undefined.

While the embedded world does have some hard depth limits, my experience has been that the readability of breaking up a function are worth it. This seems especially true when the nested functions are carefully named.